UP | HOME

Parser Project
Compiler Project
COP-5621

Table of Contents

1. Overview

In this project you will implement a pretty printer using the ANTLR visitor of the While grammar from the Lexer project.

The only rule is that nested conditional blocks should be indented. The choice of whitespace and other constructs is up to you.

Submit your code to your submission repo.

2. Instructions

Create a python program called

~/cop5621spring25/compiler/compiler/Parser.py

Here is a template base visitor to start from:

2.1. 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

class Printer(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)
printer = Printer()
printer.visit(tree)

3. Grading Criteria

Test case

  • 10pt if the pretty printer works and indents properly, i.e., nested constructs work and each level has the same indent level
  • -1pt if nested constructs aren't handled
  • -1pt if each level does not have same indent level
  • -1pt for using the wrong program name
  • -1pt for additional or missing tokens from the input
  • 0pt if missing or not working

Author: Paul Gazzillo

Created: 2025-03-04 Tue 06:08

Validate