[antlr-interest] Composite grammar issues
Bill Andersen
bill.andersen at mac.com
Sun Jan 9 20:30:47 PST 2011
Folks
I have played quite a bit with the ANTLR documentation wrt composite grammars and there seem to be some holes that are causing me some problems getting grammar import going. Let describe the simple setup first. Code follows description.
1) I have two grammars A and B. B is meant to be a proper super-set of A.
2) Seems a natural way to implement this in ANTLR would be to have the B grammar import and extend the A grammar.
3) I have a lexer for A. The A parser has option set to use tokenVocab from the A lexer
See below code for description of problems.
========== Code ===============
///
/// A Lexer
///
lexer grammar ALexer;
@header {
package test;
}
ID
: ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
;
LP
: '('
;
RP
: ')'
;
WS
: ( ' ' | '\t' | '\r' | '\n' ) {$channel=HIDDEN;}
;
///
/// A Parser
///
parser grammar AParser;
options {
language = Java;
tokenVocab = ALexer;
}
@header {
package test;
}
a
: form EOF
;
form
: LP ID RP
;
///
/// B Parser
///
parser grammar BParser;
import AParser;
@header {
package test;
}
b
: form form EOF
;
========== Problems ===============
[1] Compiling BParser produces the error message "redefinition of header action". Without this, my generated code ends up in the wrong package.
[2] I am accustomed in writing simple ANTLR combined grammars to specifying some of the tokens in the 'tokens' section and some in the lexer rule section. With the above setup (sans the definitions of LP and RP in ALexer) and including
tokens {
LP = '(';
RP = ')';
}
in AParser yields the error "literal has no associated lexer rule".
[3] In BParser, the line
import AParser;
is underlined in ANTLRWork with the message "Undefined import "AParser""
========== Questions ===============
I would like the BParser to have access to an extended stock of tokens over those supplied by ALexer. Is the right thing to do write a BLexer that imports ALexer, have AParser use ALexer's tokenVocab and BParser import AParser and use BLexer's tokenVocab?
That's about all for now. Any help appreciated.
More information about the antlr-interest
mailing list