[antlr-interest] Variable visibility problem with ANTLR 3 code generation

Denis Washington dwashington at gmx.net
Tue Aug 15 07:32:00 PDT 2006


Hi,
My ANTLR 3 grammar contains following rule:

multiplicativeExpression returns [Expression expr]
    scope { ArithmeticOperator op; }
    :   e=unaryExpression { expr = e; }
        (            
            (   '*' { $multiplicativeExpression::op =
                            ArithmeticOperator.MULTIPLY; }
            |   '/' { $multiplicativeExpression::op =
                            ArithmeticOperator.DIVIDE; }
            )
            
            e2=multiplicativeExpression
            {
                expr = new ArithmeticExpression(expr, e2,
                        $multiplicativeExpression::op);
                expr.setSource(e);
            }
        )*
    ;

If I generate code for it and try to compile that code, I get errors 
about non-existing variables e and e2 in the part in parenthes. A look 
in the code reveals why: e and e2 get automatically declared at the 
beginning of multiplicativeExpression() as it should be, but during 
decision, another methods with the name "synpred*number*()" are called 
with code for the things in the parenthes (e2=multiplicativeExpression 
and so forth), which is naturally errorous because this method doesn't 
have access to the local variables of multiplicativeExpression(). Is 
this a bug or "a feature"? I need help! (using v3.0b3)


More information about the antlr-interest mailing list