I haven't read everything yet, but I thought I'd post a link to a tutorial of mine before I come back to this. Operator Associativity and Eliminating Left-Recursion in Parse::RecDescent was written for Parse::RecDescent, but the principles apply to other LL parsers too.
Quickly, I think
expr : expr expr binop { [ BINOP => [ $item[3], $item[1], $item[2] ] ] } | term
can be transformed into
expr : term { push($item[1]) } expr_ | term expr_ : expr binop { [ BINOP => [ $item[2], pop(), $item[1] ] ] }
Factoring out common prefixes for speed:
expr : term { push($item[1]) } expr_1 expr_1 : expr_2 | # Automatic match { pop() } expr_2 : expr binop { [ BINOP => [ $item[2], pop(), $item[1] ] ] }
Inlining expr_2 for simplicity:
expr : term { push($item[1]) } expr_ expr_ : expr binop { [ BINOP => [ $item[2], pop(), $item[1] ] ] } | # Automatic match { pop() }
In short, keep looking for terms until you meet something that isn't (an operator), then eat up the last two terms that have been met.
I'll come back later to verify this and put it into 5.10 regex syntax.
Update: Fixed link
In reply to Re: An adventure with 5.10 regular expressions
by ikegami
in thread An adventure with 5.10 regular expressions
by hipowls
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |