3 ms·
One thing that helped me work around left-recursive restrictions is to stop thinking in terms of full expressions and to start thinking in terms of expressions
by curun1r 8y ago
One thing that helped me work around left-recursive restrictions is to stop thinking in terms of full expressions and to start thinking in terms of expressions that begin with terminals and then the suffixes that follow. This means that instead of an expr expanding to the left, it expands to the right.
Let's say you're parsing:
1 + 5 + 7
You can choose to look at it as 7 plus the expression (1+5) or you can choose to look at it as the literal 1 with a +5 suffix that then has a +7 suffix. So in this simplified example, instead of defining:
expr = literal | expr + expr
you'd define expr as:
expr = literal (+ expr)*
That shift in how I thought about the language I was creating basically ended all the left-recursive problems I was having with my parser generator library.