Question: The C language allows you to define new names for existing types using typedefs. Here is an example: typedef int money; int x; money y;
The C language allows you to define new names for existing types using typedefs. Here is an example:
typedef int money;
int x;
money y;
typedef money dollars;
dollars z;
x ; OK because x and are of type int
y x; OK because x and y are of type int
z y; OK because y and z are of type int
A typedef can occur anywhere that a variable declaration local or global can occur. The usual static 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. Assume that the A language has been extended with typedefs. Now consider the nameanalysis phase of the compiler. Note that, in addition to the usual errors for multiplydefined names and for uses of undefined names, the name analyzer must enforce the following rules:
The declaration typedef T xxx; is an error if T is not a builtin type eg int, bool or a new type defined by a previous typedef in the current or an enclosing scope.
The declaration typedef T xxx; is an error if xxx has already been declared in the current scope as a variable, function, parameter, or new type
A variable, function, or parameter can only be declared to be of type T if T is either a builtin type or a new type defined by a previous typedef in the current or an enclosing scope. A variable can still be declared to be of type struct ttt as before.
For the remaining questions, you will be asked various questions about how this extension to the language might change semantic analysis. Illustrate your answers about name analysis by showing the entries that would be in the symbol table after processing the following declarations:
typedef string date;
date today;
typedef int dollars;
dollars salary;
typedef dollars moreDollars;
moreDollars md;
int d;
You may answer the questions about type analysis with your own examples or with descriptions of what changes should be made. If no changes to an analysis is needed, you may simply state that.
Question : How does the symbol table need to be augmented to support typedefs?
Question : What should be done to process a typedef typedef T xxx; in name analysis and type analysis?
Question : What should be done to process a declaration of a variable, function, or parameter named xxx and declared to be of type T in name analysis and type analysis?
Question : What should be done to process the use of a name xxx in a statement in name analysis and type analysis?
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
