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

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

问题描述

我在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?

见前面的问答A:可视化使用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 into同一目录;

  3. 创建文件 Main.java Test.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).



Test.java



Test.java

public class Test {

    int i = 1 + 2;
    String s;

    Test(String s) {
        this.s = s;
    }
}



Main.java



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 Tree解析器输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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