CMPS 450: LAB 06

CMPS 450 Lab 06 - Type Checking by Symbol Table Lookup

resources:
lab06_files

This lab is a continuation of the type checking semantics we started in Lab 05. In this lab you will enforce strong typing for identifiers by symbol table lookup. This will allow you to check type rules in assignments where the lvalue (left side of the assignment) references an identifier declared earlier in the program:

     x = b;   # valid only if type of identifier x matches type of identifier b 
     x = 3.4  # valid only if identifier x was declared as type float 
     x = y;   # valid only if x and y have the same type
Assignment statements such as shown above that reference identifiers declared in a previous statement can only be type checked by a symbol table lookup. A few changes need to be made to your scanner and parser to facilitate a symbol table lookup for an identifier:
  1. modify symbol table entries to hold type
  2. add functions to set and get type in symbol table
  3. modify the parser to add %union so an identifier can holds its name (the parser needs the name to do a symbol table lookup)
  4. modify the scanner to stuff the attribute bucket of an identifier with the identifier's name

The changes noted above have been made for you in lab06.l, lab06.y, and in symtab.c. You will need to write similar code in your parser for phase II. The current version of the scanner in lab06.l inserts an identifier into the symbol table and copies yytext to the identifier's attribute bucket (char * name in the union for yylval). The current version of the parser sets the type for an identifier.

You can start with the partial solution provided or make the necessary changes to your lab05 solution. You will need to finish the changes in order to decorate the parse tree with type and spit out semantic errors at the appropriate time. The current program writes to "log" a few debugging statements but not catch any semantic errors. You need to force the parser to lookup an identifer in the symbol table when checking the type for identifiers.

     $$ = lookupType($1); 
Copy all files from lab06_files into your directory:
       $cp  /home/fac/donna/public_html/cs450/lab06_files/* . 
Note that one of the files is a header called lab06.h. This header is included in lab06.y and symtab.c. This header file contains the defines associated with type checking.

The parser uses name as the hash to lookup that identifier in the symbol table using the lookup function that is coded for you. You must add semantic actions when you encounter assignments like this

    a = b; 
    a = 3.4;
by looking up the type for the identifier and and testing that the types on both sides match. Spit out the error to "log" and continue parsing. For this lab your parser should catch all semantic errors in bad.cf. If you run your parser against this bad.cf:
{ 
  int myInt;
  float myFloat;
  myFloat = 4;          //  type error lineno 4
  myInt  = 5 + 7.9;     //  type error lineno 5
  myFloat  = 2 + 3;     // type error  lineno 6
  myFloat = myInt;      // type error  lineno 7
}

Your log file should look something like this:

type mismatch id: myFloat, line 4
type mismatch in expr, line 5
type mismatch id: myFloat, line 6
type mismatch id: myFloat, line 7

semantic error cnt: 4