Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to visualize an abstract syntax tree in graphviz dot, and it's just way too wide. How to fix?

So I'm working on a language and I wanted to, mostly out of curiosity, see if I could visualize the abstract syntax tree of a file. After some looking around I found graphviz dot, converted my AST prettyprinter to be able to output to this kind of format:

digraph G {
    main -> parse -> execute;
    main -> init;
    main -> cleanup;
    execute -> make_string;
    execute -> printf
    init -> make_string;
    main -> printf;
    execute -> compare;
}

But my problem is, when I run

dot -Tpng dotf.gv -o graph.png

On the input file, I end up with a file that is 8000 pixels in width, which is just not practical. See here.

I don't know if it's possible to fix, but if someone can I'd be grateful.

like image 240
Atheuz Avatar asked Nov 02 '22 21:11

Atheuz


1 Answers

The first thing to do would be to set the direction of the graph from the default bottom-to-top ranking to left-to-right, by inserting:

rankdir=LR;

... in the .dot file. That should orient the graph left-to-right and thereby make it much more compact for a case like this that probably has many nodes with long node labels.

There are some other ideas for reducing the width of graphs like this in Create a call graph for a file with clang.

like image 126
Simon Avatar answered Nov 15 '22 20:11

Simon