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: [email protected] [mailto:antlr-interest- > [email protected]] On Behalf Of Kieran Simpson > Sent: Saturday, March 06, 2010 5:20 AM > To: [email protected] > 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 List: http://www.antlr.org/mailman/listinfo/antlr-interest Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address -- You received this message because you are subscribed to the Google Groups "il-antlr-interest" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/il-antlr-interest?hl=en.
