KOI: Lab Exercise 6

This exercise is about a (comparatively) simple way to build an interpreter, by executing the syntax trees.

Executing syntax trees

Write an interpreter that executes the syntax trees from lab 5.

One way is to write a recursive function, called for example execute, that takes a pointer to a tree node as argument. This function should examine the type field in the node, and, depending on the type of node, perform the proper action.

For example, if the type of node is '+', your function should first evaluate (that is, "calculate the value of") the arguments, by calling itself recursively with the two sub-trees of the nodes as arguments. Then it should perform the addition, and finally return the result.

Control structures, such as the if statement, can be handled in a similar way, except that sometimes some arguments should not be evaluated.

Report!

Show your results and discuss them with the teacher,
or,
send an e-mail:


Thomas Padron-McCarthy (Thomas.Padron-McCarthy@tech.oru.se) January 12, 2004