[antlr-interest] ANTLR 3.0b4 code generation question

John B. Brodie jbb at acm.org
Fri Oct 27 19:13:30 PDT 2006


Greetings!

>Thanks for the wonderful hints so far.  I've noticed some unusual
>java code generated under ANTLR3, and I'm wondering if it is a
>cockpit error or if I might be tickling an ANTLR3 bug.
>
>The MIME attached smalltest.g using my recently downloaded version of
>ANTLRworks creates the following line in the parser:
>
>       void c = null;
>
>Is this correct Java, or did I do something wrong to cause this?

You have done something wrong which ANTLR v3 (in my opinion) should have
complained about.

Your grammar is (essentially this):

>grammar SmallTest;
>
... at members snipped, not relevant to this issue
>
>startRule : c=command NEWLINE | NEWLINE ;
>
>command : a=A ;
>
>NEWLINE	: ('\r' | '\n' | '\r\n');	
>A	: 'a';

and now we must ask what is the type of variable c in startRule to be?

there is no

options { output = AST; }

so variable c can not be an AST.

and variable c is not declared in an @init{} section, never mind that rule
command has no returns clause

command returns [SomeClass foo] : ... ; // not present!



so it is a quandary as to exactly what your desires for the variable c are.



I think ANTLR should have scolded you for not specifying a type for c ;=)



You need to decide (and tell ANTLR) what the type of the variable c is.

Hope This Helps
   -jbb


More information about the antlr-interest mailing list