如何从 Java 源代码生成 AST?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1967987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to generate AST from Java source-code?
提问by Gili
As far as I know, the only way to parse Java source-code into an AST (Abstract Syntax Tree) is to use the Java Compiler Tree API: com.sun.source.tree
据我所知,将 Java 源代码解析为 AST(抽象语法树)的唯一方法是使用Java 编译器树 API:com.sun.source.tree
I have two questions:
我有两个问题:
- What JDKs support com.sun.source.tree?
- Is there a portable replacement that works for all JDKs?
- 哪些 JDK 支持 com.sun.source.tree?
- 是否有适用于所有 JDK 的便携式替代品?
采纳答案by TofuBeer
You can possibly take the tools.jar and use it. javac is open sourceso you can just grab that code (assuming you can deal with the license). Antlrhas grammars for Java as well.
您可以使用 tools.jar 并使用它。 javac 是开源的,因此您只需获取该代码(假设您可以处理许可证)。 Antlr也有 Java 语法。
回答by Brett Daniel
Regarding your second question, there are dozens of Java parsers available in addition to Sun's. Here is a small sample:
关于您的第二个问题,除了 Sun 的解析器之外,还有许多 Java 解析器可用。这是一个小样本:
- Eclipse's org.eclipse.jdt.core.dompackage.
- Spoonoutputs a very nice annotated parse tree with type information and variable binding (and uses Eclipse's parser internally)
- ANTLRis a parser-generator, but there are grammars for Java available
- javaparser(which I have not used)
- Eclipse 的org.eclipse.jdt.core.dom包。
- Spoon输出带有类型信息和变量绑定的非常漂亮的带注释的解析树(并在内部使用 Eclipse 的解析器)
- ANTLR是一个解析器生成器,但有可用的 Java 语法
- javaparser(我没有使用过)
My best advice is to try each of them to see which works best for your needs.
我最好的建议是尝试每一个,看看哪个最适合您的需求。
回答by Jeremy Raymond
I've used Eclipse's AST parser. I found it to be pretty good (well it was part of an Eclipse plug-in so it did make sense to use it). See Exploring Eclipse's ASTParser.
我使用过 Eclipse 的 AST 解析器。我发现它非常好(它是 Eclipse 插件的一部分,所以使用它确实有意义)。请参阅探索 Eclipse 的 ASTParser。
回答by Ira Baxter
It is not the only way.
这不是唯一的方法。
See our Java Front End, which is a full featured Java parser built on top of the DMS Software Reengineering Toolkit. It parses Java, and builds ASTs as internal data structures.
请参阅我们的Java 前端,它是构建在DMS 软件再工程工具包之上的全功能 Java 解析器。它解析 Java,并将 AST 构建为内部数据结构。
The point of DMS is that it provides a huge variety of additionaluseful machinery (attribute grammars, symbol tables, flow analysis, AST manipulation including access and update, as well as source-to-source transformations) to analyze and transform that AST into results and/or modified source code. If you get "just" a Java parser (e.g., JavaCC + Java grammar) you will, IMHO, not be able to do a lot with it. DMS makes it possible to do a lot, without having to invent all that extra machinery yourself.
DMS 的重点在于它提供了大量其他有用的机制(属性语法、符号表、流分析、AST 操作,包括访问和更新,以及源到源转换)来分析 AST 并将其转换为结果和/或修改过的源代码。如果您“只是”获得一个 Java 解析器(例如,JavaCC + Java 语法),恕我直言,您将无法用它做很多事情。DMS 可以做很多事情,而不必自己发明所有额外的机器。
If you really don't want to use the extra machinery DMS provides, it will dump the tree as XML.
如果您真的不想使用 DMS 提供的额外机制,它会将树转储为 XML。
回答by Esteban Küber
I've just come across Jexast, an extraction of the JDT's ASTParserto work independent of Eclipse(it depends on org.eclipse.jdt.internal.compiler.**
).
我刚刚遇到Jexast,它提取了JDT 的 ASTParser以独立于 Eclipse 工作(取决于org.eclipse.jdt.internal.compiler.**
)。
I haven't tried it yet, but it does seem interesting.
我还没有尝试过,但看起来确实很有趣。
回答by Federico Tomassetti
A working, simple to use Java Parser is... JavaParser. The project has been active for some years already. While it was initially hosted on Google code it is now available on GitHub: https://github.com/javaparser/javaparser
一个工作的、易于使用的 Java Parser 是... JavaParser。该项目已经活跃了几年。虽然它最初托管在 Google 代码上,但现在可以在 GitHub 上使用:https: //github.com/javaparser/javaparser
It is quite simple to use and the number of dependencies is small. It is also available on Maven.
它使用起来非常简单,并且依赖项的数量很少。它也可以在 Maven 上使用。
It has been used for a few years, so it works quite well and permits to parse also comments, to change the AST and regenerate the code.
它已经使用了几年,所以它工作得很好,并且还允许解析注释、更改 AST 并重新生成代码。