Type specification
Lecture 9

Table of Contents

Live coding continued

Type specification

What is a type?


  • Set of values
  • Operations on those values
  • Example: set of integers, arithmetic operators

Type checking

  • Catch untrapped bugs
    • Divide-by-zero trapped by hardware
    • Buffer overflow (in C) can happen silently (untrapped)

Static type checking

  • Guarantee correct type usage at compile-time

More info on types

Specifying type rules

Implementing type checking

Apply type rules

  • Equivalent to logical deduction
  • Example: 2 + 3
    • If 2 is an int, 2 is an int, and "+" returns an int
    • Then 2 + 3 is an int
  • Example: x = 4
    • If x is a struct, 4 is an int, and "=" requires same type
    • Then this is not type safe

Tree walking algorithm

  • Post-order traversal
  • Collect types from definitions and declarations
  • Deduce types bottom-up
    • Leaves are axioms: types are already defined
    • Inner nodes are conclusion: determine types from leaves and node type
      • Example: tree for 2 + 3

Repo prep

  • Initializing the repo
  • Creating and adding files
  • Adding the given files
  • Repo hygiene
    • Omit generated files (typical convention, but useful to have generated files somtimes)
    • .gitignore for generated files (makes it easy to manage changes)


Prepare your github compiler project repository to have the following files:

  • Create these files yourself
    • SimpleC.g4 (this is from a prior HW where you type it up yourself)
    • README.md (write a description of your project yourself here
  • These files you can get from the following links:

These should be all that is in your compiler project repo for now (no need for any subdirectories). By convention, do not push any generated files. Put their names in .gitignore so that git status shows no untracked files.

Author: Paul Gazzillo

Created: 2022-02-14 Mon 11:49