Let’s try building an SLR parsing table for another simple grammar: S → XaY | Y X → bY | c Y → X Canonical collection

:

S → XaY | Y X → bY | c Y → X

I0: {S ′ → ·S, S → ·XaY, S → ·Y, X → ·bY, X → ·c, Y → ·X} I1: {S ′ → S·} I2: {S → X · aY, Y → X·} I3: {S → Y ·} I4: {X → b · Y, Y → ·X, X → ·bY, X → ·c} I5: {X → c·} I6: {S → Xa · Y, Y → ·X, X → ·bY, X → ·c} I7: {X → bY ·} I8: {Y → X·} I9: {S → XaY ·}

1

2

2 3. X → ·bY. Y → ·X. X → ·c. S → ·Y. S → ·XaY. Y → X·} I3: {S → Y ·} I4: {X → b · Y. Y → X·} I0: {S → ·S. Y → ·X} I3: {S → Y ·} I4: {X → b · Y. S → ·XaY. X → ·c} I7: {X → bY ·} FOLLOW(S) = {$} FOLLOW(Y ) = {a. 2 3. X → ·bY.0 1. 4 5 I1: {S ′ → S·} S′ → S S → XaY | Y X → bY | c Y →X I2: {S → X · aY. X → ·c. Y → ·X. X → ·c} I5: {X → c·} I6: {S → Xa·Y. Y → ·X. X → ·bY. X → ·bY. $} STATE 0 1 2 3 4 5 6 7 8 9 a action b c $ S goto X Y STATE a 0 1 2 s6.r5? 3 4 s4 5 r4 6 s4 7 r3 8 r5 9 action b c s4 s5 $ acc r5 r2 S 1 goto X 2 Y 3 s5 r4 s5 r3 r5 r1 4 8 8 7 9 3 . X → ·bY. X → ·bY. S → ·Y. Y → ·X} I1: {S → S·} ′ I0: {S → ·S. $} I8: {Y → X·} I9: {S → XaY ·} FOLLOW(X) = {a. 4 5 ′ S′ → S S → XaY | Y X → bY | c Y →X ′ 0 1. X → ·c} I5: {X → c·} I6: {S → Xa·Y. X → ·c} I7: {X → bY ·} FOLLOW(S) = {$} FOLLOW(Y ) = {a. $} I8: {Y → X·} I9: {S → XaY ·} FOLLOW(X) = {a. $} I2: {S → X · aY. Y → ·X.

. 2 3. 4 5 S′ → S S → XaY | Y X → bY | c Y →X goto X 2 action b c s4 s5 STATE 0 1 2 3 4 5 6 7 8 9 a action b c s4 s5 $ acc r5 r2 S 1 Y 3 s6. ACTION s5 r4 s6. not r5! s5 r4 r5 r1 acc STACK 0 0c5 0X2 0Y3 0S1 INPUT cac$ ac$ ac$ ac$ ac$ ACTION s5 r4 try r5. Otherwise the parse will fail. . the stack should always contain a viable prefix. 2 3. .0 1. instead of s6 r2 error In LR parsing. 6 . and the stack plus lookahead (unless it’s $) should be a prefix of a right-sentential form.r5? r2 r4 r3 r5 s6. 4 5 STATE 0 1 2 3 4 5 6 7 8 9 a S′ → S S → XaY | Y X → bY | c Y →X $ acc r5 r2 s4 s5 r4 s4 s5 r3 r5 r1 8 9 8 7 S 1 goto X 2 Y 3 0 1. .r5? r2 s4 r4 s4 r3 r5 s5 s5 8 r4 8 r3 r5 r1 7 9 Let’s try a parse: STACK 0 0c5 0X2 0X2a6 0X2a6c5 0X2a6X8 0X2a6Y9 0S1 INPUT cac$ ac$ ac$ c$ $ $ $ $ 5 Let’s try the alternative.

. S ⇒ XaY ⇒ bY aY To get an a we must use production S → XaY . we had better not reduce X to Y : Y a is not a prefix of a right-sentential form. X → ·bY. and reduction by 5 leaves us with stack Y (that is. the stack contains X (that is.0 S′ → S S → XaY | Y X → bY | c Y →X 0 1. Y → ·X} I1: {S ′ → S·} I3: {S → Y ·} I5: {X → c·} I7: {X → bY ·} I2: {S → X · aY. what viable prefixes take the parser to state 2? X only! Whenever the parser is in state 2 with lookahead a. X → ·bY. X → ·c} I8: {Y → X·} I9: {S → XaY ·} If we have X on the stack. 7 8 . . Y → ·X. So in state 2. X → ·bY. the parser should reduce with production 5. Y → ·X. The problem is with the use of FOLLOW sets to decide when to reduce. with lookahead a. Y → X·} I4: {X → b · Y. 2 3. 0X2). 2 3. The SLR parsing table isn’t adequate.) So. $} 1. . X → ·c. X → ·c} I6 : {S → Xa · Y. 0Y 3) — in which case the stack plus lookahead is not a prefix of any right-sentential form. (So we can’t get substring Y a without a leading b. . 4 5 S′ → S S → XaY | Y X → bY | c Y →X FOLLOW(Y ) = {a. S → ·Y. which we can do only with production X → bY . we must produce a Y from the X. 4 5 I0: {S ′ → ·S. with lookahead a. Then to get a Y in front of that a. in state 2 on lookahed a. we should always shift and go to state 6.) (Notice that in this case we showed that Ba is not a prefix of any sentential form — of course it follows that it is not a prefix of any right-sentential form. But maybe there are still cases in which. S → ·XaY. despite the fact that a ∈ FOLLOW(Y ).

If [X → α · Y β. LR(1) closure function Definition Given a set I of LR(1) items from an augmented grammar. then [X → α · β. a] . which are smaller and weaker (and are what yacc builds!). Definition An LR(1) item is a pair of an LR(0) item and a token or $. I suspect this is not true. Both methods can handle the previous examples.t. a] calls for reduction by X → αβ if the lookahead token is a.) . and can be parsed by the LR method. Why not true? Because the definition of FOLLOW is about existence of sentential forms. but this definition is not. we will reduce by a production X → α only on those lookaheads a for which there is an item [X → α·. the lookahead a has no effect if β = ǫ. closure(I) is the least set of items satisfying the following two conditions: 1. The set of such a’s is always a subset of FOLLOW(X). Intuitively. We’ll look first at canonical LR parsing tables. LR(1) items The grammar in the previous example is not ambiguous. but it is suggestive. a] ∈ closure(I) and there is an LR(1) item [Y → ·γ. if only we can construct a more adequate parsing table.Canonical LR parsing tables. 9 10 Claim: FIRST(βa) ⊆ FOLLOW(Y ). then s. b] ∈ closure(I) . to better take into account lookahead tokens. but if β = ǫ. I ⊆ closure(I) 2. Then LALR parsing tables. Thus. (As elsewhere. b] b ∈ FIRST(βa) [Y → ·γ. typically written [X → α · β. We begin by enriching the notion of an item. a].

Now consider X → ·bY and X → ·c. 4 5 S′ → S S → XaY | Y X → bY | c Y →X 1. 0 1. I ⊆ closure(I) 2. a]. 0 1. I ⊆ closure(I) 2. $]. a] ∈ closure(I) and there is an LR(1) item [Y → ·γ. a] and [X → ·c. so FIRST(β$) = {a}. Since FIRST(β$) = {$}. Now consider X → ·bY and X → ·c. $]. Here again β = ǫ.t.t. we add item [Y → ·X. [Y → X·. we add [Y → ·X. b ∈ FIRST(βa). Now consider Y → ·X. 2 3. $]})? So we consider LR(1) items with first components S → ·XaY and S → ·Y . so FIRST(β$) = {$}. $] and [X → ·c. If [X → α · Y β. In this case. $]})? What is closure({[X → b · Y. with β = ǫ. Now consider X → ·bY and X → ·c. Since FIRST(β$) = {$}. a|$] and [X → ·c. 11 12 closure({[S → X · aY . so FIRST(β$) = {$} and FIRST(βa) = {a}. then [Y → ·γ. Thus we add [X → ·bY. $]. 2 3. $]})? . In this case. If [X → α · Y β. b] ∈ closure(I) . $] and [S → ·Y. What is closure({[S → Xa · Y. b ∈ FIRST(βa). then [Y → ·γ. Here β = aY . and since FIRST(βa) = {a}. a|$]})? (stands for 2 LR(1) items) Consider LR(1) items with first component Y → ·X. β = ǫ. we add [Y → ·X. b] s. a]. Thus we add items [X → ·bY. So we add LR(1) items [S → ·XaY .1. with β = ǫ. β = ǫ. Since FIRST(β$) = {$}. $]. we add items [X → ·bY. b] s. a|$]. b] ∈ closure(I) . a] ∈ closure(I) and there is an LR(1) item [Y → ·γ. $]. 4 5 Example What is S′ → S S → XaY | Y X → bY | c Y →X Example What is closure({[S ′ → ·S.

$]}) }. a|$]. [X → ·c. with the LR(1) versions of closure and goto. [Y → ·X. a|$]}) = {[X → b · Y. $]. goto(I. X) is the closure of the set of all LR(1) items s. goto(I. repeat for each set of LR(1) items I ∈ C and each grammar symbol X s. S) = closure({[S → S·. $]} goto(I0. [A → αX · β. Example I0: {[S ′ → ·S. a|$]. 14 . a|$]. [S → ·Y.LR(1) goto function Definition For any set I of LR(1) items and grammar symbol X. [Y → X·. [X → ·c. $]}) = {[S → Y · . [Y → X·. a] ∈ I . [Y → ·X. $]. $]}) = {[S → X · aY . $]} goto(I0. $] [X → ·bY. c) = closure({[X → c · . a|$]} 13 ′ ′ Construction of sets of LR(1) items Here is the algorithm for construction of the canonical collection of sets of LR(1) items for an augmented grammar: C := { closure({[S ′ → ·S.t. $]}) goto(I0. a|$]}) = {[X → c · . $]. X) is not empty and is not already in C do add goto(I. b) = closure({[X → b · Y. $]} goto(I0. X) = closure({[S → X · aY . a|$]} goto(I0. a|$]. a|$]. $]. a] [A → α · Xβ. $]}) = {[S → S·. X) to C until no more sets of items can be added to C in this way So this is the same construction we used for LR(0) items — but now for LR(1) items. and the initial LR(1) item. [S → ·XaY .t. [X → ·bY. Y ) = closure({[S → Y ·.

15 . If Ik = closure({[S ′ → ·S. If any conflicting actions are generated in step 2. . the grammar is “not LR(1)”. then goto(k. A) is the corresponding goto entry. and we’ll often write k to refer to the state Ik . 5. Each element of C is a “state”. For all states k and variables A. All remaining undefined entries have action “error”. $] ∈ k and a = $. . a] ∈ k and B is not S ′ . In } of sets of LR(0) items. .Construction of canonical LR(1) parsing table 1. then k is the initial state of the parser. A) is nonempty. then “accept” is applicable. a) = j. 2. c) If [S ′ → S·. $]}). if goto(k. 4. The applicable parsing actions for state k and lookahead a are determined as follows: a) If goto(k. then “shift and go to state j” is applicable. b) If [B → α·. . then “reduce by production B → α” is applicable. 3. Construct the canonical collection C = {I0.

Sign up to vote on this title
UsefulNot useful