I was having difficulty figuring out what does ^ and ! stand for in ANTLR grammar terminology.
ANTLR (ANother Tool for Language Recognition) is a tool for processing structured text. It does this by giving us access to language processing primitives like lexers, grammars, and parsers as well as the runtime to process text against them. It's often used to build tools and frameworks.
You should include an explicit EOF at the end of your entry rule any time you are trying to parse an entire input file. If you do not include the EOF , it means you are not trying to parse the entire input, and it's acceptable to parse only a portion of the input if it means avoiding a syntax error.
The G4 file format is designed to keep the grammer for ANTLR which stands for ANother Tool for Language Recognition, is a parser generator. ANTLR takes as input a G4 file which containing a grammar that specifies a language and generates as output source code for a recognizer of that language.
A lexer is recognizer that draws input symbols from a character stream. lexer grammars result in a subclass of this object. A Lexer object uses simplified match() and error recovery mechanisms in the interest of speed.
Have a look at the ANTLR Cheat Sheet:
!
don't include in AST^
make AST root node
And ^
can also be used in rewrite rules: ... -> ^( ... )
. For example, the following two parser rules are equivalent:
expression
: A '+'^ A ';'!
;
and:
expression
: A '+' A ';' -> ^('+' A A)
;
Both create the following AST:
+
/ \
A A
In other words: the +
is made as root, the two A
's its children, and the ;
is omitted from the tree.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With