Вы находитесь на странице: 1из 8

S → XaY | Y

Let’s try building an SLR parsing table for another simple


X → bY | c
grammar:
Y → X
S → XaY | Y
X → bY | c Canonical collection:
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
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, $}

action goto action goto


STATE a b c $ S X Y STATE a b c $ S X Y
0 0 s4 s5 1 2 3
1 1 acc
2 2 s6,r5? r5
3 3 r2
4 4 s4 s5 8 7
5 5 r4 r4
6 6 s4 s5 8 9
7 7 r3 r3
8 8 r5 r5
9 9 r1
3 4
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
action goto
action goto
STATE a b c $ S X Y
STATE a b c $ S X Y
0 s4 s5 1 2 3
0 s4 s5 1 2 3
1 acc
1 acc
2 s6,r5? r5
2 s6,r5? r5
3 r2 r2
3 r2 r2
4 s4 s5 8 7
4 s4 s5 8 7
5 r4 r4
5 r4 r4
6 s4 s5 8 9
6 s4 s5 8 9
7 r3 r3
7 r3 r3
8 r5 r5
8 r5 r5
9 r1
9 r1

Let’s try a parse: Let’s try the alternative. . .


STACK INPUT ACTION
STACK INPUT ACTION
0 cac$ s5
0 cac$ s5
0c5 ac$ r4
0c5 ac$ r4
0X2 ac$ s6, not r5!
0X2 ac$ try r5, instead of s6
0X2a6 c$ s5
0Y3 ac$ r2
0X2a6c5 $ r4
0S1 ac$ error
0X2a6X8 $ r5
In LR parsing, the stack should always contain a viable prefix,
0X2a6Y9 $ r1
and the stack plus lookahead (unless it’s $) should be a prefix
0S1 $ acc
of a right-sentential form. Otherwise the parse will fail. . .
5 6
0 S′ → S
1, 2 S → XaY | Y
0 S′ → S
3, 4 X → bY | c
1, 2 S → XaY | Y 5 Y →X
3, 4 X → bY | c FOLLOW(Y ) = {a, $}
5 Y →X
I0: {S ′ → ·S, S → ·XaY, S → ·Y, X → ·bY, X → ·c, Y → ·X}
I1: {S ′ → S·} I2: {S → X · aY, Y → X·}
If we have X on the stack, with lookahead a, we had better I3: {S → Y ·} I4: {X → b · Y, Y → ·X, X → ·bY, X → ·c}
not reduce X to Y : Y a is not a prefix of a right-sentential I5: {X → c·} I6 : {S → Xa · Y, Y → ·X, X → ·bY, X → ·c}
form, despite the fact that a ∈ FOLLOW(Y ). I7: {X → bY ·} I8: {Y → X·} I9: {S → XaY ·}

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

The grammar in the previous example is not ambiguous, and


Definition Given a set I of LR(1) items from an augmented
can be parsed by the LR method, if only we can construct a
grammar, closure(I) is the least set of items satisfying the
more adequate parsing table.
following two conditions:
We’ll look first at canonical LR parsing tables. Then LALR
1. I ⊆ closure(I)
parsing tables, which are smaller and weaker (and are what
yacc builds!). Both methods can handle the previous 2. If
examples. [X → α · Y β, a] ∈ closure(I)
and there is an LR(1) item
We begin by enriching the notion of an item, to better take
[Y → ·γ, b]
into account lookahead tokens.
s.t.
Definition An LR(1) item is a pair of an LR(0) item and a
b ∈ FIRST(βa)
token or $, typically written
then
[X → α · β, a] .
[Y → ·γ, b] ∈ closure(I) .

Intuitively, the lookahead a has no effect if β 6= ǫ, but if β = ǫ,


then [X → α · β, a] calls for reduction by X → αβ if the Claim: FIRST(βa) ⊆ FOLLOW(Y ). (As elsewhere, I
lookahead token is a. suspect this is not true, but it is suggestive. Why not true?
Because the definition of FOLLOW is about existence of
Thus, we will reduce by a production X → α only on those
sentential forms, but this definition is not.)
lookaheads a for which there is an item [X → α·, a].
The set of such a’s is always a subset of FOLLOW(X).
9 10
1. I ⊆ closure(I) 1. I ⊆ closure(I)
2. If [X → α · Y β, a] ∈ closure(I) and there is an LR(1) 2. If [X → α · Y β, a] ∈ closure(I) and there is an LR(1)
item [Y → ·γ, b] s.t. b ∈ FIRST(βa), then item [Y → ·γ, b] s.t. b ∈ FIRST(βa), then
[Y → ·γ, b] ∈ closure(I) . [Y → ·γ, b] ∈ closure(I) .
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

Example What is closure({[S ′ → ·S, $]})?


Example What is
So we consider LR(1) items with first components S → ·XaY closure({[S → X · aY , $], [Y → X·, $]})?
and S → ·Y . In this case, β = ǫ, so FIRST(β$) = {$}.

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, $],

[X → ·c, a]. and since FIRST(βa) = {a}, we add [Y → ·X, a].

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

1. Construct the canonical collection C = {I0, . . . , In } of sets


of LR(0) items. Each element of C is a “state”, and we’ll often
write k to refer to the state Ik .

2. The applicable parsing actions for state k and lookahead a


are determined as follows:

a) If goto(k, a) = j,
then “shift and go to state j” is applicable.

b) If [B → α·, a] ∈ k and B is not S ′ ,


then “reduce by production B → α” is applicable.

c) If [S ′ → S·, $] ∈ k and a = $, then “accept” is applicable.

If any conflicting actions are generated in step 2, the grammar


is “not LR(1)”.

3. For all states k and variables A, if goto(k, A) is nonempty,


then goto(k, A) is the corresponding goto entry.

4. All remaining undefined entries have action “error”.

5. If Ik = closure({[S ′ → ·S, $]}), then k is the initial state of


the parser.

15

Вам также может понравиться