At 06:18 9/08/2009, Tomasz Jastrzebski wrote:
>Now I understand that the only reasonable such declaration must 
>look like:
>
>multExpr
>  : primaryExpr (('*' | '/') primaryExpr)*
>  ;

Actually, you *could* use this:
   multExpr
     : primaryExpr (('*' | '/') multExpr)?
     ;

The recursion would even make it easier to apply the rewrites as 
you want.  The downside of this is that it will only work properly 
for left-associative operators (unlike the original, which can be 
done either way).  Using the * is generally preferred practice 
though; avoids using up stack space :)

>After adding rewrite rules this may look like that:
>addExpr
>  : (multExpr -> multExpr) ( (o='+' | o='-') e=multExpr -> ^($o 
> $addExpr $e) )*
>  ;
>multExpr
>  : (primaryExpr -> primaryExpr) ( (o='*' | o='/') e=primaryExpr 
> -> ^($o $multExpr $e) )*
>  ;

Yes, this is a good way to do it.

>I would think that now I can simply replace:
>-> ^($o $addExpr $e)
>with
>-> ^(BIN_EXPR<BinaryExpression>[o.text, $addExpr.tree, $e.tree]>)
>
>- but not. $addExpr.tree in the second version is always NIL.

Have you tried just using $addExpr instead of $addExpr.tree?  (Not 
sure if that's valid, but it's worth trying.)  Also, your o.text 
should be $o.text; although you're going to lose some context in 
your tree by not keeping the full token objects.


List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"il-antlr-interest" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to