[antlr-interest] Variable Expansion Code Generation Bug (3.1b)

Foust javafoust at gmail.com
Thu Aug 7 02:37:36 PDT 2008


tree grammar X;

 

statement : ^(T_STMT expr?)

                {

                                System.out.println("STMT: " + ((expr ==
null) ? "" : $ expr.text));

                }

 

Generates the code:

System.out.println("STMT: " + ((expr == null) ? "" :
(expr1!=null?(input.getTokenStream().toString(

                input.getTreeAdaptor().getTokenStartIndex(expr1.start),

 
input.getTreeAdaptor().getTokenStopIndex(expr1.start))):null)));

 

a)      Which doesn't compile, since the rule "expr" was not converted to
"expr1". 

 

b)      If "$expr" is used, instead, ANTLR won't compile the grammar,
complaining that a attribute must be referenced.

 

 

This similar code does, in fact, compile:

 

statement : ^(T_STMT e=expr?)

                {

                                System.out.println("STMT: " + ((e == null) ?
"" : $ e.text));

                }

 

And generates the following correct Java code:

System.out.println("STMT: " + ((e  == null) ? "" :
(e!=null?(input.getTokenStream().toString(

                input.getTreeAdaptor().getTokenStartIndex(e.start),

                input.getTreeAdaptor().getTokenStopIndex(e.start))):null)));

 

Brent

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080807/325e971e/attachment-0001.html 


More information about the antlr-interest mailing list