^和!是什么?在ANTLR语法中代表(What does ^ and ! stand for in ANTLR grammar)

45 IT屋

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

解决方案

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.

我很难弄清楚^和!是什么。


解决方案

看看 ANTLR速查表




!不包含在AST

^ 设置AST根节点




和 ^ 也可以用于重写规则:。 --> ^(...)。例如,以下两个解析器规则是等效的:



  expression 
:A'+'^ A';' !
;


和:



 < code>表达式
:A'+'A';'-> ^(’+’A A)
;


两者均创建以下AST:



  + 
/ \
AA


换句话说: + 是根目录,两个 A 是它的子级,而; 从树中省略。


本文地址:IT屋 » ^和!是什么?在ANTLR语法中代表