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

Ken and Timi Cecka ceckak at alumni.washington.edu
Fri Nov 25 22:37:47 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?  If not, would this be hard to include in v3.0?  I
can work around it by moving my utility functions into the header block or
a separate library, but it seems like the natural thing would be for these
code blocks to carry across in the inheritance chain.

A contrived example is included below.

Ken




---begin super.g---

header
{
  #define LIFE_VAL 6
  #define UNIVERSE_VAL 7
}

options
{
  language="Cpp";
}

{
  static int ask(void)
  {
    return LIFE_VAL * UNIVERSE_VAL;
  }
}

class Super extends TreeParser;

question returns [int i] 
  : #(QUESTION LIFE UNIVERSE)
    { i = ask(); }
  ;

---end super.g---

---begin sub.g---

header
{
#ifdef WORKAROUND2 //would prefer this was inherited
  #include "Super.hpp"
#endif
}

options
{
  language="Cpp";  //would prefer this was inherited
}

{
#ifdef WORKAROUND1 //would prefer this was inherited
  static int ask(void)
  {
    return LIFE_VAL * UNIVERSE_VAL;
  }
#endif
}

class Sub extends Super;

answer returns [int i]
  : i=question
  ;

---end sub.g---

---begin build log---
$ g++ -c -DWORKAROUND1 -DWORKAROUND2 Sub.cpp
$ antlr super.g
ANTLR Parser Generator   Version 2.7.5 (20050128)   1989-2005 jGuru.com
$ antlr -glib super.g sub.g
ANTLR Parser Generator   Version 2.7.5 (20050128)   1989-2005 jGuru.com
$ g++ -c Super.cpp
$ g++ -c Sub.cpp
expandedsub.g: In member function `int Sub::question(antlr::RefAST)':
expandedsub.g:29: error: `ask' undeclared (first use this function)
expandedsub.g:29: error: (Each undeclared identifier is reported only once
for each function it appears in.)
$ g++ -c -DWORKAROUND1 Sub.cpp
expandedsub.g: In function `int ask()':
expandedsub.g:15: error: `LIFE_VAL' undeclared (first use this function)
expandedsub.g:15: error: (Each undeclared identifier is reported only once
for each function it appears in.)
expandedsub.g:15: error: `UNIVERSE_VAL' undeclared (first use this function)
$ g++ -c -DWORKAROUND1 -DWORKAROUND2 Sub.cpp
---end build log---



More information about the antlr-interest mailing list