

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; Professor: Zelkowitz; Class: INTRO TO COMPILERS; Subject: Computer Science; University: University of Maryland; Term: Spring 2004;
Typology: Exams
1 / 3
This page cannot be seen from the preview
Don't miss anything!


Answer all questions in the exam book. You may keep the exam questions after you are done.
(a) An LL(0) grammar generates a single string. This grammar generates 2 strings so can’t be LL(0). (b) First(Yb) = First(Yzc) = {d} so can’t be LL(1). (c) Try to generate LR(0) table first: S0 [S.Xa] [X.Yb] [X.YZc] [Y.d]
d 3
S1 SX.a] a 4 S2 [XY.b] [XY.Zc] [Z.b]
b 5 Z 6
S3 [Yd.] Reduce 4 S4 SXa.] Reduce 1 S5 [XYb.] [Zb.]
Reduce – reduce conflict so not LR(0).
(d) LR(1) S0 [S.Xa,] [X.Yb,a] [X.YZc,a] [Y.d,b]
d 3
S1 SX.a,] a 4 S2 [XY.b,a] [XY.Zc,a] [Z.b,c]
b 5 Z 6
S3 [Yd.,b] Reduce 4 S4 SXa.,] Reduce 1 S5 [XYb.,a] [Zb.,c]
a Reduce 2 c Reduce 5 S6 [XYZ.c,a] c 7 S7 [XYZc.,a] Reduce 3
No conflicts so LR(1).
(e) SLR(1) Copy over table from (c) -- LR(0) S0 [S.Xa] [X.Yb] [X.YZc] [Y.d]
d 3
S1 SX.a] a 4 S2 [XY.b] [XY.Zc] [Z.b]
b 5 Z 6
S3 [Yd.] Reduce 4 S4 SXa.] Reduce 1 S5 [XYb.] [Zb.]
aReduce 2 cReduce 5 In S5: Follow(X) ={a} Follow(Z) = {c} Disjoint so fix up above table and continue S6 [XYZ.c] c 7 S7 [XYZc.] Reduce 3
No further conflicts so SLR(1)
(f) LALR(1) – ALL item lists in (e) (LR(1)) are disjoint, so grammar is LALR(1) by default.
(g) Generate precedence relations:
From rule 2: Y = b From rule 3: Y = Z Z = c Y < First(Z) = b Thus Y=b and Y<b. Precedence relations are not unique, so not precedence grammar.
Yes LL(1). Grammar only generates 2 strings: dba⊥ and dbca⊥. Write grammar as follows:
(b) Give the minimal state DFA that recognizes the same set.
NFADFA algorithm gives:
Which reduces to:
(c) Give a regular grammar that recognizes the same set.
A simple PDA recognizes this set. Push A on stack. When find first B, pop one A for every 2 Bs read. End of string and empty stack occur at same time.
K=0 here since no lookahead is needed.
(b) Show that AnB2nCn^ for n>0 is not LR(k) for any k.
No PDA can work, therefore it can’t be LR(K) for any K. To show this, a PDA must stack and match As to Bs and no
way to count the Cs or must stack As and Bs and match Bs to Cs and no way to count the As.
3427++85+
(b) If the grammar for an expression is given by the grammar: E E + T | E – T | E * T | E / T | T T number | ( E ) Give the parse tree for the expression in (a).
(c) Using the grammar in (b), give the Postfix for the expression in (a).
34+27+8+5
0 or 1
CDE CD
BCD
ACD
E
8 E
E T
7
E
T ( )
2
T
T T
4
3
E
T
( E )
E
E
T
T 5