AST Visualizer for TinyPie

by Ruslan Spivak on January 23, 2011

If you want to see how TinyPie AST (Abstract Syntax Tree) for different language constructs looks like you can now use gendot command line utility that I’ve added to the project and that is available in the development mode when you use buildout. This utility generates DOT file than can be further processed by dot program to draw nice graphs.

This is how it works.
First you need to clone or git pull changes from the tinypie repo.

$ cd tinypie
$ git pull

Rerun buildout to generate gendot utility.

$ bin/buildout

Now you can generate DOT output for TinyPie AST. You can pass a file name to gendot or send some text to its standard input.
To generate DOT file for function call, for example, do the following:

$ echo -n 'foo(3)' | bin/gendot
digraph astgraph {
   node [shape=plaintext, fontsize=12, fontname="Courier", height=.1];
   edge [arrowsize=.5]

   node1 [label="BLOCK"];
   node2 [label="CALL"];
   node3 [label="ID (foo)"];
   node4 [label="INT (3)"];

   node2 -> node3
   node2 -> node4
   node1 -> node2

If you save that output and process it further with the dot command you’ll get a nice image of function call AST:

$ echo -n 'foo(3)' | bin/gendot >
$ dot -Tpng -o funcall.png
$ ls -l funcall.png
-rw-rw-r--. 1 alienoid alienoid 4617 Jan 23 17:37 funcall.png

This is how AST looks like for a factorial program:

$ cat
print factorial(5)

def factorial(x):
  if x < 2 return 1
  return x * factorial(x - 1)
$ cat | bin/gendot >
$ dot -Tpng -o factorial.png

If you enjoyed this post why not subscribe via email or my RSS feed and get the latest updates immediately. You can also follow me on GitHub or Twitter.

{ 1 comment… read it below or add one }

pk December 14, 2013 at 9:03 AM

Hi, nice work! Going to study tinypie… :)
About dot, do you know if this method of drawing
the AST always preserves the ordering among the nodes?
Have you had any surprises with that? It seems correct
in all you pictures and from what I have tried. But they
say that dot might not preserve the ordering….


Speak your mind

{ 1 trackback }

Previous post:

Next post: