CS362 2011F Compilers

Project, Phase 4: Type Checking

One important phase of semantic analysis is type checking. In particular, most programmers find it useful if the compiler identifies locations in which they are using types in what seems to be an inappropriate way.

In this phase of the project, you will build a type checker for Pascal. You already have much of the infrastructure necessary to do type checking. Your parser (phase 2) builds an attributed parse tree, so you can add rules for processing types in the tree (and storing types in certain locations). Your symbol table (phase 3) provides a mechanism for you to store information about various named types.

As you may have noted, there are a few primary that you will need to pay attention to when type checking.

We will use a conservative type equivalence metric. In most cases, if two things have different types, they cannot be used in place of each other.

There is one exception: integer expressions can be used when the expected type is a subrange of the integers. For example, if we declare

type:
  Indices = 1..100;
  Stuff = array[Indices] of real;
var:
  i: integer;
  s: Stuff;

then it is safe to write

  s[i] = 3.0;
  s[i+1] = 2.3;

If you want to make your compiler more useful, you could add additional exceptions to the conservative type metric.

Because enumerated types can add significant complications, particularly when combined with subranges, you need not support enumerated types.

To further simplify this project, you need not support records. You also need not support sets or arrays of more than one dimension.

What's left? Still a lot: Some basic types, type aliases, subranges of integers, one-dimensional arrays, pointer types, and file types.

Here are some additional things to think about

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Sun Nov 20 22:32:12 2011.
The source to the document was last modified on Sun Nov 20 22:32:04 2011.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS362/2011F/Handouts/phase-4.html.

Samuel A. Rebelsky, rebelsky@grinnell.edu