[antlr-interest] SimpleCalc Tree Rewrite Rules

Eric Deplagne Eric.Deplagne at loria.fr
Mon Sep 17 03:55:13 PDT 2007


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.

  expr: 
    l=term |
    l=term (o=plus_minus r=term)+ -> ^(OP $o $l $r)
  ;

-- 
  Eric Deplagne
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20070917/a1426e28/attachment.bin 


More information about the antlr-interest mailing list