Typechecking Assignment

The third step in building a compiler is to implement semantics and typechecking. You will build upon the code from the parser assignment, by constructing the Abstract Syntax Tree, resolving names to symbols, and performing type checking. To get you on the right track, you should start with the following example code that provides the header files for the AST and an implementation of the symbol table.

As before, your program must be written in plain C (not C++), and have a Makefile that produces a binary program called cflat. make clean should also delete all temporary files, so that the program can be made again from scratch.

If your program is invoked as follows:

./cflat -print sourcefile.cflat
...then you will simply construct the AST, and then reproduce the program on the standard output by traversing the AST via decl_print, stmt_print, etc. Your output may differ cosmetically in the amount of whitespace, number of parentheses, but it must print an equivalent program that can be re-parsed by your compiler.

If your program is invoked as follows:

./cflat -resolve sourcefile.cflat
...then you will construct the AST and resolve variable names to symbols using decl_resolve, stmt_resolve, etc. For each variable name resolved, you should print a mesage like:
x resolves to local variable 3

If your program is invoked as follows:

./cflat -typecheck sourcefile.cflat
...then you will construct the AST, resolve variable names, and perform typechecking, using decl_typecheck, stmt_typecheck, etc. In each place where type equivalence is required, you must look for compatibility of types. If an operation is not type-safe (for example, adding a string to an integer), then you should emit an error like: type error: cannot add a string to an integer

As always, a successful print, resolve, or typecheck should result in the compiler exiting with status zero, and a failure must emit a reasonable error message and exit with status one. The -scan and -parse options from previous assignments must also continue to function correctly. If your previous assignments had some bugs, then it is your responsibility to fix them.

A compiler has many odd corner cases that you must carefully handle. You must test your program extensively by designing and testing a large number of test cases. To encourage you to test thoroughly, we will also require you to turn in ten testing input files. Five should be named good[1-5].cflat and should be valid C-flat programs. Five should be named bad[1-5].cflat and should contain at least one error case.

For this assignment, your grade will be based upon the following:

  • Correctness of the -print option. (30 percent)
  • Correctness of the -resolve option. (30 percent)
  • Correctness of the -typecheck option. (30 percent)
  • Good programming style. (10 percent)
  • To turn in the assignment, copy your source files, Makefile, and testing files into your dropbox directory, which is:

    /afs/nd.edu/coursefa.08/cse/cse40243.1/dropbox/YOURNAME/typecheck