haskell - Parsing with Happy: left-recursion versus right-recursion -


section 2.2 of happy user manual advises use left recursion rather right recursion, because right recursion "inefficient". they're saying if try parse long sequence of items, right recursion overflow parse stack, whereas left recursion uses constant stack. canonical example given is

items : item            { $1 : [] }       | items "," item  { $3 : $1 } 

unfortunately, means list of items comes out backwards.

now it's easy enough apply reverse @ end (although maddeningly annoying have everywhere parser called, rather once it's defined). however, if list of items large, surely reverse also going overflow haskell stack? no?

basically, how make can parse arbitrarily-large files , still results out in correct order?

if want entire items reversed every time, can define

items  : items_           {reverse $1}  items_ : item             { $1 : [] }        | items_ "," item  { $3 : $1 } 

reverse won't overflow stack. if need convince of this, try evaluating length $ reverse [1..10000000]


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -