Skip to content

Instantly share code, notes, and snippets.

@cognominal
Last active October 3, 2015 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cognominal/eedf4128848d4e54ab9a to your computer and use it in GitHub Desktop.
Save cognominal/eedf4128848d4e54ab9a to your computer and use it in GitHub Desktop.
create a call graph
my $test = q:to<END>;
void a();
void a(
) {
b();
c();
}
END
grammar G {
token TOP { [ .*? <fun> ]+ .* }
token nm { <[a..z_]>+ }
token fun { ^^ \w \V*? <nm> '(' <-[)]>*? ')' \s* <fun-block> };
token end-fun-block { ^^ '}'}
token fun-block {
'{' [
[ <!before <.end-fun-block> > . ]+? # skip, not to far
<called-fun=.nm> '('
]*
.*?
<.end-fun-block>
}
}
sub infix:«->» { " $^a -> $^b\n" };
class actions {
method TOP($/) {
my $s = '';
for $<fun> {
my @called = (grep { $_ ne 'sizeof'| 'memcpy' }, .<fun-block><called-fun>).unique( :with( &[eq] )) ;
$s ~= [~] ~.<nm> X[->] @called;
}
make "digraph A \{\n$s\}";
}
}
# say G.parse($test);
say G.parse(slurp("src/rrb.c"), :actions(actions)).ast ;
git clone https://github.com/hyPiRion/c-rrb
cd c-rrb
perl6 dot.p6 > rrb.dot # dot.p6 must created in c-rrb
dot -Tpdf rrb.dot > rrb.pdf
open rrb.pdf # on mac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment