UP | HOME

Interpreter Project
Compiler Project
COP-5621

Table of Contents

1. Overview

In this project you will implement an interpreter for the While language using the ANTLR visitor of the While grammar from the Lexer project. Refer to While's semantics for specifics on its behavior.

2. Instructions

Create a python program called

~/cop5621spring25/compiler/compiler/Interpreter.py

Be sure to commit and push to your course repo. Include several test cases that illustrate the correct operation of your interpreter.

Notice that with the While language, there are no functions or well-defined input or output facilities. Please print the symbol table as output, e.g., something like this:

symtab[a]: 2
symtab[outparam]: 5
symtab[x]: 10

You could designate special variables for inputs and outputs, e.g., inparam and outparam, where inparam is taken from the first argument to the interpreter or stdin and ~outparam is printed to standard out. Be sure to described any assumptions in a README as well as providing an example command execution for you interpreter.

Feel free to assume (positive) integers for values.

You can use the ctx.op.type == WhileParser.PLUS pattern to check a token type (instead of having to use a hard-coded string). Additionally, you can use the ctx.ID().getText() to get token lexemes (the string value of the token).

Below is a suggested code template, but as long as the python program is executable and prints the symbol table as output, that is sufficient.

2.1. Suggested template code

import sys
from antlr4 import *
sys.path.append('./')  # if you want to avoid PYTHONPATH
from grammar.WhileLexer import WhileLexer
from grammar.WhileParser import WhileParser
from grammar.WhileVisitor import WhileVisitor
import logging
from textwrap import indent, dedent

symtab = {}

class Interpreter(WhileVisitor):
    # Visit a parse tree produced by WhileParser#Assignment.
    def visitAssignment(self, ctx:WhileParser.AssignmentContext):
        return self.visitChildren(ctx)


    # Visit a parse tree produced by WhileParser#Skip.
    def visitSkip(self, ctx:WhileParser.SkipContext):
        return self.visitChildren(ctx)


    # Visit a parse tree produced by WhileParser#If.
    def visitIf(self, ctx:WhileParser.IfContext):
        return self.visitChildren(ctx)


    # Visit a parse tree produced by WhileParser#While.
    def visitWhile(self, ctx:WhileParser.WhileContext):
        return self.visitChildren(ctx)


    # Visit a parse tree produced by WhileParser#Compound.
    def visitCompound(self, ctx:WhileParser.CompoundContext):
        return self.visitChildren(ctx)


    # Visit a parse tree produced by WhileParser#Not.
    def visitNot(self, ctx:WhileParser.NotContext):
        return self.visitChildren(ctx)


    # Visit a parse tree produced by WhileParser#ROp.
    def visitROp(self, ctx:WhileParser.ROpContext):
        return self.visitChildren(ctx)


    # Visit a parse tree produced by WhileParser#Or.
    def visitOr(self, ctx:WhileParser.OrContext):
        return self.visitChildren(ctx)


    # Visit a parse tree produced by WhileParser#And.
    def visitAnd(self, ctx:WhileParser.AndContext):
        return self.visitChildren(ctx)


    # Visit a parse tree produced by WhileParser#True.
    def visitTrue(self, ctx:WhileParser.TrueContext):
        return self.visitChildren(ctx)


    # Visit a parse tree produced by WhileParser#False.
    def visitFalse(self, ctx:WhileParser.FalseContext):
        return self.visitChildren(ctx)


    # Visit a parse tree produced by WhileParser#BParen.
    def visitBParen(self, ctx:WhileParser.BParenContext):
        return self.visitChildren(ctx)


    # Visit a parse tree produced by WhileParser#AOp.
    def visitAOp(self, ctx:WhileParser.AOpContext):
        return self.visitChildren(ctx)


    # Visit a parse tree produced by WhileParser#Var.
    def visitVar(self, ctx:WhileParser.VarContext):
        print(ctx.ID())
        return self.visitChildren(ctx)


    # Visit a parse tree produced by WhileParser#Num.
    def visitNum(self, ctx:WhileParser.NumContext):
        return self.visitChildren(ctx)


    # Visit a parse tree produced by WhileParser#AParen.
    def visitAParen(self, ctx:WhileParser.AParenContext):
        return self.visitChildren(ctx)

input_stream = StdinStream()
lexer = WhileLexer(input_stream)
stream = CommonTokenStream(lexer)
parser = WhileParser(stream)
tree = parser.s()
if parser.getNumberOfSyntaxErrors() > 0:
  print("syntax errors")
  exit(1)
interpreter = Interpreter()
interpreter.visit(tree)
print("\n".join([ f"symtab[{name}]: {symtab[name]}" for name in symtab ]))

Author: Paul Gazzillo

Created: 2025-02-05 Wed 15:37

Validate