2012-02-05 106 views
6

如何從java src代碼生成AST使用ANTLR?
有幫助嗎?使用ANTLR爲java源代碼生成抽象語法樹

+1

方式太寬泛。從ANTLR教程開始,或尋找現有的語法。 – millimoose 2012-02-05 20:17:13

+0

獲取Java語法。通過ANTLR生成的類運行Java源代碼。不過,可能會更容易使用Eclipse的東西。 – 2012-02-05 20:21:51

+0

你可以請發佈任何有用的鏈接或教程?謝謝 – Aboelnour 2012-02-05 23:03:49

回答

1

的setps使用antlr4生成Java SRC AST是:

  1. 安裝antlr4你可以使用this鏈接來做到這一點。安裝後下載JAVA grammar from here
  2. 現在使用命令生成Java8Lexer和Java8Parser:

    antlr4 -visitor Java8.g4

  3. 這將生成多個文件,如Java8BaseListener.javaJava8BaseVisitor.javaJava8Lexer.javaJava8Lexer.tokensJava8Listener.javaJava8Parser.javaJava8.tokensJava8Visitor.java

使用此代碼生成AST:

import java.io.File; 
import java.io.IOException; 

import java.nio.charset.Charset; 
import java.nio.file.Files; 

import org.antlr.v4.runtime.ANTLRInputStream; 
import org.antlr.v4.runtime.CommonTokenStream; 
import org.antlr.v4.runtime.ParserRuleContext; 
import org.antlr.v4.runtime.RuleContext; 
import org.antlr.v4.runtime.tree.ParseTree; 

public class ASTGenerator { 

    public static String readFile() throws IOException { 
     File file = new File("path/to/the/test/file.java"); 
     byte[] encoded = Files.readAllBytes(file.toPath()); 
     return new String(encoded, Charset.forName("UTF-8")); 
    } 

    public static void main(String args[]) throws IOException { 
     String inputString = readFile(); 
     ANTLRInputStream input = new ANTLRInputStream(inputString); 
     Java8Lexer lexer = new Java8Lexer(input); 
     CommonTokenStream tokens = new CommonTokenStream(lexer); 
     Java8Parser parser = new Java8Parser(tokens); 
     ParserRuleContext ctx = parser.classDeclaration(); 

     printAST(ctx, false, 0); 
    } 

    private static void printAST(RuleContext ctx, boolean verbose, int indentation) { 
     boolean toBeIgnored = !verbose && ctx.getChildCount() == 1 && ctx.getChild(0) instanceof ParserRuleContext; 

     if (!toBeIgnored) { 
      String ruleName = Java8Parser.ruleNames[ctx.getRuleIndex()]; 
      for (int i = 0; i < indentation; i++) { 
       System.out.print(" "); 
      } 
      System.out.println(ruleName + " -> " + ctx.getText()); 
     } 
     for (int i = 0; i < ctx.getChildCount(); i++) { 
      ParseTree element = ctx.getChild(i); 
      if (element instanceof RuleContext) { 
       printAST((RuleContext) element, verbose, indentation + (toBeIgnored ? 0 : 1)); 
      } 
     } 
    } 
} 

你做的編碼就可以使用gradle這個來構建你的項目,也可以在你的項目目錄下載antlr-4.7.1-complete.jar並啓動編譯之後。

如果你想在一個DOT文件輸出,使u能可視化的AST,那麼你可以參考this QNA郵寄或直接引用這個repository在我所使用gradle這個來構建項目。

希望這會有所幫助。 :)

12

OK,這裏的步驟:

  1. ANTLR Site現在下載最新版本是4.7
  2. here下載Java.gJavaTreeParser.g文件。
  3. 執行如下命令:

    java -jar antlrTool Java.g 
    java -jar antlrTool JavaTreeParser.g 
    
  4. 5的文件將被生成:

    1. Java.tokens
    2. JavaLexer.java
    3. JavaParser.java
    4. JavaTreeParser.g
    5. JavaTreeParser.tokens

使用這個Java代碼來生成抽象語法樹,並打印:

 String input = "public class HelloWord {"+ 
        "public void print(String r){" + 
        "for(int i = 0;true;i+=2)" + 
        "System.out.println(r);" + 
        "}" + 
        "}"; 

    CharStream cs = new ANTLRStringStream(input); 
    JavaLexer jl = new JavaLexer(cs); 

    CommonTokenStream tokens = new CommonTokenStream(); 
    tokens.setTokenSource(jl); 
    JavaParser jp = new JavaParser(tokens); 
    RuleReturnScope result = jp.compilationUnit(); 
    CommonTree t = (CommonTree) result.getTree(); 

    CommonTreeNodeStream nodes = new CommonTreeNodeStream(t); 

    nodes.setTokenStream(tokens); 

    JavaTreeParser walker = new JavaTreeParser(nodes); 

    System.out.println("\nWalk tree:\n"); 

    printTree(t,0); 


    System.out.println(tokens.toString()); 

    } 

public static void printTree(CommonTree t, int indent) { 
    if (t != null) { 
     StringBuffer sb = new StringBuffer(indent); 
     for (int i = 0; i < indent; i++) 
      sb = sb.append(" "); 
     for (int i = 0; i < t.getChildCount(); i++) { 
      System.out.println(sb.toString() + t.getChild(i).toString()); 
      printTree((CommonTree)t.getChild(i), indent+1); 
     } 
    } 
} 
+1

謝謝@Aboelnour。但是,你正在接受的頁面不再存在。任何幫助? – 2014-08-22 06:23:15

+0

另一個問題,如果JavaTreeParser.java不在生成的文件中,它來自哪裏? – 2014-08-22 06:26:30

+1

@MakanTayebi我認爲這將有所幫助:https://github.com/antlr/grammars-v4 – ConductedClever 2017-08-07 20:23:08