5

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

ssingh3
  • 125
  • 1
  • 8

1 Answers1

8

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.

Bart Kiers
  • 161,100
  • 35
  • 287
  • 281
aioobe
  • 399,198
  • 105
  • 792
  • 807
  • Thanks ..that is what i was looking for – ssingh3 Aug 12 '10 at 10:26
  • 1
    I took the liberty to expand your answer a bit instead of creating an answer myself. Feel free to rephrase it as you see fit, or remove it (in which case I'll create one myself). – Bart Kiers Aug 12 '10 at 11:49