[antlr-interest] Grammar composition in v3, multiple packages, no joy

Christian Nentwich christian at modeltwozero.com
Tue Feb 9 15:22:56 PST 2010


Hi all,

I'm a long time user of ANTLR, and have recently gone through the painful
but definitely useful exercise of moving from v2 to v3.

I have now hit a point where I am stuck, with no workaround in sight, due to
the difference in grammar inheritance. This is the situation here - note, no
lexers involved, only parsers:

Language L1    -    lives in a Java project, with grammar L1.g; usable
standalone
Language L2    -    lives in another Java project, with grammar L2.g;
extends L1

With ANTLR 2, L2 extended L1; that created expanded.g in the background, and
everything worked fine. With ANTLR 3, I get no joy. Following attempts made:

a) Use import mechanism; L2 imports L1. Does not work, because of this bug
http://www.antlr.org/jira/browse/ANTLR-375. Because L2 is in another Java
project, different package, etc, both grammars need a header, and ANTLR
throws that out with an error. It would only work if the root grammar header
was an override for anything imported, which it doesn't seem to be.

b) Don't use import mechanism. Instead, duplicate L1 grammar into L2 by
hand, simulating ANTLR 2. Turns out, I can't even do that: the tokens are
not stable and get different numbers assigned. If you wonder why that's a
problem: the code in the first package has switch statements based on some
tokens; where such code is reused in package 2, the switch statements will
switch on the wrong numbers.

So - it seems I'm stuck with no feature to handle this, and no workaround.
Can anybody please tell me I'm doing something stupid? :) Is the composition
feature one that's not widely used, other than for simple lexer/parser
splits?

thanks,
Christian

p.s. to pay my debts I wanted to post on confluence how to get case
insensitive parsers using ANTLR3 with JFlex, but it seems editing/commenting
is locked down. Any reason why?


More information about the antlr-interest mailing list