Parser and Interpreter Implementation

Project Overview

The main goal of this project is to create an interpreter for my project language. I accomplished this using Java and the JavaCC tool.

Source code can be found at: github

Project Language

To the right is the context-free syntax of the project language in Extended BNF notation.

This language combines imperative and functional features and is primarily based on SML. The language is statically scoped and dynamically typed.

Parser Implementation

I first used the JavaCC tool to generate a purse parser, ProjLangParser, for my language. I expressed the language syntax using EBNF. This parser was tested using the ParseMain.java file which created a ProjLangParser object and used input from standard input.

Interpreter Implementation

I then modified this parser so that I would be able to enforce static and dynamic semantic rules in addition to ensuring syntatic validality. To implement semantic constructs into the project I modified the previous JavaCC parser by adding return types to the non-terminals as well as including actions in the grammar rules to the right of these non-terminals. This allowed the parser to return an Abstract Syntax Tree (AST).

Abstract Syntax Tree:

In this project, the AST was simply an object that invokes the evaluator method defined for its class. I defined an expression class, Expr.Java, that has several subclasses for each of the possible expressions that can be evaluated for this language.

These subclasses would be required to define their own evaluation methods that evaluates the input per its semantic rules. For instance, I implemented a LetValExp and WhileExp subclass.

Testing:

This parser was tested using InterpretMain.java which created a ProjLangParser object and used input from standard input. It uses this input to create an expression object which would invoke its evaluation method.