Take the 2-minute tour ×

I heard that a program can be presented as a tree. I also heard that there is a programming language whose syntax make this idea very clear. I mean that programs written in this language can be easily represented as a tree. Does anybody know what is the name of this language?

share|improve this question
Programs are actually graphs, not trees. –  anon Mar 12 '10 at 10:17
@Neil: it depends on how you look at them. There are call graphs and control flow graphs (which are usually not trees), and then there are parse trees (which are usually, erm, trees). –  ЯegDwight Mar 12 '10 at 15:33
Until you join common subexpressions, where it can suddenly turn into graphs. –  Vatine Mar 12 '10 at 16:07

6 Answers 6

up vote 17 down vote accepted

The Lisp language can be easily seen as a tree due to its parenthesized form. For example:

 (lambda (arg) (+ arg 1))

                             / | \ 
                            /  |  \
                           /   |   \
                      lambda   O    O
                              /    /|\ 
                             /    / | \
                            /    /  |  \
                          arg   +  arg  1

Of course every program can be represented by its syntax tree but this is not as easy to see.

share|improve this answer
I would consider "lambda" as a node with two branches "arg" and "+". And then I would consider "+" as another node with two branches "arg" and "1". –  Roman Mar 12 '10 at 10:44
That would be another way but you would have problems with the node "()" which is a valid element syntax-wise –  Remo.D Mar 12 '10 at 11:22


share|improve this answer

Make :-)

share|improve this answer
I would say that Make is more resembling a graph than a tree. –  Remo.D Mar 12 '10 at 10:31
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. –  Mark Hurd Aug 26 '12 at 14:20

The syntax of code in most programming languages can be represented as a tree. Parsers usually work by transforming the program text into a tree, which is called a parse tree.

Lisp has a syntax that makes the tree-structure of code quite explicit, since the source code is written as nested lists. Eg. an if in Lisp:

(if (= a (+ b c))
    (print (string-append "something" a))
    (print "something else"))

Equivalent in Python:

if a == b + c:
    print "something: " + a
    print "something else"

The tree structure of the code is very clear in the Lisp code (as a tree of nested lists), while it is somewhat obscured in the Python code due to syntax and precedence rules.

The representation of code as a tree structure of nested lists is a powerful feature. For example, you can pretty easily write macros which transforms code. The drawback is that some find the very uniform Lisp code hard to read.

share|improve this answer

Any language using polish notation matches your question.

Other than Lisp, most popular are Postscript and PDF.

Unix dc is another example.

share|improve this answer
I thought Forth was a lot of fun, quite a few years ago. One of the charming things is how : ? . ! ; is a standard definition in the language, but it is firmly based on Polish notation. –  David Thornley Mar 12 '10 at 15:36

You might be thinking of the rather unfortunately named 'Jackson Structured Programming' method which, whilst not a language in its own right, does force you to lay out programs in a strict tree format.

share|improve this answer

Your Answer


