LR Parsing Table Costruction
LR Parsing Table Costruction
Lecture 6
Syntax Analysis
1
LR parsing example
Grammar:
1. E -> E + T
2. E -> T
3. T -> T * F
4. T -> F
5. F -> ( E )
6. F -> id
2
LR parsing example
CONFIGURATIONS
STACK INPUT ACTION
0 id * id + id $ shift 5
3
STACK INPUT ACTION
5
How to construct an LR parse table?
6
SLR (Simple LR) Parse Table Construction
7
SLR parse tables
The SLR parse table is easy to construct, but the resulting parser is
a little weak.
The table is based on LR(0) ITEMS, or just plain ITEMS.
A LR(0) item is a production G with a dot at some position on the
RHS.
The production A -> XYZ could generate the following LR(0) items:
A -> .XYZ
A -> X.YZ
A -> XY.Z
A -> XYZ.
8
LR(0) items
9
Augmenting the grammar G
10
Item set closure
11
Itemset closure example
12
The goto table
13
I0: E' · E I5: F id ·
E·E+T
E·T I6: E E + · T
T·T*F
T·T*F
T·F
T·F
F · ( E)
F · (E)
F · id
F · id
I7: T T * · F
F · ( E)
F · id
I1: E' E ·
EE·+T
I2: E T · I8: F ( E · )
TT·*F EE·+T
( E )
I4 I8 I11
+
to I6
T
to I2
F
id to I3
id
I5
16
How to build the SLR parse table
1. Take the augmented grammar G’
2. Construct the canonical LR(0) itemsets C for G’
3. Associate a state with each itemset Ii in C
4. Construct the parse table as follows:
1. If A -> α . a β is in Ii and goto(Ii,a) = Ij, then set action[i,a]
to “shift j” (“a” here is a terminal)
2. If A -> α . is in Ii then set action[i,a] to “reduce A -> α” for
all a in FOLLOW(A)
3. If S’ -> S . is in Ii then set action[i,$] to “accept”
17
Example SLR table construction
For the first LR(0) itemset in our favorite grammar:
I0: E’ -> .E
E -> .E + T
E -> .T
T -> .T * F
T -> .F
F -> .(E) This gives us action[0,(] = shift 4
F -> .id This gives us action[0,id] = shift 5
18
Using Ambiguous Grammars
19
What to do with ambiguity?
20
What to do with ambiguity?
21
LR(0) itemsets for G1
22
Ambiguity leads to conflicts
23
Resolving the conflicts
24
If-else ambiguity
25
Non-SLR grammars
26
Non-SLR grammars
Construct the initial canonical LR(0) itemset I0.
Compute I2 = goto(I0,L) and I6 = goto(I2,=).
Compute FOLLOW(L)
Compute parse table entries for I2: shift/reduce conflict!
27
Canonical LR Parse Table Construction
28
I0: S' · S I5: L id ·
S·L=R
S·R
L·*R I6: S L = · R
R·L
L · id
L·*R
R·L
L · id
I2: S L · = R I8: R L ·
RL·
I9: SL=R·
I3: S R ·
I4: L * · R
R·L
L·*R
L · id
30
LR(1) idea
31
LR(1) parse table construction
32
Example LR(1) parser construction
Begin with augmented grammar G’:
S’ -> S
S -> C C [ what is L(G’)?? ]
C -> c C | d
33
S' → ·S , $
S → ·CC , $ S S' → S·, $
C → ·cC , c /d
I1
C → ·d , c /d
I0
S → C·C , $
C C → ·cC , $ C S → CC·, $
C → ·d , $ I5
I2
C → c·C , $
c C → ·cC , $ C C → cC·, $
C → ·d , $ I9
I6
d C → d·, $
I7
c
C → c·C , c /d
c C → ·cC , c /d C C → cC·, c /d
C → ·d , c /d I8
I3
d C → d·, c /d
I4
35
LR(1) parsers: the bad news
36
action goto
STATE
c d $ S C
0 s3 s4 1 2
1 acc
2 s6 s7 5
3 s3 s4 8
4 r3 r3
5 r1
6 s6 s7 9
7 r3
8 r2 r2
9 r2
38
LALR parse tables
39
LALR idea
40
Which LR(1) itemsets
LALR example can be merged?
41
action goto
STATE
c d $ S C
0 s36 s47 1 2
1 acc
2 s36 s47 5
36 s36 s47 89
47 r3 r3 r3
5 r1
89 r2 r2 r2
42
Efficient Construction of LALR Parsing Tables
I3: S R · I8: R L ·
I4: L * · R I9: S L = R ·
Fig. 4.42. Kernels of the sets of LR(0) items for grammar (4.20).
43
Efficient Construction of LALR Parsing Tables
Example 4.47. Let us construct the kernels of the LALR(1) items for the grammar in
the previous example. The kernels of the LR(0) items were shown in Fig. 4.42.
When we apply Algorithm 4.12 to the kernel of set of items I0, we compute
closure ({[S' · S, #]}), which is
S' · S, #
S · L = R, #
S · R, #
L · * R, #/=
L · id, #/=
R · L, #
44
FROM TO
I0: S' · S I1: S' S ·
I2: SL·=R
I2: RL·
I3: SR·
I4: L*·R
I5: L id ·
45
LOOKAHEADS
SET ITEM
INIT PASS1 PASS2 PASS3
I0: S' · S $ $ $ $
I1: S' S · $ $ $
I2: S L · = R $ $ $
I2: R L · $ $ $
I3: S R · $ $ $
I4: L * · R = =/$ =/$ =/$
I5: L id · = =/$ =/$ =/$
I6: S L = · R $ $
I7: L * R · = =/$ =/$
I8: R L · = =/$ =/$
I9: S L = R · $
47