Compilers (CS362 2004S)

Type Checking a Simple Language

Summary: In today's lab, you will experiment with a simple type checker that I've written for your edification. I hope that this lab and the related code will help you think about the design of your parser, your parse trees, your type checker, and other tree walkers.

Contents:

Preliminaries

Preparation:

a. Obtain the files for this lab with

cvs -d  /home/rebelsky/Web/Courses/CS362/2004S/CVS checkout Lab09

b. Make sure that /home/rebelsky/Web/Courses/CS362/2004S/Examples is in your classpath. The easiest way to do so is

export CLASSPATH=".:/home/rebelsky/Web/Courses/CS362/2004S/Examples"

or

export CLASSPATH="/home/rebelsky/Web/Courses/CS362/2004S/Examples:$CLASSPATH"

Collaboration: Feel free to work on this lab in pairs or trios.

Turning It In: You don't need to bother turning in this lab.

Grading: Since you're not turning in the lab, I won't grade it.

Background

Since Pascal is a fairly big language, at times in the construction of a compiler or the consideration of pieces, it's worthwhile working with smaller pieces of the language. For the problem of type checking, two key program parts are the declarations and assignment statements.

I've designed a simple language that consists only of declarations and assignment statements. You can find the formal definition of the syntax of the language in the file AssignLangParser.java in the directory below. In essence, a program looks something like

var
  id1,id2,id3: type;
  id4,id5: type;
begin
  id := exp;
  id := exp;
end

Exercises

1. Read AssignLangParser.java (in the lab directory) to ensure that you understand the syntax of the language.

2. Run TestAL on the sample programs (and a few of your own) to ensure that you understand what the parse trees I build look like.

3. Given your understanding of the Assignment Language, make a list of the kinds of type-related and variable-related errors that might occur in programs.

4. Uncomment the instructions to type check in TestAL and recompile. See what kinds of errors the type checker currently seems to catch. See what kinds of errors it catches that it shouldn't.

5. Read AssignLangChecker.java. Summarize the kinds of errors the checker is designed to catch.

6. There is a significant bug in the checking of compound expressions. What is that bug? Fix it.

7. Make AssignLangChecker.java catch more errors (you can choose which other ones to catch). For example, you might check whether a variable is initialized before being used or you might deal with coercion.

8. Extend AssignLangChecker.java to permit the assignment of integers to reals with an associated warning message.

9. How would you extend AssignLangChecker.java to catch all type errors, rather than just the first one? (You need not update the code; just summarize how you would make this change.)

 

History

Tuesday, 29 October 2002 [Samuel A. Rebelsky]

Thursday, 1 April 2004 [Samuel A. Rebelsky]

 

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 Wed May 5 11:46:51 2004.
The source to the document was last modified on Thu Apr 1 17:08:36 2004.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS362/2004S/Labs/typecheck.html.

You may wish to validate this document's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu