Professional Documents
Culture Documents
Lecture 6
Haythem
c O. Ismail SLR Parsing 1 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
Objectives
Haythem
c O. Ismail SLR Parsing 2 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
Outline
1 LR(k) Parsing
Haythem
c O. Ismail SLR Parsing 3 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
Outline
1 LR(k) Parsing
Haythem
c O. Ismail SLR Parsing 4 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
Definition
An LR grammar is a grammar for which a deterministic shift-reduce
parser may be constructed.
Haythem
c O. Ismail SLR Parsing 5 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
Definition
An LR grammar is a grammar for which a deterministic shift-reduce
parser may be constructed.
Haythem
c O. Ismail SLR Parsing 5 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
Why LR Parsers?
Haythem
c O. Ismail SLR Parsing 6 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
Grammar G6
Example
We shall often refer to the following grammar G6 .
E −→ E + T | T
T −→ T ∗ F | F
F −→ ( E ) | id
Haythem
c O. Ismail SLR Parsing 7 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
Example
With G6 and input id∗id, having shifted id, a shift-reduce parser
may decide to shift ∗.
But clearly, given the rules of G6 , this will never succeed.
Haythem
c O. Ismail SLR Parsing 8 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
Example
With G6 and input id∗id, having shifted id, a shift-reduce parser
may decide to shift ∗.
But clearly, given the rules of G6 , this will never succeed.
Haythem
c O. Ismail SLR Parsing 8 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
Example
With G6 and input id∗id, we may reach a configuration where T
appears on top of the stack and ∗id remains in the input stream.
We can choose to reduce using the rule E → T.
But clearly, given the rules of G6 , this will never succeed.
Haythem
c O. Ismail SLR Parsing 9 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
Example
With G6 and input id∗id, we may reach a configuration where T
appears on top of the stack and ∗id remains in the input stream.
We can choose to reduce using the rule E → T.
But clearly, given the rules of G6 , this will never succeed.
Haythem
c O. Ismail SLR Parsing 9 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
Example
With G6 and input id∗id, we may reach a configuration where T
appears on top of the stack and ∗id remains in the input stream.
We can choose to reduce using the rule E → T.
But clearly, given the rules of G6 , this will never succeed.
Haythem
c O. Ismail SLR Parsing 9 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
Outline
1 LR(k) Parsing
Haythem
c O. Ismail SLR Parsing 10 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
LR(0) Items
Definition
An LR(0) item of CFG G = hV, Σ, R, Si is a pair hA → α, ii, where
(A → α) ∈ R and 0 ≤ i ≤ |α|.
Haythem
c O. Ismail SLR Parsing 11 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
Definition
For a CFG G = hV, Σ, R, Si, the LR(0) NFA is an NFA
NG = hI, V ∪ Σ, δ, S0 → .S, Ii, where
I is the set LR(0) items of G together with S0 → .S;
S0 ∈
/ V ∪ Σ;
δ(A → α.sβ, s) = {A → αs.β};
δ(A → α.Bβ, ε) = {B → .γ | (B → γ) ∈ R}
Haythem
c O. Ismail SLR Parsing 12 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
Definition
The LR(0) automaton for a CFG G is the DFA MG which is equivalent
to NG and constructed using the standard subset construction.
Haythem
c O. Ismail SLR Parsing 13 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
Exercise
Example
Construct the LR(0) automaton of G6 :
E −→ E + T | T
T −→ T ∗ F | F
F −→ ( E ) | id
Haythem
c O. Ismail SLR Parsing 14 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
Exercise (II)
Example
c Aho et al. (2007)
Haythem
c O. Ismail SLR Parsing 15 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
Outline
1 LR(k) Parsing
Haythem
c O. Ismail SLR Parsing 16 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
Haythem
c O. Ismail SLR Parsing 17 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
Haythem
c O. Ismail SLR Parsing 17 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
Haythem
c O. Ismail SLR Parsing 17 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
Haythem
c O. Ismail SLR Parsing 17 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
Haythem
c O. Ismail SLR Parsing 17 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
Haythem
c O. Ismail SLR Parsing 17 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
Haythem
c O. Ismail SLR Parsing 17 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
Haythem
c O. Ismail SLR Parsing 18 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
Haythem
c O. Ismail SLR Parsing 18 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
Exercise (I)
Example
Construct the SLR parsing table for G6 .
(1) E −→ E+T
(2) E −→ T
(3) T −→ T ∗F
(4) T −→ F
(5) F −→ (E)
(6) F −→ id
Note:
si means “shift state i”.
rj means “reduce rule j”.
Haythem
c O. Ismail SLR Parsing 19 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
Exercise (I)
Example
Construct the SLR parsing table for G6 .
(1) E −→ E+T
(2) E −→ T
(3) T −→ T ∗F
(4) T −→ F
(5) F −→ (E)
(6) F −→ id
Note:
si means “shift state i”.
rj means “reduce rule j”.
Haythem
c O. Ismail SLR Parsing 19 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
Exercise (II)
Example ( table construction , automaton )
c Aho et al. (2007)
Haythem
c O. Ismail SLR Parsing 20 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
Haythem
c O. Ismail SLR Parsing 21 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
Haythem
c O. Ismail SLR Parsing 21 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
Haythem
c O. Ismail SLR Parsing 21 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
Haythem
c O. Ismail SLR Parsing 21 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
The Algorithm
Given G = hV, Σ, R, Si and w.
1 Construct MG and the parsing table for G.
2 Push the start state of MG onto the stack.
3 While (true) do
1 s ←− top of the stack state.
2 a ←− first symbol of w.
3 If ACTION(s, a) = “shift t”, then
1 Push t on top of the stack.
2 w ←− w with a removed.
4 If ACTION(s, a) = “reduce A → α”, then
1 Pop |α| states off the stack.
2 t ←− top of the stack state.
3 Push GOTO(t, A) onto the stack.
4 Output the rule A → α.
5 If ACTION(s, a) = “accept”, then break.
6 Else call error-recovery routine.
Haythem
c O. Ismail SLR Parsing 22 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
Exercise (I)
Example
Trace the operation of the LR parsing algorithm given G6 and input
id∗id+id.
Haythem
c O. Ismail SLR Parsing 23 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
Exercise (II)
Example ( algorithm , automaton , table )
c Aho et al. (2007)
Haythem
c O. Ismail SLR Parsing 24 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
Example
Consider the following grammar G7 :
S −→ L=R | R
L −→ ∗R | id
R −→ L
Haythem
c O. Ismail SLR Parsing 25 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
c Aho et al. (2007)
Haythem
c O. Ismail SLR Parsing 26 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
Example
Consider ACTION(2, =).
Due to (S → L.=R), we get “shift 6”.
But = ∈ Follow(R). (Why?)
Thus, due to (R → L), we get “reduce R → L”.
Hence, a shift/reduce conflict.
Haythem
c O. Ismail SLR Parsing 27 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
Example
Consider ACTION(2, =).
Due to (S → L.=R), we get “shift 6”.
But = ∈ Follow(R). (Why?)
Thus, due to (R → L), we get “reduce R → L”.
Hence, a shift/reduce conflict.
Haythem
c O. Ismail SLR Parsing 27 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
Example
Consider ACTION(2, =).
Due to (S → L.=R), we get “shift 6”.
But = ∈ Follow(R). (Why?)
Thus, due to (R → L), we get “reduce R → L”.
Hence, a shift/reduce conflict.
Haythem
c O. Ismail SLR Parsing 27 / 27
LR(k) Parsing
The LR(0) Automaton
The SLR Parsing Algorithm
Example
Consider ACTION(2, =).
Due to (S → L.=R), we get “shift 6”.
But = ∈ Follow(R). (Why?)
Thus, due to (R → L), we get “reduce R → L”.
Hence, a shift/reduce conflict.
Haythem
c O. Ismail SLR Parsing 27 / 27