D.3 Problems for Chapter 6: Type Checking

D.3.1 Type as Attribute

In Pascal language, a programmer can declare two integer variables a and b with the syntax

var a,b: int

This declaration might be described with the following grammar:

decl   –> var idlist : typeid
idlist –> idlist , ID
idlist –> ID

Write an attribute grammar that assigns the correct data type to each declared variable. (You may have to rewrite the grammar.) Determine an evaluation order of the attributes irrespective of the way the parse tree is constructed. Can this translation be performed in a single pass over the parse tree as it is created?

Solution: The grammar would require an inherited attribute for passing the type of the identifier to the top of the parse tree where the ...

Get Compilers: Principles and Practice now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.