A parse tree is associated with each of these. The File contains two Dictionaries , which allow us to lookup procedures and functions by name, and a pointer to the main program itself. In order to decouple the parse tree data structure from the various actors which want to traverse it, we use the Visitor design pattern. Each node in the parse tree inherits from an abstract class ParseTreeNode . Then, there is another abstract class called ParseTreeVisitor from which the PrettyPrinter , Typechecker , and Interpreter inherit.
Visitors then walk the tree by visiting each node.
For example, the Interpreter , when visiting an If node, would first visit the conditional Expression in the node. TypeChecker TypeChecker is a subclass of ParseTreeVisitor . Its job is to verify that the code meets the language's static semantics. The TypeChecker maintains a Dictionary which maps variable names to their types, and traverses the parse tree for each module, ensuring that variables are used in accordance with their declaration. For example, when the TypeChecker visits an If node, it check that the Expression is a boolean.