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

Edson Tirelli ed.tirelli at gmail.com
Tue Feb 22 12:48:19 PST 2011


   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


More information about the antlr-interest mailing list