Christoph Lehner's Tree Drawing Program for Prolog
I was looking for a tree-drawing utility, to depict the syntax trees generated by a compiler I wrote in Prolog in order to teach a friend how compilers work. I have just found Christoph Lehner's Prolog Tree Drawing Program.
His program can be downloaded from the link above, which also shows some examples. It displays trees as ASCII. Here are some examples which I tried, passing various terms to the main predicate "drucke_baum":
3 ?- <strong>drucke_baum( 1 ).</strong><br /> 1<br /><br /><br />true.<br /><br />4 ?- <strong>drucke_baum( a ).</strong><br /> a<br /><br /><br />true.<br /><br />5 ?- <strong>drucke_baum( 1+a ).</strong><br /> +<br /> |<br /> / \<br /> 1 a<br /><br /><br />true <br />.<br /><br />6 ?- <strong>drucke_baum( sum( [1,2,3] ) ).</strong><br /> sum<br /> |<br /> |<br /> .<br /> _|_<br /> / \<br /> 1 .<br /> _|_<br /> / \<br /> 2 .<br /> _|<br /> / \<br /> 3 []<br /><br /><br />true <br />.<br /><br />7 ?- <strong>drucke_baum( root( a, b(1), c(3,4), d(e(5),f(g(6))), h ) ).</strong><br /> root<br /> ________|_______<br /> / | | | \<br /> a b c d h<br /> | | |<br /> | / \ / \<br /> 1 3 4 e f<br /> | |<br /> | |<br /> 5 g<br /> |<br /> |<br /> 6<br /><br /><br />true <br />.<br />
Notice that the program displays lists in terms of their underlying representation: the list
[1,2,3]<br />is
.(1, .(2, .(3, [])))<br />
I tried the program under SWI-Prolog for XP, version 5.6.64. Consulting it gave me lots of singleton-variable warnings, which can be ignored. It also gave three errors, which I fixed (correctly, I hope) as follows:
- On line 201, replace '\' by '\\'. This escapes the backslash. Otherwise, SWI will complain about a string being too long.
- On line 221, put brackets around the argument of "not". On SWI, "not" isn't an operator, so needs a bracketed argument.
- On line 290, remove the definition of "compound". This is built in to SWI, and will provoke an error about attempted redefinition.

