[antlr-interest] missing attribute access on rule scope when testing token in action section

Andres Bernasconi andres.bernasconi at gmail.com
Fri Dec 21 04:54:26 PST 2007


I would like to provide an example for this problem (working with ANTLRWorks
1.1.5):

This grammar "compiles" OK:
grammar testGrammar;

error_test
    :    someval=(other_rule)
    {
        if($someval != null ){
            System.out.println($someval.text);
        }
    };


other_rule
    :    'A' ('A' | 'B')+;


but this one, does NOT:
grammar testGrammar;

error_test
    :    someval=other_rule
    {
        if($someval != null ){
            System.out.println($someval.text);
        }
    };


other_rule
    :    'A' ('A' | 'B')+;


Another problem is the Java code generated by ANTLR (in the first case):
        try {
            //
D:\\data\\dev\\tools\\antlrWorks\\grammars\\testGrammar.g:4:2: (someval= (
other_rule ) )
            //
D:\\data\\dev\\tools\\antlrWorks\\grammars\\testGrammar.g:4:4: someval= (
other_rule )
            {
            //
D:\\data\\dev\\tools\\antlrWorks\\grammars\\testGrammar.g:4:12: ( other_rule
)
            //
D:\\data\\dev\\tools\\antlrWorks\\grammars\\testGrammar.g:4:13: other_rule
            {
            pushFollow(FOLLOW_other_rule_in_error_test14);
            other_rule();
            _fsp--;


            }


                    if(someval != null ){
                        System.out.println(someval.getText());
                    }


            }

you can notice that the "someval" variable is never assigned a value.

Now if I change the second example (the non-working one) and avoid the $ in
the if statement
if( someval != null )

The code compiles and it generates the following valid Java Code:
...
pushFollow(FOLLOW_other_rule_in_error_test13);
someval=other_rule();
_fsp--;
...

where 'someval' is being assigned a value.

Regards
Andres B.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20071221/9f3ae5ba/attachment-0001.html 


More information about the antlr-interest mailing list