Академический Документы
Профессиональный Документы
Культура Документы
3 LR(1) Parsers
Compilers
(Procesadores
de Lenguaje)
Course 2007-2008
S
S0
S.S$
S .A
S .xb
A .aAb
A .B
B .x
S2
S A.
S3
x
a
Sacc
S S.$
This state
allows both
reduce and shift
S4
B
S5
S6
S x.b
B x.
A a.Ab
A .aAb
A .B
B .x
S xb.
b
A
A aA.b
B
x
AB.
S9
S7
S8
B x.
A aAb.
0 SS$
1 SA
2 Sxb
3 AaAb
4 AB
2
5 Bx
the DFA for the grammar has a double-circled node with one or more
outward arcs.
the LHS of the reducing production can be followed by any of the
labels of the outward arcs.
S1
S2
S
S0
S.S$
S .A
S .xb
A .aAb
A .B
B .x
S S.$
S A.
S6
S3
S x.b
B x.
x
a
S xb.
A a.Ab
A .aAb
A .B
B .x
S4
S5
AB.
S6/
r5
s3
S1
S S.$
S
S0
S.S$
S .A
S .xb
A .aAb
A .B
B .x
A
x
a
Sacc
S A.
S4
B
S5
S6
S x.b
B x.
A a.Ab
A .aAb
A .B
B .x
S
1
A B
2 5
r1
r5
7
S xb.
S9
S7
A aA.b
B
x
AB.
x $
s3
s8
s4 s4
r4
r4
s5
r2
s6
s9
s7
r5
r5
s8
r3
r3
s9
S2
S3
S8
B x.
A aAb.
0 SS$
1 SA
2 Sxb
3 AaAb
4 AB
4
5 Bx
We have a conflict because it seems that if the next token is b, either a shift or
reduce is possible.
In fact, with a following b, the reduce operation would not lead to a valid parse.
S.S$
S .A
S .xb
A .aAb
A .B
B .x
S A.
S3
x
a
S4
S6
S x.b
B x.
S xb.
A a.Ab
A .aAb
A .B
B .x
S5
AB.
Rather than using the general FOLLOW list for each nonterminal, LR(1) parsers
calculate which terminals can in fact follow the nonterminal GIVEN the current
state.
In producing the DFA for a grammar, each item is given with the token which
could follow it in that context:
S0
S.S { $ }
S .A {$}
S .xb {$}
A .aAb {$}
A .B {$}
B .x {$}
Rather than using the general FOLLOW list for each nonterminal, LR(1) parsers
calculate which terminals can in fact follow the nonterminal GIVEN the current
state.
In producing the DFA for a grammar, each item is given with the token which
could follow it in that context:
S0
S.S { $ }
S .A {$}
S .xb {$}
A .aAb {$}
A .B {$}
B .x {$}
Rather than using the general FOLLOW list for each nonterminal, they
calculate which terminal can in fact follow the nonterminal GIVEN the
current state.
In producing the DFA for a grammar, each item is given with the single
token which could follow it in that context.
Sacc
S S. {$}
S0
S.S { $ }
S .A {$}
S .xb {$}
A .aAb {$}
A .B {$}
B .x {$}
S
A
S1
S2
x
a
S3
S A. {$}
S5
S x.b {$}
B x. {$}
A a.Ab {$}
A .aAb {b}
A .B {b}
B .x {b}
S xb. {$}
Expansions of A
are followed by b
S4
A B . {$}
Now, when forming the parse table, we know exactly which terminal can
follow a nonterminal in a given state.
The true Follow set of B in state S2 is {$} not {$b}
The conflict in this case is thus avoided.
Sacc
S S. {$}
S0
S.S { $ }
S .A {$}
S .xb {$}
A .aAb {$}
A .B {$}
B .x {$}
S
A
S1
S A. {$}
S5
S2
S x.b {$}
B x. {$}
x
a
S3
S xb. {$}
A a.Ab {$}
A .aAb {b}
A .B {b}
B .x {b}
B
S4
A B . {$}
A side effect of associating followers with items is that we cannot merge two
states unless:
1. They share the same closure (as before)
2. Each item has the same Follower (new)
3. Thus, some of the state mergers from the previous example cannot happen
S
Sacc
1
S S. {$}
S0
S.S { $ }
S .A {$}
S .xb {$}
A .aAb {$}
A .B {$}
B .x {$}
S
A
S1
S2
x
a
S3
S A. {$}
S5
S x.b {$}
B x. {$}
A a.Ab {$}
A .aAb {b}
A .B {b}
B .x {b}
S4
A B . {$}
S xb. {$}
Cannot
Merge
S4
A a.Ab {b}
A .aAb {b}
A .B {b}
B .x {b}
10
S.S$ { }
S .A {$}
S .xb {$}
A .aAb {$}
A .B {$}
B .x {$}
S0
S1
S2
S A. {$}
S5
S x.b {$}
B x. {$}
S xb. {$}
S7
S4
A B . {b}
B x.{b}
B
S4
A B . {$}
S3
S6
A aA.b {$}
A aA.b {b}
S8
A a.Ab {b}
A .aAb {b}
A .B {b}
B .x {b}
S6
S3
A a.Ab {$}
A .aAb {b}
A .B {b}
B .x {b}
S8
A aAb. {$}
A aAb. {b}
11
S6
S1
S S.$
S
S0
S.S$
S .A
S .xb
A .aAb
A .B
B .x
S2
S A.
S3
x
a
S S$.
S4
B
S5
S7
S x.b
B x.
A a.Ab
A .aAb
A .B
B .x
S xb.
b
A
A aA.b
A aAb.
B
x
AB.
S10
S8
S9
B x.
12
s0
s1
s2
s3
s3
S4
s4
S5
s6
s6
s7
S8
s8
a
s3
x
s2
A
1
B
4
6
6
4
4
r1
r5
s5
S3
S3
S
acc
s7
S7
r4
r4
r4
r4
r2
s8
S8
r5
r3
r3
r5
r3
r3
13
s0
s1
s2
s3
s3
S4
s4
S5
s6
s6
s7
S8
s8
a
s3
x
s2
s7
S7
e
r4
s8
S8
r5
e
r3
S
acc
A
1
B
4
6
6
4
4
r1
r5
s5
S3
S3
r4
e
r2
e
r3
e
S:S :A :B :B :-
S $
A B
a x
c
d
S :- . A B
A :- . a x
{$}
{c, d}
E :- . E {$}
2.
E:- E
E :- T
E :- E + T
T :- id
T :- ( E )
We thus expand items for E, and E, being at the end of the rule,
can also be followed only by $:
E :- . E {$}
E :- . T {$}
E :- . E + T {$}
3.
Expanding T:
E
E
E
T
T
:::::-
.
.
.
.
.
E {$}
T {$}
E + T {$}
id {$}
( E ) {$}
16
E
E
E
T
T
:::::-
.
.
.
.
.
E {$}
T {$}
E + T {$}
id
{$}
( E ) {$}
E:- E
E :- T
E :- E + T
T :- id
T :- ( E )
4. The E in the 3rd item needs to be expanded. In SLR(1) this was ignored because E
was expanded in an earlier step. But note that the lookahead set is here different:
E
E
E
T
T
E
E
T
T
:::::::::-
.
.
.
.
.
.
.
.
.
E
T
E +
id
( E
T
E +
id
( E
T
)
T
)
{$}
{$}
{$}
{$}
{$}
{+}
{+}
{+}
{+}
17
We can simplify by merging items which are identical except for the
lookahead set :
E
E
E
T
T
:::::-
.
.
.
.
.
E
{$}
T
{$, +}
E + T {$, +}
id
{$, +}
( E ) {$, +}
18
19
if
Sacc
S1
IfStmt :- if . logexp then Block ElseForm {$}
logexp
S3
IfStmt :- if logexp . then Block ElseForm {$}
then
S4
IfStmt :- if logexp then . Block ElseForm {$}
Block :- . stmt {else $}
S5
stmt
Block
S6
else
IfStmt :- if logexp then Block . ElseForm {$}
ElseForm :- . else Block {$}
ElseForm :- {$}
1 IfStmt:- IfStmt $
ElseForm
20
10
22
11