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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:26:15  来源:igfitidea点击:

Java Tree parser output for ANTLR

javatreeantlrabstract-syntax-tree

提问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.gfrom 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.gto traverse it. The tree grammar JavaTreeParser.gis 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.gexpects an AST as input that the parser generated from Java.gproduced. 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:

这是一个快速演示:

  1. copy the Java.gin a directory and remove the @header{...}and @lexer:::header{...}declarations from it;
  2. copy antlr-3.3.jarinto the same directory;
  3. create the files Main.javaand Test.javain this directory (see below).
  1. 将 复制到Java.g目录中并从中删除@header{...}@lexer:::header{...}声明;
  2. 复制antlr-3.3.jar到同一个目录;
  3. 创建的文件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 .javasource files:

然后编译所有.java源文件:

javac -cp antlr-3.3.jar *.java 

And finally run the Mainclass 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,结果如下图:

enter image description here

在此处输入图片说明

回答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,如下所示:

enter image description here

在此处输入图片说明