Skip to content

Instantly share code, notes, and snippets.

@tonyseek
Created November 28, 2012 12:46
Show Gist options
  • Select an option

  • Save tonyseek/4161012 to your computer and use it in GitHub Desktop.

Select an option

Save tonyseek/4161012 to your computer and use it in GitHub Desktop.
Visualizing Gcc's AST
#!/usr/bin/env sh
SCRIPT_PATH=$(dirname $0)
gcc -o $1 $1.c -fdump-tree-original-raw
$SCRIPT_PATH/pre.awk $1.c.* | $SCRIPT_PATH/treeviz.awk > $1.dot
dot -Tpng $1.dot -o $1.png
#!/usr/bin/env gawk -f
/^[^;]/{
gsub(/^@/, "~@", $0);
gsub(/( *):( *)/, ":", $0);
print;
}
#!/usr/bin/env gawk -f
#http://alohakun.blog7.fc2.com/?mode=m&no=355
BEGIN {RS = "~@"; printf "digraph G {\n node [shape = record];";}
/^[0-9]/{
s = sprintf("%s [label = \"{%s | {", $1, $1);
for(i = 2; i < NF - 1; i++)
s = s sprintf("%s | ", $i);
s = s sprintf("%s}}\"];\n", $i);
$0 = s;
while (/([a-zA-Z]+):@([0-9]+)/){
format = sprintf("\\1 \\3\n %s:\\1 -> \\2;", $1);
$0 = gensub(/([a-zA-Z]+):@([0-9]+)(.*)$/, format, "g");
};
printf " %s\n", $0;
}
END {print "}"}
@alapha23

alapha23 commented Feb 1, 2018

Copy link
Copy Markdown

Strictly it is Parse Tree rather than Ast you are visualizing

Thank you for your sharing and your code is helpful.

I personally also built something similar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment