如何在 ANTLR4 中隐藏括号? [英] How can I hide parens in ANTLR4?

查看:25
本文介绍了如何在 ANTLR4 中隐藏括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,输入 = '(1+2)*3' .

For example, input = '(1+2)*3' .

树就像这样 '(expr (expr ((expr (expr 1) + (expr 2)) ))*(expr 3))'

tree is like that '(expr (expr ((expr (expr 1) + (expr 2)) ))*(expr 3))'

然后,我想隐藏或删除树中的 '(' 和 ')' ,不再需要它们.我试图做到,但没有.

And then, I would like to hide or delete the '(' and ')' in the tree , they are no needed any more. I try to make it , but didn't.

expr : ID LPAREN exprList? RPAREN
     | '-' expr
     | '!' expr
     | expr op=('*'|'/') expr
     | expr op=('+'|'-') expr
     | ID
     | INT
     | LPAREN expr RPAREN //### Parens Here ####
     ;
 LPAREN : '(' ;
 RPAREN : ')' ;

我想要的是**不**以下内容.

What I want is** NOT** the following.

PAREN : ( '(' | ')' ) -> channel(HIDDEN)

推荐答案

标准解析器生成器方案将解析与树构建分开.

Standard parser generator schemes separate parsing from tree building.

这允许设计自定义操作来构建 AST,并将其结构微调到目标语言(包括省略诸如括号"之类的具体语法).

This allows one to design custom actions to build an AST, and fine tune its structure to the targeted langauge (including leaving out concrete syntax such as "parentheses").

代价是不仅要指定语法,还必须指定构建 AST 的规则.这使得定义语法 + 树构建器"的工作量是定义语法的两倍.当您的语法很小时,这无关紧要,但通常很小的语法意味着玩具问题".对于大型实际生产粗糙的语法,这很重要;在尝试正确使用此类语法时通常会有一堆初始流失,而在此阶段 AST 构建的东西只是妨碍了.聪明的人会延迟添加 AST 构建规则,直到流失阶段结束,但这只是部分有效,而且事实证明,您可能希望基于要构建的 AST 重塑语法,因此这种延迟实际上增加em> 有点流失.一个人还需要支付维护费用;如果你的语法有任何规模,你会改变它,然后AST构建部分也必须改变.

The price is that one must not only specify the grammar, but one must also specify the rules for building the AST. And that makes defining a "grammar + tree builder" about twice as much work as just defining a grammar. When your grammars are tiny, this doesn't matter, but usually tiny grammars means "toy problem". With big real production gnarly grammars, this matters a lot; there's usually a bunch of initial churn in trying to get such grammars right and the AST building stuff just gets in the way during this phase. Clever people delay adding AST building rules till the churn phase is over, but that only partially works, and it turns out that you may want to reshape the grammar based on AST you want to build, so this delay actually increases the churn somewhat. One also pays a maintenance cost; if your grammar has any scale, you will change it, and then the AST building part must change, too.

我的公司构建了一个工具,即 DMS 软件再工程工具包,其中包含一个解析器生成器.大约 20 年前,我们决定第一个这样做 AFAIK,这个额外的 AST 构建步骤对于我们期望(并且确实)构建的许多大型语法的好处来说太过分了.因此,我们设计了 DMS 以在解析时自动构建具体的语法树.瞧,写一个语法,得到一个解析器,树就免费了.事实证明,这个决定非常好.

My company builds a tool, the DMS Software Reengineering Toolkit, which contains a parser generator. We decided, the first to do so AFAIK, some 20 years ago, that this extra AST building step was too much work for the benefit for the many big grammars we expected to (and did) build. So we designed DMS to automatically build a concrete syntax tree as it parsed. Voila, write a grammar, get a parser and the tree is free. This decision has turned out to be a really good one.

代价是最终的树保留了所有具体的语法,例如括号.虽然它看起来可能不优雅,但事实证明,在操作树(检查、遍历、分析、修改……)时,这在实践中并不重要.为了更容易的树构建和语法维护,我们牺牲了一些不优雅.

The price is the final tree retains all the concrete syntax, e.g., the parentheses. While it may not look elegant, it turns out that this does not matter much in practice when manipulating trees (inspecting, traversing, analyzing, modifying, ...). We've traded a bit of inelegance for much easier tree building and grammar maintenance.

ANTLR 家伙(!)决定使用 ANTRL4,与他以前的 ANTLR1/2/3 系统不同,跟随我们的领导,转而从语法自动构建AST",作为具体的语法树.(我不知道您是否真的可以编写自己的 AST 构建规则来覆盖 ANTLR4 的内置功能.对此答案的评论表明,从 ANTLR4 获取 AST 的方法是走 CST 并构建您想要的. 我并不热衷于那个解决方案;它让我觉得为构建和管理 AST 付出了代价,并且还有构建 CST 的解析开销 [时间和空间].如果你只构建小树,也许你不会不在乎.对于 DMS,我们定期读取数千个文件进行处理;空间和时间很重要!)

The ANTLR guy(!) decided for ANTRL4, unlike his previous ANTLR1/2/3 systems, to follow our lead and switch to building "ASTs" automatically from the grammar, as concrete syntax trees. (I don't know if you can actually write your own AST building rules to override the built-in feature for ANTLR4. Comments on this answer suggest that the way to get an AST from ANTLR4 is to walk the CST and build what you want. I'm not keen on that solution; it strikes me as paying the price for building and managing the AST, and also having the parsing overhead [time and space] of building the CST. If you only build small trees, maybe you don't care. For DMS, we regularly read thousands of files for processing together; space and time matter!)

有关如何使其更优雅(实际上更像 AST)的一些讨论,请参阅我的 关于 AST 与. CST

For some discussion on how to make this a bit more elegant (effectively even more AST like), see my SO answer on ASTs vs. CSTs

这篇关于如何在 ANTLR4 中隐藏括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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