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

查看:108
本文介绍了生成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.

P.S.我读过一篇文章,坚持认为"手动爬树胜过树语法" .这是可靠的信息吗?如果是这样,那么与编写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写道:

sky wrote:

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

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写道:

sky wrote:

P.S.我读过一篇文章,坚持认为手动走树胜过树语法" .这是可靠的信息吗?

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

是的.请注意,ANTLR的创建者Terence Parr亲自在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写道:

sky wrote:

如果是这样,那么与编写ANTLR树语法文件相比,对我来说,做一个手动的树行者会更好吗?

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

正如安迪(Andy)在其结论中提到的那样:决定是否使用树语法"方法进行翻译还是仅使用手工完成"来做决定." .因此,如果您认为编写树语法太麻烦了,请采用手动方式.由您决定:这里没有最好的方式.

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写道:

sky wrote:

然后,如何使用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 -doc).您可以使用该树来获取子代,父代,令牌的类型等:所有您需要手动行走的树.

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.

请参阅:

  • http://www.antlr.org/wiki/display/~admin/ANTLR+v4+plans
  • http://www.antlr.org/wiki/display/~admin/2011/09/05/Auto+tree+construction+and+visitors
  • http://www.antlr.org/wiki/display/~admin/2011/09/08/Sample+v4+generated+visitor

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

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