ANTLR 的 Java 树解析器输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10061548/
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
Java Tree parser output for ANTLR
提问by marchemike
I've found a sample template in the ANTLR website, its the Javatreeparser.g, which the site says could produce the AST that I need, but since I'm new to ANTLR, how do I make it show? What I've done so far is placing the grammar file together with my existing java grammar. But I have no idea on how to use and output the AST that I need from the file. How do I do it?
我在 ANTLR 网站上找到了一个示例模板,它是 Javatreeparser.g,该网站说它可以生成我需要的 AST,但是由于我是 ANTLR 的新手,我该如何让它显示出来?到目前为止,我所做的是将语法文件与我现有的 java 语法放在一起。但我不知道如何使用和输出文件中我需要的 AST。我该怎么做?
回答by Bart Kiers
I've found a sample template in the ANTLR website, its the Javatreeparser.g, which the site says could produce the AST that I need,
我在 ANTLR 网站上找到了一个示例模板,它是 Javatreeparser.g,该网站说它可以生成我需要的 AST,
No, the combined grammar Java.g
from the ANTLR wikiproduces a lexer and parser for Java source files. The parser then constructs an AST of this source and this AST can then be used by JavaTreeParser.g
to traverse it. The tree grammar JavaTreeParser.g
is notused to create an AST. This is done by the parser created from Java.g
.
不,Java.g
来自 ANTLR wiki的组合语法为 Java 源文件生成词法分析器和解析器。然后解析器构造这个源的 AST,然后可以使用这个 ASTJavaTreeParser.g
来遍历它。树语法JavaTreeParser.g
是不用于创建AST。这是由从Java.g
.
What I've done so far is placing the grammar file together with my existing java grammar.
到目前为止,我所做的是将语法文件与我现有的 java 语法放在一起。
That is incorrect. The tree grammar JavaTreeParser.g
expects an AST as input that the parser generated from Java.g
produced. You can't just plug in another parser (or other tree grammar, for that matter).
那是不正确的。树语法JavaTreeParser.g
期望 AST 作为解析器生成的输入Java.g
。您不能只是插入另一个解析器(或其他树语法,就此而言)。
But I have no idea on how to use and output the AST that I need from the file. How do I do it?
但我不知道如何使用和输出文件中我需要的 AST。我该怎么做?
See this previous Q&A: Visualizing an AST created with ANTLR (in a .Net environment)
请参阅之前的问答:可视化使用 ANTLR 创建的 AST(在 .Net 环境中)
EDIT
编辑
I didn't want to post this immediately, because I wanted you to give it a try yourself first (yes, I'm mean!) ;)
我不想立即发布此内容,因为我希望您先自己尝试一下(是的,我是认真的!);)
Here's a quick demo:
这是一个快速演示:
- copy the
Java.g
in a directory and remove the@header{...}
and@lexer:::header{...}
declarations from it; - copy
antlr-3.3.jar
into the same directory; - create the files
Main.java
andTest.java
in this directory (see below).
- 将 复制到
Java.g
目录中并从中删除@header{...}
和@lexer:::header{...}
声明; - 复制
antlr-3.3.jar
到同一个目录; - 创建的文件
Main.java
,并Test.java
在该目录中(见下文)。
Test.java
测试.java
public class Test {
int i = 1 + 2;
String s;
Test(String s) {
this.s = s;
}
}
Main.java
主程序
import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;
import org.antlr.stringtemplate.*;
public class Main {
public static void main(String[] args) throws Exception {
JavaLexer lexer = new JavaLexer(new ANTLRFileStream("Test.java"));
JavaParser parser = new JavaParser(new CommonTokenStream(lexer));
CommonTree tree = (CommonTree)parser.javaSource().getTree();
DOTTreeGenerator gen = new DOTTreeGenerator();
StringTemplate st = gen.toDOT(tree);
System.out.println(st);
}
}
Now generate a lexer and parser:
现在生成一个词法分析器和解析器:
java -cp antlr-3.3.jar org.antlr.Tool Java.g
Then compile all .java
source files:
然后编译所有.java
源文件:
javac -cp antlr-3.3.jar *.java
And finally run the Main
class and pipe the output to a file called ast.dot
.
最后运行Main
该类并将输出通过管道传输到名为ast.dot
.
java -cp .:antlr-3.3.jar Main > ast.dot
(on Windows, do: java -cp .;antlr-3.3.jar Main > ast.dot
)
(在Windows上,做的:java -cp .;antlr-3.3.jar Main > ast.dot
)
If you now open the file ast.dot
, you see a DOTrepresentation of the AST produced by the parser. You can visualize this AST by copy-pasting the DOT-source in here: http://graphviz-dev.appspot.comresulting in the following image:
如果您现在打开文件ast.dot
,您会看到解析器生成的 AST的DOT表示。您可以通过在此处复制粘贴 DOT 源来可视化此 AST:http: //graphviz-dev.appspot.com,结果如下图:
回答by Edward
I really recommend you to use antlr4.
我真的建议你使用 antlr4。
First, set your CLASSPATH (including antlr-4.5.3-complete.jar) and JAVA_HOME.
首先,设置你的CLASSPATH(包括antlr-4.5.3-complete.jar)和JAVA_HOME。
Second, generate a lexer and parser from the grammar Java.g4:
其次,从语法 Java.g4 生成词法分析器和解析器:
java -cp antlr-4.5.2-complete.jar Java.g4
Third, compile all Java*.java genereated:
三、编译所有生成的Java*.java:
javac Java*.java
Finally, run TestRig:
最后,运行 TestRig:
java org.antlr.v4.runtime.misc.TestRig Java compilationUnit -gui Test.java
You will see AST visually as follows:
您将在视觉上看到 AST,如下所示: