[antlr-interest] enum return types for rules?

Kay Roepke kroepke at classdump.org
Tue Jul 17 00:06:44 PDT 2007


On Jul 16, 2007, at 2:07 PM, Cameron Esfahani wrote:

> size_qualifier returns [ ValueNumberValueWidth Width ]
> 	:	T_SIZE_8
> 		{
> 			$Width = kValueNumberValue8Bit;
> 		}
> 	;
>
> And ValueNumberValueWidth is defined as:
>
> typedef enum {
> 	kValueNumberValue8Bit		= 0x300,
> 	kValueNumberValue16Bit,
> 	kValueNumberValue32Bit,
> 	kValueNumberValue64Bit,
> } ValueNumberValueWidth;
>
> When I try and build my tree grammar, I get the following error  
> from gcc:
>
> TWalker.c: In function 'size_qualifier':
> TWalker.c:1132: error: incompatible types in initialization
>
> Because antlr has generated the following line:
>
>     ValueNumberValueWidth Width = NULL;
> 
[...]
>     unsigned long long Number = NULL;

The problem is that the StringTemplate for a target has a type map  
for initialization values.
The C target has these:
cTypeInitMap ::= [
	"int"		    : "0",              // Integers     start out being 0
	"long"		    : "0",              // Longs        start out being 0
	"float"		    : "0.0",            // Floats       start out being 0
	"double"	    : "0.0",            // Doubles      start out being 0
	"ANTLR3_BOOLEAN"    : "ANTLR3_FALSE",   // Booleans     start out  
being Antlr C for false
	"byte"		    : "0",              // Bytes        start out being 0
	"short"		    : "0",              // Shorts       start out being 0
	"char"		    : "0",              // Chars        start out being 0
	default		    : "NULL"            // Anything other than an atomic  
type (above) is a NULL (probably NULL pointer).
]

As you can see, anything that's not explicitely listed gets  
initialized with NULL. This affects other targets likewise.
I guess the only chance is to add your enum and possibly unsigned  
long long to that map. It sucks, but I think it cannot be done in the  
grammar directly (though I remember talk of something like overriding  
templates in the grammar. That might just be my blurry memory - and  
this is definitely more than a year ago.)

That stuff is located in the antlr.jar, though if you copy the C.stg  
file to somewhere else, and put it first into the classpath, ANTLR  
will pick up that copy. There you could do your changes.

HTH,
-k
-- 
Kay Röpke
http://classdump.org/






More information about the antlr-interest mailing list