Bug found in algorithm for both PEG and packrat parsing of left-recursive grammars when there's right recursion embedded in the left recursion. see section 5. (PDF)
tratt.net/laurie/research/publications/papers...Posted Nov 1, 2010, 11:46 PM UTC
3513
The Earley parser, as originally formulated, also had some problems (paywall)
Tratt writes: "In essence, the [Warth et al] parser turns from (recursive) top-down in normal operation to (iterative) bottom-up when left-recursion is detected. [...] While the algorithm contains both a top-down and bottom-up parser, the two are not on an equal footing -- the top-down parser gets the fi rst opportunity to run to completion. [...] Thus in any battle between left and right-recursion in a single rule, right-recursion (using the top-down parser) always wins over left-recursion (using the bottom-up parser) [giving an incorrect result]."
Tratt gave a modified algorithm that parsed correctly a safe subset of direct left-recursive PEGs, but never looked at indirect left-recursion. He instead wonders "Are PEGs really suited to allowing left-recursion?"
I've implemented a PEG parser generator (in C++) and found that trying to add support for left recursion was extremely cumbersome, as well as taking a decent hit on performance due to memoised results being ignored while recursing.
I took it out and the simplicity and elegance of PEG was restored, yay. Thankfully JavaScript (for which I needed the parser) doesn't have a left recursive grammar.
Left recursion hardly ever makes sense if you allow EBNF.
Except to enforce associativity at the parse level. The normal technique when using parsing combinators on right-recursive grammars is for the left-associative repetition parser combinator to close over partial applications and deliver the result in CPS after both branches of the tree are known. Horribly cumbersome.
I also implemented a PEG parsing library, and never managed to fully implement A.Warth's parsing algorithm. Direct left-recursion works (most of the the time), but I always found problems with indirect left-recursion.
Besides implementation problem, there are major CPU and memory penalties for writing a recursive grammar in PEG -- at the end of the day, it's better to write the grammar without recursive rules (which is not that difficult), and you get a better performance and a much simpler implementation.
If you're worried about performance and have been re-writing the grammar, can't you take the extra step to using a lexer and a shift-reduce parser?
aside: Didn't those early guys in comp sci obsess on proving algorithms? So such problems wouldn't occur... or did it turn out that the proofs weren't proven after all... so that having a proof doesn't 100% prove it. just an aside, since PEG's have struck me as an agile approach, not old-school.
I also ask because I'm still very conflicted about the value of proofs in comp sci. Sometimes an agile, anti-Dijkstra approach seems better: you have a stab, and correct bugs as they arise. No need to get it all "correct" before you even begin - especially since your proof probably has bugs in it itself, they're just so much harder to find (since they are only eye-balled, and aren't being "run" all the time by different users, in different ways, which really flushes out bugs - in particular, the ones that make a difference). I know you can use tools like COQ to "run" proofs.
There's an old saw from Knuth floating around (I believe from WEB, but not sure; it might be MetaPost or the like):
"Be careful: I haven't tested this code, only proven it correct."
It would be interested to include formal analysis, unit testing, & things like fuzzing with everything you do.
actually, the recognizer was proved correct but the parser was where the bug was in the earley paper.
Earley parsers and Packrat parsing are quite similar. Packrat parsing is a depth first search of a grammar, and Earley Parsing is a breadth first search.
Amusingly enough: Left recursion is no problem in earley parsers, but right recursion runs in quadratic time.
Although pegs can recognize a limited set of grammars in linear time, earley parsers can recognize the set of lr-regular grammars in linear time. (That is, LR grammars with a DFA for lookahead)
(Leo Joop showed how to do this improvement)
As much as I like parsing expression grammars, I'm more tempted by the well tuned earley approach as it has much more room for error handling and correction.