0% found this document useful (0 votes)
229 views4 pages

Homework 3 Solutions Problem 4.35 (10 Points)

This document provides solutions to homework problems for a compilers course. Problem 4.35 asks students to construct SLR and LALR parsing tables for a grammar. It is shown that the grammar is SLR but not LALR due to reduce/reduce conflicts. Problem 4.37 shows an LL(1) grammar that is not SLR(1) due to non-disjoint lookahead sets causing shift/reduce conflicts. Problem 4.39 shows a grammar that is LALR(1) but not SLR(1) due to a shift/reduce conflict. Problem 4.40 shows a grammar that is LR(1) but not LALR(1) due to states merging causing a reduce/reduce

Uploaded by

jcsekhar9
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PS, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
229 views4 pages

Homework 3 Solutions Problem 4.35 (10 Points)

This document provides solutions to homework problems for a compilers course. Problem 4.35 asks students to construct SLR and LALR parsing tables for a grammar. It is shown that the grammar is SLR but not LALR due to reduce/reduce conflicts. Problem 4.37 shows an LL(1) grammar that is not SLR(1) due to non-disjoint lookahead sets causing shift/reduce conflicts. Problem 4.39 shows a grammar that is LALR(1) but not SLR(1) due to a shift/reduce conflict. Problem 4.40 shows a grammar that is LR(1) but not LALR(1) due to states merging causing a reduce/reduce

Uploaded by

jcsekhar9
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PS, PDF, TXT or read online on Scribd
You are on page 1/ 4

CS 412/CS413 Spring 1997

Homework 3 Solutions

Intro to Compilers and Translators

Handout March 14, 1997

Problem 4.35 (10 Points)


Consider the following grammar E !E+T jT T ! TF j F F !F jajb a) Construct the SLR parsing table for the augmented grammar. Construct the sets of LR(0) items: I0 : E 0 ! :E I5 : E ! E + :T E ! :E + T T ! :TF E ! :T T ! :F T ! :TF T ! :F T ! :F T ! :a F ! :F T ! :b F ! :a F ! :b I6 : E ! E + T: T ! T:F I1 : E ! T: T ! :F E ! T:F T ! :a E ! :F T ! :b E ! :a E ! :b I7 : T ! TF: F ! F: I2 : T ! F: F ! F: I8 : E 0 ! E: E ! E: + T I3 : F ! a: I9 : F ! F : I4 : F ! b: Now construct the SLR parsing table: FOL1 (E ) = f+; $g, FOL1(T ) = f+; a; b; $g, FOL1 (F ) = f+; a; b; ; $g
State

0 1 2 3 4 5 6 7 8 9

+ r(E ! T:) r(T ! F:) r(F ! a:) r(F ! b:)

* s9 r(F ! a:) r(F ! b:)

r(E ! E + T:) r(T ! TF:) s9 s5 r(F ! F :) r(F ! F :) r(F ! F :) r(F ! F :) 1

a b $ E T F s3 s4 8 1 2 s3 s4 r(E ! T:) 7 r(T ! F:) r(T ! F:) r(T ! F:) r(F ! a:) r(F ! a:) r(F ! a:) r(F ! b:) r(F ! b:) r(F ! b:) s3 s4 6 2 s3 s4 r(E ! E + T:) 7 r(T ! TF:) r(T ! TF:) r(T ! TF:)
r(F ! F :)
accept

action

goto

b) Construct the LALR parsing table. Construct the sets of LR(1) items:

I0 : E 0 ! :E $ E ! :E + T $=+ E ! :T $=+ T ! :TF $= + =a=b T ! :F $= + =a=b F ! :F $= + =a=b= F ! :a $= + =a=b= F ! :b $= + =a=b= I1 : E ! T: $=+ E ! T:F $= + =a=b E ! :F $= + =a=b= E ! :a $= + =a=b= E ! :b $= + =a=b= I2 : T ! F: $= + =a=b F ! F: $= + =a=b= I3 : F ! a: $= + =a=b= I4 : F ! b: $= + =a=b=
State

I5 : E ! E + :T $=+ T ! :TF $= + =a=b T ! :F $= + =a=b T ! :F $= + =a=b= T ! :a $= + =a=b= T ! :b $= + =a=b= I6 : E ! E + T: $=+ T ! T:F $= + =a=b T ! :F $= + =a=b= T ! :a $= + =a=b= T ! :b $= + =a=b= I7 : T ! TF: $= + =a=b F ! F: $= + =a=b= I8 : E 0 ! E: $ E ! E: + T $=+ I9 : F ! F : $= + =a=b=

Now construct the LALR parsing table: 0 1 2 3 4 5 6 7 8 9 + r(E ! T:) r(T ! F:) r(F ! a:) r(F ! b:) s9 r(F ! a:) r(F ! b:)

r(E ! E + T:) r(T ! TF:) s9 s5 r(F ! F :) r(F ! F :) r(F ! F :) r(F ! F :)

a b $ E T F s3 s4 8 1 2 s3 s4 r(E ! T:) 7 r(T ! F:) r(T ! F:) r(T ! F:) r(F ! a:) r(F ! a:) r(F ! a:) r(F ! b:) r(F ! b:) r(F ! b:) s3 s4 6 2 s3 s4 r(E ! E + T:) 7 r(T ! TF:) r(T ! TF:) r(T ! TF:)
r(F ! F :)
accept

action

goto

Problem 4.37 (5 Points)


a) Show that the following grammar S ! AaAb j BbBa A! B! is LL(1) but not SLR(1). If we construct the rst set of LR(0) items we get:

I0 : S ! :AaAb S ! :BbBa A!: B!:


2

But the FOL1 (A) = fa; bg and FOL1 (B ) = fa; bg. Since the lookahead sets are not disjoint there is a reduce/reduce con ict and the grammar is not SLR(1). On the other hand it is LL(1) because FIRST1(AaBb) 1 FOL1 (S ) = fag FIRST1(BbAa) 1 FOL1 (S ) = fbg and the intersection of fag and fbg is empty b) Show that every LL(1) grammar is an LR(1) grammar. (Optional) If you're interested there is a proof that every LL(k) grammar is LR(k) in Chapman, Nigel P. ,LR Parsing Theory and Practice, Cambridge University Press 1987 p. 74

Problem 4.39 (5 Points)


Show that the following grammar S ! Aa j bAc j dc j bda A!d is LALR(1) but not SLR(1). Construct the sets of LR(1) items for the augmented grammar: I0 : S 0 ! :S $ I4 : S ! bd:a $ S ! :Aa $ A ! d: c S ! :bAc $ S ! :dc $ I5 : S ! bda: $ S ! :bda $ A ! :d a I6 : S ! bA:c $

I1 : S ! d:c $ A ! d: a I2 : S ! dc: $ I3 : S ! b:Ac $ S ! b:da $ A ! :d c


And the LALR parsing table:
State

I7 : S ! bAc: $ I8 : S ! A:a $ I9 : S ! Aa: $ I10: S 0 ! S: $

0 1 r(A ! d) s2 2 r(S ! dc) 3 s4 6 4 s5 r(A ! d) 5 r(S ! bda) 6 s7 7 r(S ! bAc) 8 s9 9 r(S ! Aa) 10 accept The grammar is LALR(1) since there are no parsing action con icts in the table. The grammar is not SLR(1) because the following state in the LR(0) machine: I1 : S ! d:c A ! d: 3

b s3

action

d s1

S A 10 8

goto

has a shift/reduce con ict since FOL1 (A) = fa; cg.

Problem 4.40 (5 Points)


Show the the following grammar S ! Aa j bAc j Bc j bBa A!d B!d is LR(1) but not LALR(1). First construct the sets of LR(1) items: I0 : S 0 ! :S $ I5: S ! B:c $ S ! :Aa $ S ! :bAc $ I6: S ! Bc: $ S ! :Bc $ S ! :bBa $ I7: S ! bB:a $ A ! :d a B ! :d c I8: S ! bBa: $

I1 : S 0 ! S: $ I2 : A ! d: a B ! d: c I3 : S ! A:a $ I4 : S ! b:Ac $ S ! b:Ba $ B ! :d a A ! :d c


State

I9: S ! Aa: $ I10: B ! d: a A ! d: c I11: S ! bA:c $ I12: S ! bAc: $

Now construct the LR parsing table:

0 1 accept 2 r(A ! d) r(B ! d) 3 s9 4 s10 11 7 5 s6 6 r(S ! Bc) 7 s8 8 r(S ! bBa) 9 r(S ! Aa) 10 r(B ! d) r(A ! d) 11 s12 12 r(S ! bAc) The grammar is LR(1) since there are no parsing action con icts in the table. The grammar is not LALR(1) because when we merge state 2 and state 10 we get: I210: A ! d: a=c B ! d: a=c which has a reduce/reduce con ict. 4

b s4

action

d s2

S A B 1 3 5

goto

You might also like