ANTLR 的 Java 树解析器输出 [英] Java Tree parser output for ANTLR

查看:30
本文介绍了ANTLR 的 Java 树解析器输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 ANTLR 网站上找到了一个示例模板,它是 Javatreeparser.g,该网站说它可以生成我需要的 AST,但是由于我是 ANTLR 的新手,我该如何让它显示出来?到目前为止,我所做的是将语法文件与我现有的 java 语法放在一起.但我不知道如何使用和输出文件中我需要的 AST.我该怎么做?

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,

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,

不,组合语法 Java.g 来自 ANTLR wiki 为 Java 源文件生成词法分析器和解析器.然后解析器构造这个源的 AST,然后 JavaTreeParser.g 可以使用这个 AST 来遍历它.树语法JavaTreeParser.g用于创建AST.这是由从 Java.g 创建的解析器完成的.

No, the combined grammar Java.g from the ANTLR wiki produces 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 not used to create an AST. This is done by the parser created from Java.g.

到目前为止我所做的是将语法文件与我现有的 Java 语法放在一起.

What I've done so far is placing the grammar file together with my existing java grammar.

这是错误的.树语法 JavaTreeParser.g 期望 AST 作为解析器从 Java.g 生成的输入.您不能只是插入另一个解析器(或其他树语法,就此而言).

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).

但我不知道如何使用和输出文件中我需要的 AST.我该怎么做?

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 创建的 AST(在 .Net 环境中)

我不想立即发布此内容,因为我希望您先自己尝试一下(是的,我是认真的!);)

I didn't want to post this immediately, because I wanted you to give it a try yourself first (yes, I'm mean!) ;)

这是一个快速演示:

  1. Java.g 复制到一个目录中并删除 @header{...}@lexer:::header{...} 来自它的声明;
  2. antlr-3.3.jar复制到同一目录下;
  3. 在此目录中创建文件 Main.javaTest.java(见下文).
  1. copy the Java.g in a directory and remove the @header{...} and @lexer:::header{...} declarations from it;
  2. copy antlr-3.3.jar into the same directory;
  3. create the files Main.java and Test.java in this directory (see below).

测试.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 

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

javac -cp antlr-3.3.jar *.java 

最后运行 Main 类并将输出通过管道传输到一个名为 ast.dot 的文件.

And finally run the Main class and pipe the output to a file called ast.dot.

java -cp .:antlr-3.3.jar Main > ast.dot

(在 Windows 上,执行:java -cp .;antlr-3.3.jar Main > ast.dot)

如果您现在打开文件 ast.dot,您会看到 DOT 解析器生成的 AST 的表示.您可以通过在此处复制粘贴 DOT 源来可视化此 AST:http://graphviz-dev.appspot.com 产生以下图像:

If you now open the file ast.dot, you see a DOT representation of the AST produced by the parser. You can visualize this AST by copy-pasting the DOT-source in here: http://graphviz-dev.appspot.com resulting in the following image:

这篇关于ANTLR 的 Java 树解析器输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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