[antlr-interest] C target : default initialization to NULL

Richard Thrippleton richard.thrippleton at progress.com
Wed Apr 29 03:01:19 PDT 2009


Alexandre Hamez wrote:
>> prog : stat ;
>>
>> stat returns [ foo f ] : 'BAZ' ;
>>
>> WS : (' '|'\t'|'\n'|'\r')+
>> {$channel=HIDDEN;} ;
> 
> (foo is a C++ struct)
> the generated C code is:
> 		
>> static foo
>> stat(pgParser ctx)
>> {
>>     foo f = NULL;
>>     ...
>> }
> 
> It raises an error at compilation since foo cannot be affected a NULL  
> pointer.
> Is there a way to specify that the 'f' variable in the 'stat' rules  
> should be initialized with a specific value?
This issue has been covered before, see
http://groups.google.com.ai/group/il-antlr-interest/browse_thread/thread/7732954d2c38a918
The quick summary is that non-primitive types as return types are not 
officially supported, you'd have to return a pointer to foo.

When I encountered this issue myself, I hacked up the C code generation 
template (C.stg) in the antlr tool to simply omit this initialisation of 
return values in generated code; I cannot guarantee that this is the 
"correct" solution, but it worked for me(tm).

Alternatively, you could overload the assignment operator on your C++ 
class/struct to support having a NULL pointer 'assigned' to it. Both of 
these are, of course, grim hacks :-)

Richard


More information about the antlr-interest mailing list