[antlr-interest] Inheritance with antlr 2.7.5

Karel Bernolet masanasa at gmail.com
Wed Sep 26 04:48:22 PDT 2007


Hello all,

I am writing a grammar where I would like to use inheritance. Consider the
following situation:

I have a base grammar that defines some datatypes:

class BaseGrammar extends Parser;
{
  public boolean checkCodeList(String s) {
      return true;
  }
}

datatype : t1:TOKEN t2:TOKEN  {checkCodeList(t1.getText(()+t2.getText())}?;


next to that I have this subgrammar that uses the common datatypes:

class SubGrammar extends BaseGrammar;

subgrammar: datatype;


now, when I compile the basegrammar, I get the following class:

class BaseGrammar extends antlr.LLkParser {
  public boolean checkCodeList(String s) {
    return true;
  }
  public void datatype() {
    //do the token stuff
    if (!checkCodeList(tokenString))
      throw new SemanticException("string");
  }
}

everything as expected. however, when I compile the subgrammar (with -glib
BaseGrammar.g option), I get the following class

class SubGrammar extends antlr.LLkParser {
  public void subgrammar() {
    datatype();
  }
  public void datatype() {
    //do the token stuff
    if (!checkCodeList(tokenString))
      throw new SemanticException("string");
  }
}

as you can see, in the subgrammar the 'checkCodeList' function was not
included causing compile errors.
My question is: is there any other way to give a generated parser a
superclass where I can write my common functions (to use in semantic
predicates), or is it possible to simple include the basegrammar instead of
using an option on the commandline?

did anyone else stumbled into this problem already and how did they solve
it?
My basegrammar file is 230 lines already, my first subgrammar is 450 lines.
I'll have to write at least 5 more of those subgrammars so putting
everything in 1 file is not really an option to me.

thanks in advance,
Karel

-- 
dont blame my OS for your laziness
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070926/ed74ecf3/attachment.html 


More information about the antlr-interest mailing list