http://qs321.pair.com?node_id=414844


in reply to Left-associative binary operators in Parse::RecDescent

Precedence discussions aside (it didn't seem like you need precedence levels here), your problem is that you're only trying to match two things in a binary_op. Binary ops are more like "chains of one or more expressions of equal precedence", and since you only seem to have one level of precedence here, we can just do a loop:

binary_op : '(' subexpression (op subexpression {[\@item[1..2]]} ) +(s?) ')' { [ \$item[2], map { \@\$_ } \@{\$item[3]} ] }

If you want to be able to have an alternative that doesn't need parens, just mirror the same rule without parens. Let me know if you still have trouble...

P.S.: When writing PRD grammars, its useful to use a non-interpolating heredoc (e.g. my $grammar = << '__ENDG__') so you don't have to backslash anything. :)

binary_op : '(' subexpression (op subexpression {[@item[1..2]]})(s +?) ')' { [ $item[2], map { @$_ } @{$item[3]} ] }