Channels ▼

Jocelyn Paine

Dr. Dobb's Bloggers

Christoph Lehner's Tree Drawing Program for Prolog

February 12, 2010

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 />
  .(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.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.