[antlr-interest] SimpleCalc Tree Rewrite Rules
matthew.bowman at sogotech.com
Mon Sep 17 04:00:41 PDT 2007
Eric Deplagne wrote:
> On lun, 17 sep 2007 11:31:32 +0300, Matthew Bowman wrote:
>> So I have the following grammar:
>> expr : l=term (o=plus_minus r=term)* -> ^(OP $o $l $r);
>> term : l=factor (o=mult_divide r=factor)* -> ^(OP $o $l $r);
>> And my problem is with the rewrite rules. I want it to only rewrite the
>> rule with (OP $o $l $r) if it actually matches 'term plus_minus term' or
>> 'factor mult_divide factor' but if it only matches a term or factor I
>> want the subtree from factor.
> The idea that immediately comes to me is to replace the * by +
> and add an alternative for a single term.
> l=term |
> l=term (o=plus_minus r=term)+ -> ^(OP $o $l $r)
I tried that and it also had problems. However, I manage to get around
it altogether by just making the root node the operator itself
term ((PLUS | MINUS)^ term)*
I needed the intermediate AST in the right form for my tree grammar but
I've managed to get the tree grammar to work with this approach!
Thanks for you reply,
More information about the antlr-interest