by zaptheimpaler · 8y ago · 0 comments
Like:
expr = expr + expr
expr = expr.method(args)
I'm trying to write a parser using a parser combinator library, and left-recursive things are turning out to be really tricky. expr =
identifier : var
.operators postfix
'.' identifier '(' args ')' : method-call
.operators infix left
'+' : plusLet'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.