一般情况下 AST 到 XML(也许是 ANTLR)
我需要解析用某些语言(Java、C、C#...)编写的文件,然后将 AST(抽象语法树)跟踪到 xml。 (实际上目的是操纵它并追溯到另一种语言 - 第二部分已经实现)。经过调查,我发现没有通用的方法可以做到这一点。
最接近的一个是 srcML。但第一个问题是它不是 Java =)。第二个问题是语言数量(只有 3 种)。
我知道 DMS 可以解决这个问题,但它不是免费和开放的-来源。
因此,据我了解,有一种方法可以做到这一点:使用 ANTLR 并尝试将 AST 转换为 XML。所以问题是如何使用 ANTLR(Java) 来做到这一点,或者也许我错过了一些(不是 ANTLR 方式)来做到这一点。
I need to parse files written in some languages(Java, C, C#...) and then trace the AST(Abstract syntax tree) to xml. (Actually the aim is to manipulate it and trace to another language - this second part have been implemented). After investigation I find out that there is no common approach to do this.
The most closest one is srcML. But first problem is that it is not Java =). The second problem is amount of languages (only 3).
I know that DMS can solve this problem, but it is not free and open-source.
So, as I understand, there is single way to do this: take ANTLR and try to convert AST to XML. So question is how to do it with ANTLR(Java), or maybe I miss some(not ANTLR way) to do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除了 ANTLR 之外,还有更多 Java 工具可以做到这一点(JavaCC 是一种流行的替代方案,仅举一例)。
使用解析器生成器来解决这个问题,您需要执行以下操作:
Java、C# 和 C 的语法可在 ANTLR 的 Wiki 上找到,我确信存在现成的语法对于 JavaCC(以及其他解析器生成器工具:Google 是您的朋友)。但请注意,这是一个 Wiki,许多语法处于实验状态,或者包含错误。
您可以跳过第 1 步并找到为您构建 AST 的现有解析器。您只需要自己遍历 AST 并从中创建 XML。例如,这里是 Java 5 解析器(对于其他解析器,再次说明,Google 是您的朋友)。
祝你好运。
There are more Java tools besides ANTLR that can do this (JavaCC is a popular alternative, to name just one).
Using a parser generator to solve this problem, you'd need to do the following:
Grammars for Java, C# and C are available on ANTLR's Wiki, I'm sure readily available grammars exist for JavaCC (and other parser generator tools: Google is your friend here). But be aware that it is a Wiki, and many grammars are in an experimental state, or contain errors.
You could just skip step #1 and find existing parser that construct the AST for you. You only need to walk the AST yourself and create an XML from it. Here's a Java 5 parser, for example (for the other ones, again, Google is your friend).
Good luck.