CS536 Homework 4 Solution

Question 1:

The following productions need to be added to the Cflat grammar:

type → ID
varDeclList → varDeclList typedef

The first production permits typedefs to be used with struct types. The second production permits a declaration that uses a new type name. The third production permits a typedef to occur anywhere that a local variable declaration can occur.

Question 2:

  1. Each symbol table entry should include "kind" (variable, function, struct,or typedef) information, as well as "type" information. For a typedef, the "type" information is the built-in type or struct type for which the typedef defines a new name. (Note: It is also possible to let the "type" field be a typedef name, but that complicates type checking. Note also that if the "kind" field is not needed for type checking, then it is OK just to have a flag that says whether or not this is a typedef name.)

  2. To process typedef T xxx:

  3. Processing a variable/function/parameter declaration T xxx is the same as processing typedef T xxx except that when xxx is added to the symbol table, its kind is var/function/parameter (as appropriate) instead of "typedef".

  4. To process a use of name xxx in a statement:

Here is the symbol table after processing the given declarations:

Name Kind Type
MonthDayYear struct -
price variable int
quantity variable int
date typedef struct MonthDayYear
today variable struc MonthDayYear
dollars typedef int
salary variable int
moreDollars typedef int
md variable int
d variable int