[antlr-interest] Rewrite rules accessing subrule's results?

Terence Parr parrt at cs.usfca.edu
Tue Feb 22 13:06:54 PST 2011


Yo bro!  try {$andRule::se1} in the rewrite itself instead of just $se1
Ter
On Feb 22, 2011, at 12:48 PM, Edson Tirelli wrote:

>   Hi all,
> 
>   Imagine I have an expression grammar that has to deal with the following
> expression:
> 
> age > 10 && < 20
> 
>   When finding the above input, the grammar has to rewrite that into:
> 
> age > 10 && age < 20
> 
>   My expression grammar is a typical expression grammar otherwise, but for
> simplification lets imagine it is like this:
> 
> relationalExpression : operand (operator^ operand)*
> 
> operand : // will match the "age" token above;
> 
> operator: // will match operators, like the > and < above;
> 
>   And then I have my "and" rule where I need to use the rewrite:
> 
> andRule : relationalExpression ( '&&' operator operand )*;
> 
>   I tried the following:
> 
> andRule :
> scope { CommonTree se1 = null; }
>    ( relationalExpression -> relationalExpression )
>    ( ( '&&' op=operator se2=operand ) -> ^('&&' $andRule ^( $op $se1 $se2 )
> )*;
> 
>   And then setting se1 in the relational expression, but the rewrite rule
> does not allow me to reference $se1. I am probably over-thinking this, but
> any help would be greatly appreciated. BTW, the "operand" is actually
> several calls down in the real grammar... above is just a simplified
> snippet, but the problem is the same.
> 
>   Thanks,
>     Edson
> 
> 
> -- 
>  Edson Tirelli
>  JBoss Drools Core Development
>  JBoss by Red Hat @ www.jboss.com
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address



More information about the antlr-interest mailing list