How Can I Generate an AST from java src code Using ANTLR?
any help?
OK, here are the steps:
Java.g
and the JavaTreeParser.g
files from here.Run the following commands:
java -jar antlrTool Java.g
java -jar antlrTool JavaTreeParser.g
5 files will be generated:
use this java code to generate the Abstract Syntax Tree and to print it:
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);
}
}
}
The setps to generate java src AST using antlr4 are:
Now generate Java8Lexer and Java8Parser using the command:
antlr4 -visitor Java8.g4
This will generate several files such as Java8BaseListener.java
Java8BaseVisitor.java
Java8Lexer.java
Java8Lexer.tokens
Java8Listener.java
Java8Parser.java
Java8.tokens
Java8Visitor.java
Use this code to generate 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));
}
}
}
}
After you are done coding you can use gradle to build your project or you can download antlr-4.7.1-complete.jar in your project directory and start compiling.
If you want a the output in a DOT file so that u can visualise the AST then you can refer to this QnA post or directly refer to this repository in which i have used gradle to build the project.
Hope this helps. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With