Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

To make our C-- language more powerful, we sometimes need to learn from other existing languages. The C language allows you to define new names

To make our C-- language more powerful, we sometimes need to learn from other existing languages. 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:

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.

a. Assume that the following productions have been added to the grammar for the c-- language:

decl ? typedef typedef ? TYPEDEF type ID SEMICOLON 

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

b. 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:

The declaration typedef T xxx; is an error if T is not a built-in type (e.g., int, bool) or a struct type (in which case T will be of the form: struct ttt) 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 built-in 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.)

Answer each of the following questions:

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

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Microsoft SQL Server 2012 Administration

Authors: Adam Jorgensen, Steven Wort

1st Edition

1118106881, 9781118106884

More Books

Students also viewed these Databases questions