Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: Left-associative binary operators in Parse::RecDescent

by jryan (Vicar)
on Dec 14, 2004 at 20:26 UTC ( #414844=note: print w/replies, xml ) Need Help??

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]} ] }

Replies are listed 'Best First'.
Re^2: Left-associative binary operators in Parse::RecDescent
by samtregar (Abbot) on Dec 14, 2004 at 20:59 UTC
    Thanks for the help! I've tried just dropping the '(' and ')' but then I get errors from PRD about the grammar being left-associative.


      Yeah, that's a good point. You'll need to get rid of the left-recursion. To do that, you'd need to get rid of binary_op and subexpression in your subexpression rule. Next, you'll need to factor out all of the paren stuff into a single rule, and then use that rule within the binary_op rule instead of subexpression. Here's an example:

      paren: '(' binary_op ')' { \$item [2] } # parens belong here, and o +nly here! | subexpression subexpression: function_call | var | literal | <error> binary_op : paren (op paren { [ \@item[1..2] ] })(s?) # any +parenned expression will sink down here { [ \$item[1], map { \@\$_ } \@{\$item[2]} ] }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://414844]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2016-10-28 05:03 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (375 votes). Check out past polls.