[antlr-interest] Help newbie understand combined grammar import restrictions

Mike J. Bell ckimyt at gmail.com
Tue Mar 17 11:22:30 PDT 2009


I'm trying to model a DSL we have at work in ANTLR.  It turns out that this
language has three distinct flavors of expression, and each could best be
represented as its own language and then tied together with some knowledge
about the context of when each occurs.

All three share certain concepts though...there are lexer rules and parser
rules that I do not want to reinvent for each "sublanguage."  I thought I
could use "import ..." to make a "base" language and then build upon it for
each sublanguage.

Here's a toy example:

grammar Base;
WS: ' ' | '\t' { $channel = HIDDEN; };
fragment ALPHA: 'A'..'Z'|'a'..'z';
fragment DIGIT: '0'..'9';
IDENT: ALPHA (ALPHA|DIGIT)*;
NUMBER: DIGIT+;
rvalue: IDENT|NUMBER;
lvalue: IDENT;

Then one of my sublanguages can do:

grammar SubLanguage1;
import Base;
ASSIGN_OP: '=';
fragment HEXDIGIT: DIGIT|'A'..'F'|'a'..'f';
HEXNUM: '0x' HEXDIGIT+;
my_rvalue: rvalue | HEXNUM;
assignment: lvalue ASSIGN_OP my_rvalue;
label: IDENT ':';

And finally the master language:

grammar Master;
import SubLanguage1, SubLanguage2, SubLanguage3;
//rules to invoke rules of subs...

It's just an example, but I think you can see where I'm going.  I want to
put shared lexer tokens and parser rules in the Base grammar, and then
extend and add to it in each of the Sublanguage* grammars.  This way the
common language features can stay in one place and if small tweaks need to
happen they happen in one place.

As you can see, I have rules in the base that depend on tokens in the base,
and those tokens depend on fragments in the base as well.  In the sub, I
have rules that depend on rules and tokens in the base, and tokens that
depend on tokens and fragments in the base, and fragments that depend on
fragments in the base.

I tried splitting these into separate lexers and parsers, but ANTLRWorks
shows all kinds of red when I don't import things that are illegal to
import.  Whether I do or not, I get compile errors trying to debug the
grammar.

So I guess I'm asking two questions:  why can't combined grammars import
other combined grammars, and does anyone have any suggestions for how to
best handle this situation?

Thanks a lot in advance!
Mike

-- 
Mike J. Bell on gmail
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090317/47847c19/attachment.html 


More information about the antlr-interest mailing list