Download ANTLR Reference Manual
Transcript
importVocab: Initial Grammar Vocabulary
[See
the documentation on vocabularies for more information]
To specify an initial vocabulary (tokens, literals, and token types), use the importVocab
grammar option.
class MyParser extends Parser;
options {
importVocab=V;
}
ANTLR will look for VTokenTypes.txt in the current directory and preload the token manager for
MyParser with the enclosed information.
This option is useful, for example, if you create an external lexer and want to connect it to an
ANTLR parser. Conversely, you may create an external parser and wish to use the token set
with an ANTLR lexer. Finally, you may find it more convenient to place your grammars in
separate files, especially if you have multiple tree-walkers that do not add any literals to the
token set.
The vocabulary file has an identifier on the first line that names the token vocabulary that is
followed by lines of the form ID=value or "literal"=value. For example:
ANTLR // vocabulary name
"header"=3
ACTION=4
COLON=5
SEMI=6
...
A file of this form is automatically generated by ANTLR for each grammar.
Note: you must take care to run ANTLR on the vocabulay-generating grammar files before you
run ANTLR on the vocabulary-consuming grammar files.
exportVocab: Naming Export Vocabulary
[See
the documentation on vocabularies for more information]
The vocabulary of a grammar is the union of the set of tokens provided by an importVocab
option and the set of tokens and literals defined in the grammar. ANTLR exports a vocabulary
for each grammar whose default name is the same as the grammar. So, the following grammar
yields a vocabulary called P:
class P extends Parser;
a : A;
ANTLR generates files PTokenTypes.txt and PTokenTypes.java.
You can specify the name of the exported vocabulary with the exportVocab option. The
following grammar generates a vocabulary called V not P.
class P extends Parser;
options {
exportVocab=V;
139