Lessons Learned: Don't use import in a parser grammar, don't use tokenVocab in a lexer grammar

494 views
Skip to first unread message

Roger Costello

unread,
Mar 6, 2015, 12:30:03 PM3/6/15
to antlr-di...@googlegroups.com
Hi Folks,

I have been experimenting with import and tokenVocab. Here's what I have discovered:

1. A lexer grammar can reuse another lexer grammar using the import statement.

2. A lexer grammar cannot reuse another lexer grammar using the tokenVocab option.

3. A parser grammar cannot reuse a lexer grammar using the import statement.

4. A parser grammar can reuse a lexer grammar using the tokenVocab option.

5. The prefix of the filename for the parser and the lexer(s) must be the same. I have adopted this convention for naming parsers: AParser.g4. I name all lexers this way: ALexer.g4, ALexer2.g4, ALexer3.g4, etc.

Here are my conclusions:

a. To reuse a lexer grammar, a parser grammar must use this: options { tokenVocab=ALexer;  }

b. To reuse another lexer grammar, a lexer grammar must use this: import ALexer2;

c. The filename prefix of all lexers and parser must be identical.

If I err in any of my statements, please let me know.

/Roger

Loring Craymer

unread,
Mar 7, 2015, 12:18:56 AM3/7/15
to antlr-di...@googlegroups.com
Actually, "tokenVocab" reserves/assigns token values (so, for example, a parser can recognize the token values output by a lexer) via a text file (output when generating a lexer, read while generating a parser), while "import" reuses a grammar (lexer must import lexer, parser must import parser).

--Loring
Reply all
Reply to author
Forward
0 new messages