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

There are two ways to pass this lab. Ideally you should do this: But as an alternative, you can instead do this.

About sources and collaboration:
  • Each group (which normally consists of one or two students) must make its own solution to each assignment, and submit or present it, but it is not forbidden to collaborate or ask other students for help. However, in that case you must clearly state who you have collaborated with. Each solution must include the name of everyone who contributed to the work, Collaboration is thus perfectly fine, but must be clearly stated.
  • You must add an LLM statement to the lab report, where you state if and how you have used large language models, and some experiences from that. Was the LLM helpful, and in what way? Did it help your learning, or maybe in some way hinder it?
  • All sources (such as books, web pages, colleagues and LLMs) used in the solution of the assignment must be stated.


Thomas Padron-McCarthy (thomas.padron-mccarthy@oru.se) August 28, 2025