Skip to content

Instantly share code, notes, and snippets.

@pepasflo
Last active October 2, 2023 16:23
Show Gist options
  • Save pepasflo/04f3c440b824a638fe9559494597f3b0 to your computer and use it in GitHub Desktop.
Save pepasflo/04f3c440b824a638fe9559494597f3b0 to your computer and use it in GitHub Desktop.
Notes on using graphviz to visualize abstract syntax trees (ASTs)

Using graphviz to visualize abstract syntax trees

screen shot 2019-02-15 at 5 52 08 pm

  • After you've written your first lexer and parser, write a function which walks the AST and "compiles" it into graphviz notation like the above
digraph parse_tree {
ratio = fill;
node [style=filled];
program -> functionDecl;
functionDecl -> fun;
functionDecl -> myFun;
functionDecl -> "(";
functionDecl -> ")";
functionDecl -> "{";
functionDecl -> varDecl;
functionDecl -> "}";
varDecl -> var;
varDecl -> aVar;
varDecl -> "=";
varDecl -> addition;
addition -> 1;
addition -> "+";
addition -> 2;
program [color="0.408 0.498 1.000"];
functionDecl [color="0.408 0.498 1.000"];
fun [color="0.641 0.212 1.000"];
myFun [color="0.641 0.212 1.000"];
"(" [color="0.641 0.212 1.000"];
")" [color="0.641 0.212 1.000"];
"{" [color="0.641 0.212 1.000"];
varDecl [color="0.408 0.498 1.000"];
"}" [color="0.641 0.212 1.000"];
var [color="0.641 0.212 1.000"];
aVar [color="0.641 0.212 1.000"];
"=" [color="0.641 0.212 1.000"];
addition [color="0.408 0.498 1.000"];
1 [color="0.641 0.212 1.000"];
"+" [color="0.641 0.212 1.000"];
2 [color="0.641 0.212 1.000"];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment