ssingh3
ssingh3

Reputation: 125

What does ^ and ! stand for in ANTLR grammar

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

Upvotes: 5

Views: 1101

Answers (1)

aioobe
aioobe

Reputation: 421180

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.

Upvotes: 8

Related Questions