tags:

views:

216

answers:

3

I'd like to automatically generate a graph of a simple, tree-recursive program in Scheme. In the end, I'd like the output to wind up with something like this (only not as wretched):

     fib (2)
fib (0)  fib (1)

Edit: I know about Common Lisp's trace functionality. I was hoping for something a little more aesthetically appealing, although it will do in a pinch.

+2  A: 

I'm not sure if it is exactly what you want (re "automatically"), but you could look at Graphviz . I've used it for loads of things in the past and is a very useful utility.

Greg Whitfield
+2  A: 

@Greg

Graphviz isn't exactly what I was looking for, but "automatically" may have been too strong; I just don't want to modify my program to get the output. If I can translate the output from trace into Graphviz's DOT language, that might do the trick. If anyone's interested, I'll try to update with more details after I give it a go. I'm still looking for prepackaged solution, though.

Hank Gay
+2  A: 

It's a long time since I've done any Lisp, but I've been using GraphViz recently.

Googling for common lisp tracing, I see that TRACE outputs something like:

 0: (FACTORIAL 4)
   1: (FACTORIAL 3)
     2: (FACTORIAL 2)
       3: (FACTORIAL 1)
       3: returned 1
     2: returned 2
   1: returned 6
 0: returned 24

If I understand that format correctly, it should be pretty easy to write a program in your favourite scripting language, to convert it into .dot

output "digraph G {"
while(get line) {
     if(this line is more indented than previous line) {
          caller = parsed from previous line
          called = parsed from this line
          output "caller -> called;"
     }
}
output "}";

That's for runtime analysis. One could also do static source analysis so that instead of:

fact(4) -> fact (3) -> fact(2) -> fact(1)

... you'd just get

fact <-.
  |____|
slim