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

查看:87
本文介绍了如何在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重塑语法,因此,这种延迟实际上会增加

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 Software Reengineering Toolkit,其中包含一个解析器生成器.大约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.

与以前的ANTLR1/2/3系统不同,ANTLR的家伙(!)决定采用ANTRL4,跟随我们的领导,切换到从语法自动构建"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与vs的 SO答案.CSTs

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天全站免登陆