[antlr-interest] Error in generated C code (struct referenced without being initialized)

Jim Idle jimi at temporal-wave.com
Tue Feb 3 09:18:30 PST 2009


Andy Grove wrote:
> Hi Jim,
>
> I have further info on this now. Here's the relevant portion of 
> grammar. The "a1" is optional but is always referenced in the action. 
> This works fine when generating Java code from the grammar (the action 
> gets passed a NULL parameter for "a1"). The generated C code has a 
> NULL check for a1.start just like the generated Java code does, but 
> because a1.start is never initialized in C it blows up.
>
> | e = expression
>             ( a1 = alias1  )?
>         { sse.addSelectItem($e.text, $a1.text); }
>
> Would it be possible to modify the generator to initialize these 
> variables to sensible defaults e.g. NULL for compatibility with the 
> Java generator?
>
> Alternatively, how could I modify the grammar to check if a1 is NULL 
> or not?

Yes, well we could argue that this isn't quite Kosher use, but I was 
pretty sure that the value should be initialized. The thing is that this 
is a structure return, so the structure is allocated but I don't really 
know about the elements within the structure, that is just part of the 
codegen. However this should work better:

| e = expression
            ( a1 = alias1
                   { sse.addSelectItem($e.text, $a1.text); }
             |     { sse.addSelectItem($e.text, NULL); }
            )
...


Jim



More information about the antlr-interest mailing list