views:

278

answers:

6

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?

+9  A: 

Lisp.

AraK
+14  A: 

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

 (lambda (arg) (+ arg 1))

                               O
                              /|\
                             / | \ 
                            /  |  \
                           /   |   \
                      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.

Remo.D
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
That would be another way but you would have problems with the node "()" which is a valid element syntax-wise
Remo.D
A: 

Make :-)

Phil Nash
I would say that Make is more resembling a graph than a tree.
Remo.D
+5  A: 

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
else:
    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.

JacquesB
A: 

Any language using polish notation matches your question.

Other than Lisp, most popular are Postscript and PDF.

Unix dc is another example.

mouviciel
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
A: 

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.

Matt