

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


(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.
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”
(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.
(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.
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.
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.