[antlr-interest] Variable Expansion Code Generation Bug (3.1b)
Gavin Lambert
antlr at mirality.co.nz
Thu Aug 7 05:47:26 PDT 2008
At 21:37 7/08/2008, Foust wrote:
>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".
Because you didn't use a $ :)
>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)));
If you take a closer look at both sets of generated code, you'll
see that $expr.text and $e.text are already being null protected
anyway, so your extra null protection is (almost) unnecessary:
statement : ^(T_STMT expr?)
{
String text = $expr.text;
System.out.println("STMT: " + ((text == null) ? "" :
text));
}
(You could put this all on one line and ditch the extra variable
if you don't mind it printing a null instead of an empty string.)
More information about the antlr-interest
mailing list