Homework 4
CS536-S24 Intro to PLs and Compilers

Question 1 | Question 2 | Question 3


The C language allows you to define new names for existing types using typedefs. Here is some example code that uses typedefs:

typedef int money;
int x;
money y;
typedef money dollars;
dollars z;

x = 10;
y = x;    // OK because x and y are of type int
z = y;    // OK because y and z are of type int

The first typedef defines money to be a synonym for int. Any declaration that follows this typedef can use money instead of int. The second typedef defines dollars to be a synonym for money, which makes it a synonym for int. Any declaration that follows this typedef can use dollars instead of int.

Typedefs can also be used with struct types (which are similar to tuple types in base):

struct Pair {
    int x;
    int y;
};
typedef struct Pair Point;
Point p;

A typedef can occur anywhere that a variable declaration (local or global) can occur. The usual C scoping rules apply to the names in typedefs. Note that typedef int money; is considered to be a declaration of the name money and that both money x; and typedef money dollars; are considered to be uses of the name money.


Question 1

Assume that the following productions have been added to the grammar for the base language:

decl → typedef
typedef → TYPEDEF type ID DOT

What other productions need to be changed and/or added to the base grammar to allow typedefs?


Question 2

Now consider the name-analysis phase of the compiler. Note that, in addition to the usual errors for multiply-defined names and for uses of undefined names, the name analyzer must enforce the following rules:

Answer each of the following questions:

  1. What information should be stored with each name in the symbol table?

  2. What should be done to process a typedef: typedef T xxx;?

  3. What should be done to process a declaration of a variable, function, or parameter named xxx and declared to be of type T?

  4. What should be done to process the use of a name xxx in a statement?

Illustrate your answer by showing the entries that would be in the symbol table after processing the following declarations:

tuple MonthDayYear {
    integer month.
    integer day.
    integer year.
}.
typedef tuple MonthDayYear date.
date today.
typedef integer dollars.
dollars salary.
typedef dollars moreDollars.
moreDollars md.
integer d.

Question 3

Now consider the type-checking phase of the compiler. In addition to the usual type-checking that is done, you must ensure that the return type of a function, the formal parameters of a function, and the arguments given to a function when it is called must be either integer or logical or a typedef that ultimately "resolves" to integer or logical; for example, given the declarations in Question 2, the following would be a valid function declaration:

dollars fctnGood{moreDollars x} [
    return x * 7.
]

while the following would not:

void fctnBad{date d} [
]

What, if any, changes need to be made to this phase given that typedefs have been added to the language?


Last Updated: 4/1/2024     © 2024 CS 536