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

查看:85
本文介绍了插入符号前缀而不是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 : A B^ C;的AST,请使用^( ... )开始重写规则,其中括号内的第一个标记/规则将成为根节点.所以规则:

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 : A B^ C;相同的AST.

  • Tree construction
  • How to output the AST built using ANTLR?

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

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