UP | HOME

Typechecker Project
Compiler Project
COP-5621

Table of Contents

1. Overview

In this project you will implement a type checker for the While language, using a special version of the grammar that combines operators.

Using the template code and the informal type specification, write a type checker that emits exit code 0 when there is no type error detected and exit code 1 when there is.

Be sure to test your program amply so that visitors for all constructs are working correctly. Create your own test cases and submit at least 10 of them with your code to your submission repo.

2. Informal type specification

2.1. Declarations

  • There are only two types, "int" and "bool". Use these string names in the symbol table.
  • It is a type error if the symbol is redeclared (even to the same type)

2.2. Assignment

  • Check that the type of the symbol being assigned is the same as the expression on the right-hand-side. It is a type error if they are not
  • It is also a type error if the symbol has not been declared previously

2.3. If

  • It is a type error if the condition is not of bool type
  • It is a type error if either one of the if or else block has a type error

2.4. While

  • It is a type error if the condition is not of bool type
  • It is a type error if the body has a type error

2.5. Compound

  • It is a type error if any of the statements have a type error

2.6. Literals

  • true and false are bool
  • numbers are int

2.7. Binary operators

  • [TypeWhileParser.PLUS, TypeWhileParser.MINUS, TypeWhileParser.MULT, TypeWhileParser.DIV] take two int operands and return an int
  • [TypeWhileParser.AND, TypeWhileParser.OR] take two bool operands and return a bool
  • [TypeWhileParser.EQ, TypeWhileParser.LT, TypeWhileParser.LE, TypeWhileParser.GT, TypeWhileParser.GE] take two int operands and return a bool

3. Instructions

3.1. Setup the grammar and the get the code template

cd ~/cop5621spring25/compiler/
curl https://www.cs.ucf.edu/~gazzillo/teaching/cop5621spring25/files/TypeWhile.g4 > grammar/TypeWhile.g4
curl https://www.cs.ucf.edu/~gazzillo/teaching/cop5621spring25/files/Makefile > grammar/Makefile
make -C grammar
curl https://www.cs.ucf.edu/~gazzillo/teaching/cop5621spring25/files/TypeChecker.py > compiler/TypeChecker.py

3.2. Running

Here are a few example test programs:

mkdir -p examples
curl https://www.cs.ucf.edu/~gazzillo/teaching/cop5621spring25/files/type_declaration.while > examples/type_declaration.while
curl https://www.cs.ucf.edu/~gazzillo/teaching/cop5621spring25/files/type_declaration2.while > examples/type_declaration2.while
curl https://www.cs.ucf.edu/~gazzillo/teaching/cop5621spring25/files/type_declaration_fail.while > examples/type_declaration_fail.while

Run each and check the exit codes:

python3 compiler/TypeChecker.py < examples/type_declaration.while
echo $?
python3 compiler/TypeChecker.py < examples/type_declaration2.while
echo $?
python3 compiler/TypeChecker.py < examples/type_declaration_fail.while
echo $?

These are the outputs for each, including the exit code. The error message doesn't matter, but exit code 0 should be used for no type error and exit code 1 used for a type error.

examples/type_declaration2.while
symtab[x]: bool
0
examples/type_declaration_fail.while
type error: redeclaration of x
1
examples/type_declaration.while
symtab[x]: int
0

Author: Paul Gazzillo

Created: 2025-03-04 Tue 06:08

Validate