Docsity

Attribute Grammar - Programming Language Translation - Quiz, Exercises of Programming Languages

Toggle sidebar
bg1
Sample Problems – Final Examination - Solutions
Selected Solutions:
Problem 1: As the initial step in the design of an SLR parser, construct the DFA that corresponds to
the LR(0) item sets for the grammar. Make sure that you clearly identify the items in each set.
S’ S
S A B y
A S y | x
B y S
I0 = closure([S’.S]) = { S’.S, S .A B, A .S y, A .x}
Now we need to compute GOTO on I0 on symbols S, A, and x.
GOTO(I0,S) = closure([S’S. , A S.y]} = { S’S. , A S.y}= I1
GOTO(I0,A) = closure([S A.B]) = { S A.B, B .y S} = I2
GOTO(I0,x) = closure([A x.]) = { A x.} = I3
Now we’re done with I0, so we move on to I1. We need to compute GOTO on
I1 for symbol y, the only symbol to the right of the “.” in I1.
GOTO(I1,y) = closure([A S y.]) = { A S y.} = I4
This finishes I1, and we move on to I2. We need GOTO for symbols B and
y.
GOTO(I2,B)=closure([S A B. ])={ S A B. } = I5
GOTO(I2,y)=closure([B y. S])={B y. S, S.A B, A.Sy, A.x}=I6
Now we’re done with I2. Neither I3 nor I4 has anything to offer, since
neither has any symbols to the right of the dot. So we move on to I5,
and compute GOTO for symbol y.
Next we compute GOTO on I6 for S, A, and x.
GOTO(I6,S)=closure([B y S., A S.y]) = { B y S., A S.y} = I7
GOTO(I6,A)=closure([S A.B ])={ S A.B , B .y S} = I2
(notice this gives us an item set we’ve already seen before)
GOTO(I6,x)=closure([A x.])={ A x.} = I3
I7 has nothing to contribute, so we move on to I8 on symbol y.
GOTO(I8,y) = closure([A S y.])={ A S y.} = I4
pf3
pf4
pf5

Partial preview of the text

Download Attribute Grammar - Programming Language Translation - Quiz and more Exercises Programming Languages in PDF only on Docsity!

Selected Solutions:

Problem 1: the LR(0) item sets for the grammar. Make sure that you clearly identify the items in each set. As the initial step in the design of an SLR parser, construct the DFA that corresponds to

S’  S

SA B y AS y | x By S I 0 = closure([S’.S]) = { S’.S, S .A B, A .S y, A .x}

Now we need to compute GOTO on I0 on symbols S, A, and x.

GOTO(I 0 ,S) = closure([S’S. , A S.y]} = { S’S. , A S.y}= I (^1) GOTO(I 0 ,A) = closure([S  A.B]) = { S  A.B, B  .y S} = I (^2) GOTO(I 0 ,x) = closure([A x.]) = { A x.} = I (^3) NowI we’re done with I 0 , so we move on to I 1. We need to compute GOTO on 1 for^ symbol^ y,^ the^ only^ symbol^ to^ the^ right^ of^ the^ “.”^ in^ I^1. GOTO(I 1 ,y) = closure([A S y.]) = { A S y.} = I (^4) Thisy. finishes I 1 , and we move on to I 2. We need GOTO for symbols B and GOTO(I 2 ,B)=closure([S  A B. ])={ S  A B. } = I (^5) GOTO(I 2 ,y)=closure([B  y. S])={B y. S, S.A B, A.Sy, A.x}=I (^6) Nowneither we’re has done any with symbols I 2. (^) toNeither the right I 3 nor of Ithe 4 has dot. anything So we to move offer, on tosince I and compute GOTO for symbol y.^5 , Next we compute GOTO on I 6 for S, A, and x. GOTO(I 6 ,S)=closure([B y S., A S.y]) = { B y S., A S.y} = I (^7) GOTO(I(notice 6 ,A)=closure([S this gives us an A.Bitem ])={ set Swe’ve  A.B already , B  seen.y S} before) = I (^2) GOTO(I 6 ,x)=closure([A x.])={ A x.} = I (^3) I 7 has nothing to contribute, so we move on to I 8 on symbol y. GOTO(I 8 ,y) = closure([A S y.])={ A S y.} = I (^4)

From these we can depict the DFA, as follows:

Is the grammar SLR? Why or why not? Reason out your answer without constructing the entire parsetable!

The grammar is not SLR, since there is a shift / reduce conflict from stateTogether 8 on with symbol the y.fact This that comes from the two items AS.y and ByS.

FOLLOW(S) = {$,y} FOLLOW(A) = {y} FOLLOW(B) = {$,y}

SLR (0) Item Sets:

I0: S’S  (^) .AB.S AA  .Sy.x

I1: S’A  (^) S.yS.

I2: SB  (^) .ySA.B

I3: Ax. I4: ASy. I5: AAB. I6: By.S

SA  .AB.Sy A.x I7: ByS. AS.y LR(1) ITEM SETS I0: S’S  .S, $.AB, $/y AA  .Sy, y.x, y I1: S’A  S., $S.y, y I2: SA.B, $/y

B.yS, $/y I3: Ax., y I4: ASy., y I5: AAB., $/y I6: By.S, $/y SA  .AB, $/y.Sy, y A.x, y I7: BA  yS., $/yS.y, y

I 0

I 6

I 5

I 2

I 3

I 7

I 4

I 1

B

x

x

y

y

y

A

A

S

S

GOTO(I 3 ,f) = closure([Df.,e],[Ef.,d])={[Df.,e],[Ef.,d]}= I (^9) This finishes GOTO on I 3. Using I 4 we only have one, for symbol d. GOTO(I 4 ,d) = closure([SdDd.,$]) = {[SdDd.,$]} = I (^10) Similarly, on I 5 we have GOTO(I 5 ,d) = closure([SdEe.,$]) = {[SdEe.,$]} = I (^11) For I 6 there is nothing to do. For I 7 we calculate GOTO(I 7 ,e). GOTO(I 7 ,e) = closure([SeDe.,$]) = {[SeDe.,$]} = I (^12) A similar calculation for I 8 yields GOTO(I 8 ,e) = closure([SeEd.,$]) = {[SeEd.,$]} = I (^13) ThisDFA looksfinishes like all this: the LR(1)item sets since no other I (^) j contributes. The

Is the grammar LR(1)? Why or why not? Reason out your answer without constructing the entire

parse table!

The grammar is LR(1), since there are no shift / reduce conflicts or

reduce / reduce

conflicts. Ithey act on different input symbols. 6 and I 9 would be the candidates for R/R conflicts, but

I 0

I 7

I 6

I 5

I 2

I 3

I 8

I 4

e I 1

E

e

d

f

d e

E D D

S

I 10

I 11

I 13 I 12

I 9

f

d

SLR (0) Item Sets:` LR(1) Item Sets

I0: S’S  (^) .d D d.S SS  .e D e.d E e S.e E d I1: S’S. I2: SS  (^) d.E ed.D d DE  .f.f

I3: Se.D e SD  e.E d.f E.f I4: Sd D.d I5: Sd E.e I6: DE  f.f.

I7: Se D.e I8: Se E.d I9: Sd D d. I10: Sd E e. I11: Se D e. I12: Se E d.

I0: S’S  .S, $.d D d, $ SS  .e D e, $.d E e, $ S.e E d, $ I1: S’S. , $ I2: SS  d.D d , $d.E e, $ DE  .f, d.f, e I3: Se.D e , $ SD  e.E d, $.f, e E.f, d I4: Sd D.d, $ I5: Sd E.e, $ I6: DE  f., df., e I7: Se D.e, $ I8: Se E.d, $ I9: DE  f., ef., d I10: Sd D d. , $ I11: Sd E e. , $ I12: Se D e. , $ I13: Se E d. , $

Is the Grammar LR(1)?Why or Why Not? Which LR(1) sets areMerged for LALR(1)? Is the Grammar LALR(1)?Why or Why Not?

Problem 9:

B2, B4, B6, B7, B8, and B10 are unchanged. Other basic blocks as below:

B1 | i^ +------------+ := n; | | t8:= i4 ; |+------------+*

B3 | jmax := 1; |^ +------------+ | j := 2;| t1 := j-2 || | t2 :=t14; |+------------+*

B5 | t2 :=t2+4; |+------------+ | t3:=x[t2]; || Rest of B5 | | from t4| down is || | Unchanged+------------+ |

B9 | t8 :=t8-4;+--------------+ | | temp:=x[t8]; || Rest of B9 | | from t10| down is || | Unchanged+--------------+ |