I am working with ANTLR4 to generate AST of a java source code and i had to move to ANTLR3 because i was not getting much help and documentation and it was really tough to proceed.I managed to generate AST but not in a visual format. Then i came across an awesome answer and i was really able to generate AST in a DOT file but there was a slight problem.
My code:
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.ANTLRFileStream;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.tree.DOTTreeGenerator;
import org.antlr.stringtemplate.StringTemplate;
class Main {
        public static void main(String[] args) throws Exception {
            parseFile("/home/satnam-sandhu/Workstation/ASTGenerator/resource/java/Blabla.java");
        }
    public static void parseFile(String f)throws Exception {
            JavaLexer lexer = new JavaLexer(new ANTLRFileStream(f));
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            JavaParser parser = new JavaParser(tokens);
            CommonTree tree = (CommonTree)parser.compilationUnit().getTree();
            DOTTreeGenerator gen = new DOTTreeGenerator();
            StringTemplate st = gen.toDOT(tree);
            System.out.println(st);
    }
}
I am using gradle so i build the project by:
gradle clean build
And then run it and pipe the output to a dot file by:
java -jar ASTGenerator.jar > ast.dot
Now the issue i am facing is that, for a source code of:
class example{
    public static void print(int a){
        int b = a +1;
        System.out.println(b);
    }
    public static void main(){
        print(15);
    }
}
I am getting this as the output:
digraph {
    ordering=out;
    ranksep=.4;
    bgcolor="lightgrey"; node [shape=box, fixedsize=false, fontsize=12, fontname="Helvetica-bold", fontcolor="blue"
        width=.25, height=.25, color="black", fillcolor="white", style="filled, solid, bold"];
    edge [arrowsize=.5, color="black", style="bold"]
  n0 [label=""];
  n1 [label="class"];
  n2 [label="example"];
  n3 [label="{"];
  n4 [label="public"];
  n5 [label="static"];
  n6 [label="void"];
  n7 [label="print"];
  n8 [label="("];
  n9 [label="int"];
  n10 [label="a"];
  n11 [label=")"];
  n12 [label="{"];
  n13 [label="int"];
  n14 [label="b"];
  n15 [label="="];
  n16 [label="a"];
  n17 [label="+"];
  n18 [label="1"];
  n19 [label=";"];
  n20 [label="System"];
  n21 [label="."];
  n22 [label="out"];
  n23 [label="."];
  n24 [label="println"];
  n25 [label="("];
  n26 [label="b"];
  n27 [label=")"];
  n28 [label=";"];
  n29 [label="}"];
  n30 [label="public"];
  n31 [label="static"];
  n32 [label="void"];
  n33 [label="main"];
  n34 [label="("];
  n35 [label=")"];
  n36 [label="{"];
  n37 [label="print"];
  n38 [label="("];
  n39 [label="15"];
  n40 [label=")"];
  n41 [label=";"];
  n42 [label="}"];
  n43 [label="}"];
  n0 -> n1 // "" -> "class"
  n0 -> n2 // "" -> "example"
  n0 -> n3 // "" -> "{"
  n0 -> n4 // "" -> "public"
  n0 -> n5 // "" -> "static"
  n0 -> n6 // "" -> "void"
  n0 -> n7 // "" -> "print"
  n0 -> n8 // "" -> "("
  n0 -> n9 // "" -> "int"
  n0 -> n10 // "" -> "a"
  n0 -> n11 // "" -> ")"
  n0 -> n12 // "" -> "{"
  n0 -> n13 // "" -> "int"
  n0 -> n14 // "" -> "b"
  n0 -> n15 // "" -> "="
  n0 -> n16 // "" -> "a"
  n0 -> n17 // "" -> "+"
  n0 -> n18 // "" -> "1"
  n0 -> n19 // "" -> ";"
  n0 -> n20 // "" -> "System"
  n0 -> n21 // "" -> "."
  n0 -> n22 // "" -> "out"
  n0 -> n23 // "" -> "."
  n0 -> n24 // "" -> "println"
  n0 -> n25 // "" -> "("
  n0 -> n26 // "" -> "b"
  n0 -> n27 // "" -> ")"
  n0 -> n28 // "" -> ";"
  n0 -> n29 // "" -> "}"
  n0 -> n30 // "" -> "public"
  n0 -> n31 // "" -> "static"
  n0 -> n32 // "" -> "void"
  n0 -> n33 // "" -> "main"
  n0 -> n34 // "" -> "("
  n0 -> n35 // "" -> ")"
  n0 -> n36 // "" -> "{"
  n0 -> n37 // "" -> "print"
  n0 -> n38 // "" -> "("
  n0 -> n39 // "" -> "15"
  n0 -> n40 // "" -> ")"
  n0 -> n41 // "" -> ";"
  n0 -> n42 // "" -> "}"
  n0 -> n43 // "" -> "}"
}
When using http://viz-js.com/ for visualising the output is like this:
 
All my work till now is uploaded here if you guys feel like to dig deeper into the grammar file i am using. I think options specified in the grammar file can also be the reason. I am a beginner cannot proceed without a little help. Thanks in advance. :)
