



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Exam; Class: PROGRAMMING LANGUAGES; Subject: Computer Science; University: Rensselaer Polytechnic Institute; Term: Fall 2008;
Typology: Exams
1 / 6
This page cannot be seen from the preview
Don't miss anything!




III LL and LR Grammars.
P2. Consider the following grammar: S → [S Y ] | a Y → ≤ | + S Z | Z b Z → ≤ | − S Y c
S, Y and Z are nonterminals and a, b, c, +, −, [ and ] are terminals.
P3. Consider the following grammar over the terminals +, - (negation operator) and id : S → E E → E + E | − E | id
1
IV Attribute Grammars.
P1. Consider the grammar for postfix expressions (here E and Op are nonterminals and id , +,−,* and / are terminals):
E → E E Op | id Op → + | − | ∗ | /
Give an attribute grammar which translates postfix expressions into parenthesized infix ex- pressions. For example, expression AAB + / will be translated into (A/(A + B)).
P2. Consider this subset of the grammar for parenthesized arithmetic expressions:
E → E + T E → T T → T ∗ F T → F F → id F → (E)
Write an attribute grammar which constructs an abstract syntax tree for a parse tree. Your attribute grammar should get rid of redundant enclosing parentheses: that is, if an expression e is enclosed by multiple sets of parentheses, the redundant sets of parentheses should be removed. For example, the abstract syntax tree for expression (((a*b))+(((c+d)))) should be as follows:
(+) /
(*) (+) / \ /
a b c d
Note that only redundant sets of enclosing parentheses are removed (i.e., ((ab)) becomes (ab) and (((c+d))) becomes (c+d)). Parentheses that become redundant due to associativity and precedence are not removed (e.g., (a*b) remains parenthesized even though the parentheses are redundant due to the higher precedence of ∗ over +). You may look at slides 29 and 30 in Lecture 11 which describe the attribute grammar for the construction of the abstract syntax tree.
P3. The attribute flow graph is constructed for a parse tree given the attribute grammar asso- ciated to the underlying context-free grammar. It shows attribute dependencies. For example, recall the LL(1) version of the context-free grammar for attribute expressions and the attribute grammar which computes the value of arithmetic expressions (this grammar is described in Chapter 4 of textbook and repeatedly in lectures). Consider expression 3+5. Its corresponding parse tree is as follows (T1, F1, T’1, E’1 are the indexed nonterminals T 1 , F 1 , T 1 ′, E 1 ′):
E /
T E’ / \ /|
F T’ + T1 E’ | | | \ | num:3 eps F1 T’1 eps | | num1:5 eps
The attribute flow graph for this parse tree is as follow:
num.val → F.val → T ′.sub → T ′.val → T.val → E′.sub → E 1 ′.sub
num 1 .val → F 1 .val → T 1 ′.sub → T 1 ′.val → T 1 .val → E′ 1 .sub
E′ 1 .sub → E′ 1 .val → E′.val → E.val
Draw the parse tree for expression 3*5+7+8 and the corresponding attribute flow graph. Is it possible to construct an expression which will result in a cyclic attribute flow graph?