[antlr-interest] Bug in ANTLR 3.1.1?

Gavin Lambert antlr at mirality.co.nz
Mon Nov 10 11:14:00 PST 2008


At 05:57 9/11/2008, Robert Vaessen wrote:
>In the grammar (see the attached grammar file) the start rule 
>calls the booleanExpression rule passing it a string argument. In 
>the generated class the start() method  invokes a "delegated" 
>rule which in turn invokes a "fragment" which in turn invokes the 
>booleanExpression() method (see line 867 in the attached java 
>file) passing it the string argument which was never passed in by 
>the start() method???

It's because you've set backtrack=true, so ANTLR has inserted a 
predicate to perform the lookahead.  Predicates and rule arguments 
don't really play nicely together, especially if they get hoisted.

>start[String inCdlClassName, String inCdlVariableName] returns 
>[String code]
>@init {
>   linkDefinitions();
>   pushDefiniton(inCdlClassName);
>   mCdlVariableName = inCdlVariableName;
>   CodeBuilder builder = new CodeBuilder();
>}
>@after {
>   $code = builder.toString();
>   unlinkDefinitions();
>}
>   : booleanExpression[inCdlVariableName] { 
> builder.append($booleanExpression.code); }
>   | objectPath[builder]
>   ;

The first thing to try is to use $inCdlVariableName here, so that 
ANTLR knows you're referring to the parameter.  (You should always 
do this when referring to a symbol defined in the grammar rather 
than the code.)

If that doesn't help, then you will probably need to use scopes 
instead of rule parameters.  Or you could just use 
mCdlVariableName, since you're saving it anyway.



More information about the antlr-interest mailing list