|
Imo this missed the punchline. I banged my head on the table for days trying to fix a left-recursive grammar, just by moving bits around and hoping. The key is precedence! Parse loose expressions first (plus, minus, etc), these loose expressions can in turn call into tighter expressions (mul, div), and so on down to parentheses. Parentheses do form a loop - that is, they call all the way back to the top - but at long as the parser makes progress i.e. can only proceed if it moves past a '(', no infinite loops and you're golden. |
The most useful tools I found were adjacent to the cpp-peglib library: https://github.com/yhirose/cpp-peglib
This comes with a PEG playground: https://yhirose.github.io/cpp-peglib/
I really liked pegdebug: https://mqnc.github.io/pegdebug/
With sample output here: https://mqnc.github.io/pegdebug/example/output.html
pegdebug is nice for small sets of data, but it rapidly gets swamped by anything over about 50 lines.
If anyone has other suggestions for debugging PEGs, please reply and let me know,.