Docsity

Practice Problems in Midterm - Introduction to Compilers | CMSC 430, Exams of Computer Science

Toggle sidebar
bg1
CMSC 430
Midterm 1 Practice Problems
1. Attribute grammars and syntax-directed translation
(a) What are the advantages of syntax-directed
translation over context-free parsing?
(b) What are the differences between context-free
and context-sensitive parsing?
(c) Rank the following techniques in terms of the
languages (sets of strings) they can recognize:
syntax-directed translation, attribute grammars,
context-free grammars, context-sensitive gram-
mars.
(d) In an attribute grammar, what are the dif-
ferences between inherited and synthesized at-
tributes?
(e) Write a small attribute grammar with one in-
herited and one synthesized attribute, labeling
each.
2. Type expressions
Given the following C declarations:
int num(char *x, int chuk[4]);
typedef struct {
int a, b;
} CELL;
CELL foo[100];
CELL *bar(int x, CELL y) { ... }
(a) write the type expression for ”num”
(b) write the type expression for ”foo”
(c) write the type expression for ”bar”
3. Syntax-directed translation
Consider the following grammar productions. As-
sume you have an attribute E.odd which can be set
to either true, false, or unknown, and an attribute
CONST.val which is the value of the constant.
E→CONST {E.odd = ?? }
|ID {E.odd = unknown }
|E1+ E2{E.odd = ?? }
|E1- E2{E.odd = ?? }
|E1* E2{E.odd = ?? }
|( E1){E.odd = ?? }
|- E1{E.odd = ?? }
(a) Add rules to the attribute grammar to calcu-
late E.odd for each grammar production.
(b) Provide the parse tree for the expression
(5 + 2) - (4 * - x)
and show the calculation for E.odd at each
point.
4. Syntax-directed translation and type checking
Consider the following grammar, which generates
expressions for a number of operators. Assume you
have the attributes E.min and E.max which should
be set to the minimum and maximum values for E,
and an attribute CONST.val which is the value of
the constant.
E→CONST {E.min = ?? ; E.max = ?? }
|ID {E.min = ID.min ; E.max = ID.max }
|E1+ E2{E.min = ?? ; E.max = ?? }
|E1- E2{E.min = ?? ; E.max = ?? }
|E1* E2{E.min = ?? ; E.max = ?? }
|( E1){E.min = ?? ; E.max = ?? }
|- E1{E.min = ?? ; E.max = ?? }
(a) Write the type-checking rules which calculates
the range for each subexpression.
(b) Provide the parse tree for the expression
(5 + 2) - (4 * - x)
and show the calculation for E.min and E.max
at each point, assuming you know x.min = -3
and x.max = 10.
pf2

Partial preview of the text

Download Practice Problems in Midterm - Introduction to Compilers | CMSC 430 and more Exams Computer Science in PDF only on Docsity!

CMSC 430

Midterm 1 Practice Problems

  1. Attribute grammars and syntax-directed translation

(a) What are the advantages of syntax-directed translation over context-free parsing? (b) What are the differences between context-free and context-sensitive parsing? (c) Rank the following techniques in terms of the languages (sets of strings) they can recognize: syntax-directed translation, attribute grammars, context-free grammars, context-sensitive gram- mars. (d) In an attribute grammar, what are the dif- ferences between inherited and synthesized at- tributes? (e) Write a small attribute grammar with one in- herited and one synthesized attribute, labeling each.

  1. Type expressions

Given the following C declarations:

int num(char *x, int chuk[4]); typedef struct { int a, b; } CELL; CELL foo[100]; CELL *bar(int x, CELL y) { ... }

(a) write the type expression for ”num” (b) write the type expression for ”foo” (c) write the type expression for ”bar”

  1. Syntax-directed translation Consider the following grammar productions. As- sume you have an attribute E.odd which can be set to either true, false, or unknown, and an attribute CONST.val which is the value of the constant. E → CONST { E.odd = ?? } | ID { E.odd = unknown } | E 1 + E 2 { E.odd = ?? } | E 1 - E 2 { E.odd = ?? } | E 1 * E 2 { E.odd = ?? } | ( E 1 ) { E.odd = ?? } | - E 1 { E.odd = ?? }

(a) Add rules to the attribute grammar to calcu- late E.odd for each grammar production. (b) Provide the parse tree for the expression (5 + 2) - (4 * - x) and show the calculation for E.odd at each point.

  1. Syntax-directed translation and type checking Consider the following grammar, which generates expressions for a number of operators. Assume you have the attributes E.min and E.max which should be set to the minimum and maximum values for E, and an attribute CONST.val which is the value of the constant. E → CONST { E.min = ?? ; E.max = ?? } | ID { E.min = ID.min ; E.max = ID.max } | E 1 + E 2 { E.min = ?? ; E.max = ?? } | E 1 - E 2 { E.min = ?? ; E.max = ?? } | E 1 * E 2 { E.min = ?? ; E.max = ?? } | ( E 1 ) { E.min = ?? ; E.max = ?? } | - E 1 { E.min = ?? ; E.max = ?? }

(a) Write the type-checking rules which calculates the range for each subexpression. (b) Provide the parse tree for the expression (5 + 2) - (4 * - x) and show the calculation for E.min and E.max at each point, assuming you know x.min = - and x.max = 10.

  1. Syntax-directed translation and type checking

Consider the following grammar, which generates expressions formed by applying ”+” to integer and floating point constants. When two integers are added, the result is integer, otherwise, it is a float. E → E + T | T T → num. num | num

(a) Give a syntax-directed definition to determine the type of each subexpression. Assign each symbol an attribute ”type”. (b) Provide the parse tree for the express 5 + 4 + 3.2 + 1 and show the computation E.type and T.type at each point.

  1. Symbol table

Consider the following program in a lexically-scoped language such as C.

int x, y; int foo( ) { int z; { int y; { int x; } } } int bar( ) { int z; { int x; } { int y; // HERE } }

(a) What is nested lexical scoping? (b) How can symbol tables handle nested lexical scoping? (c) How does the compiler determine where each variable encountered during compilation is ac- tually declared when handling nested lexical scoping? (d) Use your answer to construct the logical state of the symbol table(s) for the example when the compiler reaches the point marked HERE.