Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ^ and ! stand for in ANTLR grammar

I was having difficulty figuring out what does ^ and ! stand for in ANTLR grammar terminology.

like image 583
ssingh3 Avatar asked Aug 12 '10 09:08

ssingh3


People also ask

What is an ANTLR grammar?

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.

Why should a start rule end with EOF in an ANTLR grammar?

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.

What is a G4 file ANTLR?

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.

What is ANTLR lexer?

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.


1 Answers

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.

like image 58
2 revs, 2 users 77% Avatar answered Nov 15 '22 08:11

2 revs, 2 users 77%