Grammar in the context of Antlr.
The grammar definition of Antlr is called a Lexicon because the grammar is used by the lexer (hence the lexer grammar)
See:
The file name containing grammar X must be called X.g4
The name of the file containing the grammar must match the grammar-name and have a .g4 extension.
The name of the grammar must be declared at the top of the file.
The syntax can be specified in a file with syntax:
Code Source Structure. See antlr/antlr4/blob/master/doc/grammars.md
/** Optional javadoc style comment */
grammar_type? grammar Name;
options {...}
import ... ;
tokens {...}
channels {...} // Only lexer grammars can contain custom channels specifications
@actionName {...}
rule1 // parser and lexer rules, possibly intermingled
...
ruleN
where:
Token definition needed by a grammar for which there is no associated lexical rule. The tokens section defines a set of tokens to add to the overall set.
There is two kinds of rule:
Name Case | Type | Description | Example from the getting started |
---|---|---|---|
uppercase letter | lexer rule | (known as Token name, they defines the token that the lexer will produce | ID : [a-z]+ ; defines an ID token that is made of letters from a to z |
lowercase letter | parser rule | They defines how the relation between the token and therefore how the parser tree is build | r : 'hello' ID ; defines a pattern with the world hello and the token ID defines just above |
Only lexer grammars can contain custom channels specifications
channels {
WHITESPACE_CHANNEL,
COMMENTS_CHANNEL
}
Actions are code blocks written in the target language.
Further, do not use: