生成ANTLR树语法的系统方法? [英] Systematic way to generate ANTLR tree grammar?

查看:25
本文介绍了生成ANTLR树语法的系统方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有点大的 ANTLR 解析器语法文件,想为它制作一个树语法.但是,据我所知,树语法生成这个工作不能自动完成,即我应该通过复制解析器语法,删除一些不必要的代码等手动生成它. 我想知道是否有系统的方法从解析器语法文件生成树语法文件.

I have a little bit large ANTLR parser grammar file and want to make a tree grammar for it. But, as far as I know this work of tree grammar generation can't be done automatically, i.e., I should generate it manually by copying parser grammar, removing some unnecessary code, etc. I want to know if there is a systematic way to generate a tree grammar file from a parser grammar file.

附言我读过一篇文章,坚持认为手动树走比树语法更好".这是可靠的信息吗?如果是这样,我制作一个手动的树漫步器比编写一个 ANTLR 树语法文件更好吗?然后,我如何使用我的 ANTLR 解析器语法文件制作手动树漫步器(它使用重写规则制作 AST)?

P.S. I read an article that insists that 'Manual Tree Walking Is Better Than Tree Grammars'. Is this reliable information? If so, would it be better for me to make a manual tree walker than writing an ANTLR tree grammar file? And then, how do I make a manual tree walker with my ANTLR parser grammar file(it makes an AST using rewrite rules)?

提前致谢.

推荐答案

sky 写道:

我想知道有没有系统的方法可以从解析器语法文件中生成树语法文件

I want to know if there is a systematic way to generate a tree grammar file from a parser grammar file

您已经描述了执行此操作的系统方法:复制树语法中的解析器/产生式规则,只将重写规则留在其中.这可能会处理大部分规则,但对于其他解析器规则(使用内联 AST 重写规则),它可能看起来略有不同.因此,没有自动生成树语法的方法.

You've already described the systematic way to do this: copy the parser/production rules in the tree grammar and only leave the rewrite rules in it. This will probably handle the larger part of your rules, but with other parser rules (using inline AST rewrite rules), it might look slightly different. Because of that, there is no automatic way to generate a tree grammar.

sky 写道:

附言我读过一篇文章,坚持认为'手动树走比树语法更好.这是可靠的信息吗?

P.S. I read an article that insists that 'Manual Tree Walking Is Better Than Tree Grammars'. Is this reliable information?

是的,是的.请注意,Terence Parr(ANTLR 的创建者)自己在 ANTLR wiki 上发布了这篇文章,因此它的作者(Andy Tripp)提出了有效的观点.

Yes, it is. Note that Terence Parr (creator of ANTLR) posted the article on the ANTLR wiki himself, so that says the author of it (Andy Tripp) raises valid points.

sky 写道:

如果是这样,我做一个手动的 tree walker 会比写一个 ANTLR 树语法文件更好吗?

If so, would it be better for me to make a manual tree walker than writing an ANTLR tree grammar file?

正如安迪在他的结论中所提到的:关于是使用树语法"方法还是仅仅手工完成"进行翻译的决定是一个品味问题.".因此,如果您认为编写树语法太麻烦,请使用手动方式.这取决于您:这里没有最佳方法.

As Andy mentioned in his conclusion: "The decision about whether to use a "Tree Grammar" approach to translation vs. just "doing it by hand" is a matter of taste.". So, if you think writing tree grammar is too much hassle, go the manual way. It's up to you: there is no best way here.

sky 写道:

然后,我如何使用我的 ANTLR 解析器语法文件(它使用重写规则制作 AST)来制作手动树漫步器?

And then, how do I make a manual tree walker with my ANTLR parser grammar file(it makes an AST using rewrite rules)?

您的解析器将创建一个 AST,默认类型为 CommonTree(API 文档).您可以使用该树来获取子项、父项、令牌的类型等:手动遍历树所需的一切.

Your parser will create an AST, which by default is of type CommonTree (API-doc). You can use that tree to get the children, the parent, the type of the token etc.: all you need to manually walk the tree.

请注意,在 ANTLR 的下一个版本(版本 4)中,(最有可能)在给定组合或解析器语法的情况下自动生成树遍历器.

Note that in the next version of ANTLR (version 4) it will (most likely) be possible to automatically generate a tree walker given a combined- or parser grammar.

见:

这篇关于生成ANTLR树语法的系统方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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