[antlr-interest] RE: header and extra code blocks in supergrammars

Ken and Timi Cecka ceckak at alumni.washington.edu
Sat Nov 26 09:43:00 PST 2005


Micheal J wrote:

>> Hi All,
>> 
>> I'm experimenting with splitting my TreeParser into a
>> supergrammar and several distinct subgrammars.  One annoyance
>> I'm running in to is that the subgrammars don't appear to
>> inherit the header block or the extra code block.  That means
>> I have to replicate those sections in each subgrammar, which
>> is a bit of a maintenance headache.
>> 
>> For the header section, I can get away with having the
>> subgrammar's include the supergrammar's header, but this
>> trick won't work for the extra code block because it gets
>> inserted into the cpp file.
>> 
>> Am I missing something?
> 
> Perhaps not. I've usually tackled this by instructing ANTLR to generate
> tree parsers that derive from my own custom class (itself a
> TreeParser-derivative). This takes care of the common code in the extra
> code block since I can include all that in my custom base class.
> 
> ANTLR v2's "inheritance" feature is then used to ensure the right
> rules/actions are generated for individual tree parsers.
> 
> As you say, the header stuff has to be repeated in each grammar but that's
> not an issue really.
> 
> Micheal

Thanks.  For now I've moved the functions from the global extra code block
down into the class's extra code block.  That inserts the functions as
inlines in the class definition, which antlr *does* propagate to derived
classes.  The only potential downside is that they're inline functions, but
your approach would solve that problem.

Ken



More information about the antlr-interest mailing list