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

Kieran Simpson kierans777 at gmail.com
Sat Mar 6 05:20:05 PST 2010


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.




More information about the antlr-interest mailing list