Midterm Exam with Answer Key - Introduction to Compilers | CMSC 430, Exams of Computer Science

Material Type: Exam; Professor: Zelkowitz; Class: INTRO TO COMPILERS; Subject: Computer Science; University: University of Maryland; Term: Spring 2004;

Typology: Exams

Pre 2010

Uploaded on 02/13/2009

koofers-user-mcy-1
koofers-user-mcy-1 🇺🇸

5

(1)

9 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC 430 Midterm Answers March 18, 2004
1
Answer all questions in the exam book. You may keep the
exam questions after you are done.
1. [56] Consider the following grammar G:
1. S X a
2. X Y b
3. X Y Z c
4. Y d
5. Z b
For each of the following grammar classes, if G is of that class,
give the appropriate parsing table. If it is not of that class, fully
explain why it isn’t.
(a) LL(0)
(b) LL(1)
(c) LR(0)
(d) LR(1)
(e) SLR(1)
(f) LALR(1)
(g) General precedence
(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]
X 1
Y 2
d3
S1 SX.a] a4
S2 [XY.b]
[XY.Zc]
[Z.b]
b5
Z6
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]
X 1
Y 2
d3
S1 SX.a,] a4
S2 [XY.b,a]
[XY.Zc,a]
[Z.b,c]
b5
Z6
S3 [Yd.,b] Reduce 4
S4 SXa.,] Reduce 1
S5 [XYb.,a]
[Zb.,c]
a Reduce 2
c Reduce 5
S6 [XYZ.c,a] c7
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]
X 1
Y 2
d3
S1 SX.a] a4
S2 [XY.b]
[XY.Zc]
[Z.b]
b5
Z6
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] c7
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.
2. [8] Is the language given by the grammar in problem
above LL(1)? Prove your answer.
Yes LL(1). Grammar only generates 2 strings: dba and
dbca. Write grammar as follows:
1. S dbX
2. Xa
3. X ca
Language generated by this grammar is same as original
grammar and it is LL(1) since
First(a) = {a}, First(ca)={c} and
First(a) First(ca) = Ø
3. [12] For the regular expression
(00)* (0 | 1)* (11)*
(a) Give the non-deterministic FSA that accepts the same set.
pf3

Partial preview of the text

Download Midterm Exam with Answer Key - Introduction to Compilers | CMSC 430 and more Exams Computer Science in PDF only on Docsity!

Answer all questions in the exam book. You may keep the exam questions after you are done.

  1. [56] Consider the following grammar G:
    1. S  X a ⊥
    2. X  Y b
    3. X  Y Z c
    4. Y  d
    5. Z  b For each of the following grammar classes, if G is of that class, give the appropriate parsing table. If it is not of that class, fully explain why it isn’t. (a) LL(0) (b) LL(1) (c) LR(0) (d) LR(1) (e) SLR(1) (f) LALR(1) (g) General precedence

(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]

X  1

Y 2

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]

X  1

Y 2

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]

X  1

Y 2

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.

  1. [8] Is the language given by the grammar in problem above LL(1)? Prove your answer.

Yes LL(1). Grammar only generates 2 strings: dba⊥ and dbca⊥. Write grammar as follows:

  1. S  dbX ⊥
  2. Xa
  3. X  ca Language generated by this grammar is same as original grammar and it is LL(1) since First(a) = {a}, First(ca)={c} and First(a)  First(ca) = Ø
  4. [12] For the regular expression (00)* (0 | 1)* (11)* (a) Give the non-deterministic FSA that accepts the same set.

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

  1. S   | 0S | 1S 4[8] Show that the language AnB2n^ for n>0 is LR(k). What is K?

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.

  1. [12] Answer each of the following: (a) Assuming the usual grammar for expressions, give the Polish Postfix for the expression: (3+4(2+7)+85)

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