[antlr-interest] Uninitialised global scope struct instance in C code

Kieran Simpson kierans777 at gmail.com
Sun Mar 7 17:40:38 PST 2010


I see where I went wrong.  I needed to include the scope in the rule  eg:

ruleA returns [int result]
scope ParserGlobals;
@init  {
  $ParserGlobals::x = 0
}
  : ruleB { $result = $ParserGlobals::x; }
 ;

Thanks for the pointer.

Jim Idle wrote:
> If the scope is null it means (as per the comments and antlr.markmail.org), that you hoave a path in to your rule that does not go through the rule where the scope lives. Put a breakpoint in the if condition and it will tell you which path it is. 
>
> Jim
>
>   
>> -----Original Message-----
>> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
>> bounces at antlr.org] On Behalf Of Kieran Simpson
>> Sent: Saturday, March 06, 2010 5:20 AM
>> To: antlr-interest at antlr.org
>> Subject: [antlr-interest] Uninitialised global scope struct instance in
>> C code
>>
>> I am using  Antlr Version 3.2 and C libantlr3c-3.2
>>
>> I have a grammar "Foo".  In the grammar I have a global scope declared
>>
>> scope ParserGlobals {
>>   int x;
>> }
>>
>> and a rule that initialises it before descending further into the parse
>> tree
>>
>> ruleA returns [int result]
>> @init  {
>>   $ParserGlobals::x = 0
>> }
>>   : ruleB { $result = $ParserGlobals::x; }
>>  ;
>>
>> The code that is generated by the C target is comprised (with other
>> code
>> omitted for brevity) of the following
>>
>> static String ruleA(pFooParser ctx)  {
>>   (SCOPE_TOP(ParserGlobals))->x= 0;
>> }
>>
>> in it's post processed form (by gcc -e)
>>
>> static String ruleA(pFooParser ctx)  {
>>   (ctx->pFooParser_ParserGlobalsTop)->x= 0;
>> }
>>
>> However for some reason ctx->pFooParser_ParserGlobalsTop is null.  I
>> changed the generated code to the following
>>
>> if ((SCOPE_TOP(ParserGlobals)) == NULL)  {
>>   printf("ParserGlobals stack is not initialised");
>> }
>>
>> And the 'if' is evaluated to true.
>>
>> In the "constructor" for the parser, I noticed
>>
>> ctx->pFooParser_ParserGlobalsTop      = NULL;
>>
>> However as far as I can see there is no code in the generated source
>> that changes the value of pFooParser_ParserGlobalsTop before my @init
>> code accesses it (thus causing a seg fault).
>>
>> I noticed that someone else had a similar problem with local scopes
>> (http://www.antlr.org/pipermail/antlr-interest/2008-April/027524.html)
>> However I'm not sure why a global scope in a grammar is generating code
>> that is seg faulting due to the scope not being initialised.
>>
>> Any ideas/assistance would be appreciated.
>>
>>
>>
>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
>> email-address
>>     
>
>
>
>   


More information about the antlr-interest mailing list