Академический Документы
Профессиональный Документы
Культура Документы
I1: {S ′ → S·}
I3: {S → Y ·}
I5: {X → c·}
I7: {X → bY ·}
I8: {Y → X·}
I9: {S → XaY ·}
1 2
0 S′ → S 0 S′ → S
1, 2 S → XaY | Y 1, 2 S → XaY | Y
3, 4 X → bY | c 3, 4 X → bY | c
5 Y →X 5 Y →X
′ ′
I0: {S → ·S, S → ·XaY, S → ·Y, X → ·bY, X → ·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·} I1: {S ′ → S·} I2: {S → X · aY, Y → X·}
I3: {S → Y ·} I4: {X → b · Y, Y → ·X, X → ·bY, X → ·c} 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} I5: {X → c·} I6: {S → Xa·Y, Y → ·X, X → ·bY, X → ·c}
I7: {X → bY ·} I8: {Y → X·} I9: {S → XaY ·} I7: {X → bY ·} I8: {Y → X·} I9: {S → XaY ·}
FOLLOW(S) = {$} FOLLOW(X) = {a, $} FOLLOW(S) = {$} FOLLOW(X) = {a, $}
FOLLOW(Y ) = {a, $} FOLLOW(Y ) = {a, $}
S ⇒ XaY ⇒ bY aY So, what viable prefixes take the parser to state 2? X only!
To get an a we must use production S → XaY . Then to get Whenever the parser is in state 2 with lookahead a, the stack
a Y in front of that a, we must produce a Y from the X, contains X (that is, 0X2), and reduction by 5 leaves us with
which we can do only with production X → bY . (So we can’t stack Y (that is, 0Y 3) — in which case the stack plus
get substring Y a without a leading b.) lookahead is not a prefix of any right-sentential form.
(Notice that in this case we showed that Ba is not a prefix of So in state 2, with lookahead a, we should always shift and go
any sentential form — of course it follows that it is not a to state 6.
prefix of any right-sentential form.)
The SLR parsing table isn’t adequate. . .
But maybe there are still cases in which, in state 2 on The problem is with the use of FOLLOW sets to decide
lookahed a, the parser should reduce with production 5. . . when to reduce.
7 8
Canonical LR parsing tables, LR(1) items LR(1) closure function
So we add LR(1) items [S → ·XaY , $] and [S → ·Y, $]. What is closure({[X → b · Y, a|$]})? (stands for 2 LR(1) items)
Now consider X → ·bY and X → ·c. Here β = aY , so Consider LR(1) items with first component Y → ·X. In this
FIRST(β$) = {a}. Thus we add items [X → ·bY, a] and case, β = ǫ. Since FIRST(β$) = {$}, we add [Y → ·X, $],
Now consider Y → ·X, with β = ǫ. Since Now consider X → ·bY and X → ·c. Here again β = ǫ, so
FIRST(β$) = {$}, we add item [Y → ·X, $]. FIRST(β$) = {$} and FIRST(βa) = {a}. Thus we add
[X → ·bY, a|$] and [X → ·c, a|$].
Now consider X → ·bY and X → ·c, with β = ǫ. Since
FIRST(β$) = {$}, we add items [X → ·bY, $] and What is closure({[S → Xa · Y, $]})?
[X → ·c, $].
11 12
LR(1) goto function Construction of sets of LR(1) items
Definition For any set I of LR(1) items and grammar Here is the algorithm for construction of the canonical
symbol X, goto(I, X) is the closure of the set of all LR(1) collection of sets of LR(1) items for an augmented grammar:
items
C := { closure({[S ′ → ·S, $]}) };
[A → αX · β, a] repeat
s.t.
for each set of LR(1) items I ∈ C and each grammar symbol X
[A → α · Xβ, a] ∈ I . s.t. goto(I, X) is not empty and is not already in C do
add goto(I, X) to C
Example until no more sets of items can be added to C in this way
I0: {[S ′ → ·S, $], [S → ·XaY , $], [S → ·Y, $]
So this is the same construction we used for LR(0) items —
[X → ·bY, a|$], [X → ·c, a|$], [Y → ·X, $]}
but now for LR(1) items, with the LR(1) versions of closure
′
goto(I0, S) = closure({[S → S·, $]}) and goto, and the initial LR(1) item.
′
= {[S → S·, $]}
goto(I0, X) = closure({[S → X · aY , $], [Y → X·, $]})
= {[S → X · aY , $], [Y → X·, $]})
goto(I0, Y ) = closure({[S → Y ·, $]})
= {[S → Y · , $]}
goto(I0, b) = closure({[X → b · Y, a|$]})
= {[X → b · Y, a|$], [Y → ·X, a|$],
[X → ·bY, a|$], [X → ·c, a|$]}
goto(I0, c) = closure({[X → c · , a|$]})
= {[X → c · , a|$]}
13 14
Construction of canonical LR(1) parsing table
a) If goto(k, a) = j,
then “shift and go to state j” is applicable.
15