[antlr-interest] AST Visualisation
Jim Idle
jimi at temporal-wave.com
Mon Mar 16 09:17:39 PDT 2009
Sam Barnett-Cormack wrote:
> Hi all,
>
> Having used google to search the archives, and the whole web, and found
> only references to some apparently out-of-date stuff to do with
> "ASTFrame", I'm now asking - does anyone know an easy way to visualise a
> generated AST?
>
Assuming that t is your tree returned from the parser:
// Pick up the generic tree
//
Tree t = (Tree)psrReturn.getTree();
...
// Use the ANTLR 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 to generate the grahpical
// version of the dot file.
//
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.");
Note that this produces the .dot file, which is then processed by the
dot command to make a png or whatever else you need. See the graphviz
web site for details on that:
dot -Tpng -omygraphic.png mydotspec.dot
> Sam
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>
More information about the antlr-interest
mailing list