[antlr-interest] Good "dot" Invocation Options for Visualizing ASTs?

Jim Idle jimi at temporal-wave.com
Mon Oct 6 08:36:46 PDT 2008


On Mon, 2008-10-06 at 07:00 -0700, Randall R Schulz wrote:

> Hi,
> 
> Has anybody come up with good recipes for using the GraphViz 
> package's "dot" command for rendering the graphs of ASTs produced by 
> ANTLR 3 (.1.1)? If so, could you share? Or point me to resources that 
> would be me a head start on coming up with good incantations of the dot 
> command?
> 


it is built in to the Java and C runtimes. For Java:


  public static StringTemplate _treeST =
                new StringTemplate(
			"digraph {\n" +
			"  ordering=out;\n" +
			"  ranksep=.4;\n" +
            "  bgcolor=\"lightgrey\";" +
			"  node [shape=box, fixedsize=false, fontsize=12, fontname=\"Helvetica-bold\", fontcolor=\"blue\"\n" +
			"        width=.25, height=.25, color=\"black\", fillcolor=\"white\", style=\"filled, solid, bold\"];\n" +
			"  edge [arrowsize=.5, color=\"black\", style=\"bold\"]\n" +
			"  $nodes$\n" +
			"  $edges$\n" +
			"}\n");


    public static StringTemplate _edgeST =
		new StringTemplate("$parent$ -> $child$ // \"$parentText$\" -> \"$childText$\"\n");

...

                    // Use the ANLTR built in dot generator
                    //
                    DOTTreeGenerator gen = new DOTTreeGenerator();
                    
                    // Which we can cause to generate the DOT specification
                    // with the input file name suffixed with .dot. You can then use
                    // the graphviz tools or zgrviewer (Java) to view the grahpical
                    // version of the dot file.
                    //
                    source = source.substring(0, source.length()-3);
                    String outputName = source + "dot";
                    
                    System.out.println("    Producing AST dot (graphviz) file");
                    
                    // It produces a jguru string template.
                    //
                    StringTemplate st = gen.toDOT(t, new CommonTreeAdaptor(), _treeST, _edgeST);

                    // Create the output file and write the dot spec to it
                    //
                    FileWriter outputStream = new FileWriter(outputName);
                    outputStream.write(st.toString());
                    outputStream.close();

                    // Invoke dot to generate a .png file
                    //
                    System.out.println("    Producing png graphic for tree");
                    pStart = System.currentTimeMillis();
                    Process proc = Runtime.getRuntime().exec("dot -Tpng -o" + source + "png " + outputName);
                    proc.waitFor();
                    stop = System.currentTimeMillis();
                    System.out.println("      PNG graphic produced in " + (stop - pStart) + "ms.");



> Thanks.
> 
> 
> Randall Schulz
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
> 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20081006/59c68c43/attachment.html 


More information about the antlr-interest mailing list