What does ^ and ! stand for in ANTLR grammar

2019-04-29 09:20发布

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

1条回答
倾城 Initia
2楼-- · 2019-04-29 10:00

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.

查看更多
登录 后发表回答