[antlr-interest] enum return types for rules?

Gavin Lambert antlr at mirality.co.nz
Tue Jul 17 01:30:34 PDT 2007


At 19:06 17/07/2007, Kay Roepke wrote:
 >> size_qualifier returns [ ValueNumberValueWidth Width ]
[...]
 >>     ValueNumberValueWidth Width = 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
[...]
 >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.)

Surely it wouldn't be too hard to extend the ANTLR grammar to 
accept something like this:

   size_qualifier returns [ ValueNumberValueWidth Width = 0 ]

ie. explicitly defining a default value for the case when ANTLR 
doesn't know what to put.  This would save having to put an @init 
section in for a lot of cases, too (since it would override 
ANTLR's default even for types it did recognise).

If possible, ANTLR should even throw an error if an unrecognised 
type is used without an explicit default (for C, anyway -- in Java 
and C++ objects can be presumed to have proper default 
constructors).  Though a pointer to any kind of object should be 
treated as recognised and use NULL implicitly.  I'm not sure how 
much of that you can accomplish with just StringTemplate, though.

Ter?



More information about the antlr-interest mailing list