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

Micheal J open.zone at virgin.net
Sat Nov 26 05:14:51 PST 2005


> 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



More information about the antlr-interest mailing list