Skip to content

Instantly share code, notes, and snippets.

@TuringKi
Forked from tonyseek/Usage.markdown
Created October 8, 2022 11:50
Show Gist options
  • Save TuringKi/ed09933c589b127207ff8733b619ca79 to your computer and use it in GitHub Desktop.
Save TuringKi/ed09933c589b127207ff8733b619ca79 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 "}"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment