在 antlr 中插入符号前缀而不是后缀 [英] caret prefix instead of postfix in antlr

查看:26
本文介绍了在 antlr 中插入符号前缀而不是后缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在 antlr 中插入符号后缀是什么意思(即生成 root)但是当插入符号是前缀时呢,就像我一直在阅读的以下语法(这个语法是全新的,由一个学习 antlr 的新团队完成))....

I know what the caret postfix means in antlr(ie. make root) but what about when the caret is the prefix as in the following grammar I have been reading(this grammar is brand new and done by a new team learning antlr)....

selectClause
    : SELECT resultList -> ^(SELECT_CLAUSE resultList) 
    ;


fromClause
    : FROM tableList -> ^(FROM_CLAUSE tableList) 
    ;

另外,我知道 => 是什么意思,但是 -> 呢?-> 是什么意思?

Also, I know what => means but what about the -> ? What does -> imply?

谢谢,院长

推荐答案

^ 用作内联树运算符,表示某个标记应该成为树的根.

The ^ is used as an inline tree operator, indicating a certain token should become the root of the tree.

例如规则:

p : A B^ C;

创建以下 AST:

  B
 / \
A   C

还有另一种使用重写规则来创建 AST 的方法.重写规则位于解析器规则的替代之后(或右侧).你用一个箭头"开始重写规则->,然后是你想要在 AST 中的规则/令牌.

There's another way to create an AST which is using a rewrite rule. A rewrite rule is placed after (or at the right of) an alternative of a parser rule. You start a rewrite rule with an "arrow", ->, followed by the rules/tokens you want to be in the AST.

采用之前的规则:

p : A B C;

并且您想反转令牌,但保持 ASST平坦"(无根节点).这可以使用以下重写规则来完成:

and you want to reverse the tokens, but keep the ASST "flat" (no root node). THis can be done using the following rewrite rule:

p : A B C -> C B A;

如果你想创建一个类似于 p : AB^ C; 的 AST,你用 ^( ... )<开始你的重写规则/code> 其中括号内的第一个标记/规则将成为根节点.所以规则:

And if you want to create an AST similar to p : A B^ C;, you start your rewrite rule with ^( ... ) where the first token/rule inside the parenthesis will become the root node. So the rule:

p : A B C -> ^(B A C);

产生与 p 相同的 AST : A B^ C;.

这篇关于在 antlr 中插入符号前缀而不是后缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆