【问题标题】:Extracting AST from JAVA and printing the AST to a file从 JAVA 中提取 AST 并将 AST 打印到文件中
【发布时间】:2015-03-10 11:11:53
【问题描述】:

我是 Java 编程语言的初学者。我想从 java 源代码中提取 AST 并将 AST 打印到文件或标准输出中。

我按照本教程学习了如何使用 AST。 http://www.programcreek.com/2011/01/a-complete-standalone-example-of-astparser/

所以根据我目前的代码如下。

import java.util.HashSet;
import java.util.Set;

import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;

public class Test {
    public static void main(String args[]){
        ASTParser parser = ASTParser.newParser(AST.JLS3);
        parser.setSource("public class A { int i = 9;  \n int j; \n ArrayList<Integer> al = new ArrayList<Integer>();j=1000; }".toCharArray());
        //parser.setSource("/*abc*/".toCharArray());
        parser.setKind(ASTParser.K_COMPILATION_UNIT);
        //ASTNode node = parser.createAST(null);


        final CompilationUnit cu = (CompilationUnit) parser.createAST(null);

    }
}

我尝试使用以下代码 sn-p 将其打印到标准输出,但它没有给我预期的结果,

System.out.println(cu.getAST().toString());

如果有人可以帮助我将 AST 打印到文件中,那将是一个很大的帮助。

提前致谢。

【问题讨论】:

  • 你期待什么结果?
  • 我想打印出整个 AST,但结果是这样的,“org.eclipse.jdt.core.dom.AST@6eebc39e”
  • 你必须手动检查cu.getAST()对象的结构并打印你想要的。没有人(我希望)会代替你自己去做。
  • @hbn1991 这意味着AST 类不会覆盖toString() 方法本身。您应该对其内容进行迭代并手动打印。
  • 感谢两位的回复。但实际上有没有更简单的方法来转储 ast。因为我实际上不想对 AST 做任何处理。

标签: java debugging abstract-syntax-tree extraction compilationunit


【解决方案1】:

这里是an example of transforming the AST to JSON。您可以修改 JSONStyleASTPrinter.java 文件以生成 XML 而不是 JSON 等。

基于来自How To Train the JDT Dragon combined.pdf的示例:

private void print(ASTNode node) {
    List properties = node.structuralPropertiesForType();
    for (Iterator iterator = properties.iterator(); iterator.hasNext();) {
        Object descriptor = iterator.next();
        if (descriptor instanceof SimplePropertyDescriptor) {
            SimplePropertyDescriptor simple = (SimplePropertyDescriptor) descriptor;
            Object value = node.getStructuralProperty(simple);
            System.out.println(simple.getId() + " (" + value.toString() + ")");
        } else if (descriptor instanceof ChildPropertyDescriptor) {
            ChildPropertyDescriptor child = (ChildPropertyDescriptor) descriptor;
            ASTNode childNode = (ASTNode) node.getStructuralProperty(child);
            if (childNode != null) {
                System.out.println("Child (" + child.getId() + ") {");
                print(childNode);
                System.out.println("}");
            }
        } else {
            ChildListPropertyDescriptor list = (ChildListPropertyDescriptor) descriptor;
            System.out.println("List (" + list.getId() + "){");
            print((List) node.getStructuralProperty(list));
            System.out.println("}");
        }
    }
}

private void print(List nodes) {
    for (Iterator iterator = nodes.iterator(); iterator.hasNext();) {
        print((ASTNode) iterator.next());
    }
}

【讨论】:

    猜你喜欢
    • 2011-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-25
    • 1970-01-01
    • 1970-01-01
    • 2012-05-25
    相关资源
    最近更新 更多