21 SLR Parsing
21 SLR Parsing
Arles Rodríguez
[email protected]
Facultad de Ciencias
Departamento de Matemáticas
Universidad Nacional de Colombia
Motivation
• SLR parsing is build on the ideas of valid items
and viable prefixes.
• To understand SLR lets define a very weak
bottom-up algorithm called LR(0) parsing.
LR(0) parsing
• LR(0) parsing assume:
– Stack contains
– Next input is token t
– DFA on input , terminates in state s
• Reduce by if
– State s contains item
• Shift if
– State s contains item
• s has a transition labelled t
LR(0) parsing
• LR(0) has a reduce/reduce conflict if:
– Any state has two reduce items:
T (
T + 𝑇 → ( 𝐸 .)
(
E )
T
(
𝑇 → ( 𝐸) .
′
𝑆 →𝐸.
(
E int
int
int * T
. 𝑇 → 𝑖𝑛𝑡 ∗ 𝑇 .
int
We can reduce or if next
input is a + we could do shift
Shift/reduce conflict
DFA has some conflicts
E 𝐸 → 𝑇 +𝐸 .
T (
T + 𝑇 → ( 𝐸 .)
(
E )
T
(
𝑇 → ( 𝐸) .
′
𝑆 →𝐸.
(
E int
int
int * T
. 𝑇 → 𝑖𝑛𝑡 ∗ 𝑇 .
int
We can reduce or if next
input is a * we could do shift
Shift/reduce conflict
SLR (Simple Left Right) Parsing
• Improve LR(0) parsing:
– Add some heuristics that will refine when we shift
and when we reduce.
– Fewer states have conflicts.
SLR Parsing intuition
• LR(0) parsing assume:
– Stack contains
– Next input is token t
– DFA on input , terminates in state s
To replace we see the stack
• Reduce by if and say that we can replace
– State s contains item by X if it means that we can
reduce:
• Shift if
– State s contains item
• s has a transition labelled t
SLR Parsing
• If there are conflicts under these rules, the
grammar is not SLR.
• The rules are a heuristic for detecting handles:
– SLR grammars are those where the heuristics detect
exactly the handles.
• We consider:
– The contents of the stack.
– DFA
• what items are possible when we get to the top of the stack.
• what is coming up in input.
SLR Parsing
What if next input is +? E 𝐸 → 𝑇 +𝐸 .
T (
T + 𝑇 → ( 𝐸 .)
(
E )
T
(
𝑇 → ( 𝐸) .
′
𝑆 →𝐸.
(
E int
int
int * T
. 𝑇 → 𝑖𝑛𝑡 ∗ 𝑇 .
int
SLR Parsing
If next input is +, shift E 𝐸 → 𝑇 +𝐸 .
T (
T + 𝑇 → ( 𝐸 .)
(
E )
T
(
𝑇 → ( 𝐸) .
′
𝑆 →𝐸.
(
E int
int
int * T
. 𝑇 → 𝑖𝑛𝑡 ∗ 𝑇 .
int
When to reduce? SLR Parsing
𝑙𝑙𝑜𝑤 ( 𝐸 )= { $ , ) } E 𝐸 → 𝑇 +𝐸 .
T (
T + 𝑇 → ( 𝐸 .)
(
E )
T
(
𝑇 → ( 𝐸) .
′
𝑆 →𝐸.
(
E int
int
int * T
. 𝑇 → 𝑖𝑛𝑡 ∗ 𝑇 .
int
When to reduce? SLR Parsing
𝐹𝑜𝑙𝑙𝑜𝑤
Look at ( 𝐸 )= { $ , ) } E 𝐸 → 𝑇 +𝐸 .
T (
T + 𝑇 → ( 𝐸 .)
(
E )
T
(
𝑇 → ( 𝐸) .
′
𝑆 →𝐸.
(
E int
int
int * T
. 𝑇 → 𝑖𝑛𝑡 ∗ 𝑇 .
int
SLR Parsing
Reduce if next input is end
of expression $ or ) E 𝐸 → 𝑇 +𝐸 .
T (
T + 𝑇 → ( 𝐸 .)
(
E )
T
(
𝑇 → ( 𝐸) .
′
𝑆 →𝐸.
(
E int
int
int * T
. 𝑇 → 𝑖𝑛𝑡 ∗ 𝑇 .
int
SLR Parsing
What if next input is *? 𝐸 → 𝑇 +𝐸 .
E
T (
T + 𝑇 → ( 𝐸 .)
(
E )
T
(
𝑇 → ( 𝐸) .
′
𝑆 →𝐸.
(
E int
int
int * T
. 𝑇 → 𝑖𝑛𝑡 ∗ 𝑇 .
int
SLR Parsing
What if next input is *? shift 𝐸 → 𝑇 +𝐸 .
E
T (
T + 𝑇 → ( 𝐸 .)
(
E )
T
(
𝑇 → ( 𝐸) .
′
𝑆 →𝐸.
(
E int
int
int * T
. 𝑇 → 𝑖𝑛𝑡 ∗ 𝑇 .
int
When to reduce? SLR Parsing
E 𝐸 → 𝑇 +𝐸 .
T (
T + 𝑇 → ( 𝐸 .)
(
E )
T
(
𝑇 → ( 𝐸) .
′
𝑆 →𝐸.
(
E int
int
int * T
. 𝑇 → 𝑖𝑛𝑡 ∗ 𝑇 .
int
When to reduce? SLR Parsing
Look at 𝐸 → 𝑇 +𝐸 .
E
T (
T + 𝑇 → ( 𝐸 .)
(
E )
T
(
𝑇 → ( 𝐸) .
′
𝑆 →𝐸.
(
E int
int
int * T
. 𝑇 → 𝑖𝑛𝑡 ∗ 𝑇 .
int
When to reduce? SLR Parsing
Look at 𝐸 → 𝑇 +𝐸 .
E
T (
T + 𝑇 → ( 𝐸 .)
(
E )
T
(
𝑇 → ( 𝐸) .
′
𝑆 →𝐸.
(
E int
int
int * T
. 𝑇 → 𝑖𝑛𝑡 ∗ 𝑇 .
int
SLR Parsing
Look at
Reduce if next input is +, $ or ) E 𝐸 → 𝑇 +𝐸 .
T (
T + 𝑇 → ( 𝐸 .)
(
E )
T
(
𝑇 → ( 𝐸) .
′
𝑆 →𝐸.
(
E int
int
int * T
. 𝑇 → 𝑖𝑛𝑡 ∗ 𝑇 .
int
SLR Parsing
E 𝐸 → 𝑇 +𝐸 .
T (
T + 𝑇 → ( 𝐸 .)
(
E )
T
(
𝑇 → ( 𝐸) .
′
𝑆 →𝐸.
(
E int
int
.
int * T
𝑇 → 𝑖𝑛𝑡 ∗ 𝑇 .
int
• We can reduce by if
SLR Parsing example
Configuration DFA Halt State Action
|int*int$ 1 Shift
int|*int$
• We can reduce by if
, $, )}
So
SLR Parsing example
Configuration DFA Halt State Action
|int*int$ 1 Shift
int|*int$ 3( Shift
• We can shift *.
SLR Parsing example
Configuration DFA Halt State Action
|int*int$ 1 Shift
int|*int$ 3( Shift
int*|int$
• We can reduce to
S’ if $ is in the
Follow of S’
• Since S’ is the start
symbol we accept
at this point
Exercise
SLR improvements
• Our SLR algorithm can be improved because
most of the work that the automata does it
when read the stack is redundant.
• New work is performed near the top of the
stack
…𝛼𝛽 𝑋∨¿
repeated new
SLR improvements
• We can remember the state of the automata
on each stack prefix.
• We can change the stack to contain pairs
• For a stack :
𝑏
𝑆→𝑏.
SLR Parsing Examples 1
• Start state of the NFA is:
′
S →. 𝑆
• If we see a b:
𝑏
𝑆→𝑏.
SLR Parsing Examples 1
• Start state of the NFA is:
′
S →. 𝑆
• If we see a s:
𝑏
𝑆→𝑏.
SLR Parsing Examples 1
• Start state of the NFA is:
′
S →. 𝑆
• If we see an
𝑎
𝑏
𝑆→𝑏. 𝑆 → 𝑆𝑎 .
SLR Parsing Examples 1
• DFA of grammar is
𝑎
𝑏
𝑆→𝑏. 𝑆 → 𝑆𝑎 .
¡No shift-reduce conflicts because there is just one state!
SLR Parsing Examples 1
• DFA of grammar is
𝑎
𝑏
𝑆→𝑏. 𝑆 → 𝑆𝑎 .
¡No shift-reduce conflicts there are only shifts!
SLR Parsing Examples 1
• DFA of grammar is
𝑎
𝑏
𝑆→𝑏. 𝑆 → 𝑆𝑎 .
Require evaluate follow of S’
SLR Parsing Examples 1
• DFA of grammar is
𝑎
𝑏
𝑆→𝑏. 𝑆 → 𝑆𝑎 .
Require evaluate follow of S’: Follow ( S′ ) ={$ }
SLR Parsing Examples 1
• DFA of grammar is
𝑎
𝑏
𝑆→𝑏. 𝑆 → 𝑆𝑎 .
Follow ( S′ ) ={$ } We have an SLR grammar!
SLR Parsing Examples 2
• Consider the grammar:
𝑏
𝑆→𝑏.
SLR Parsing Examples 2
• If we see S:
𝑏
𝑆→𝑏.
SLR Parsing Examples 2
• If we see :
𝑆 𝑎
𝑆 → 𝑆𝑎 . 𝑆
𝑏
𝑆→𝑏.
SLR Parsing Examples 2
• Now we need to add all the
production of S:
𝑆 𝑎
𝑏
𝑆→𝑏.
SLR Parsing Examples 2
• If we see a b
𝑆 𝑎
𝑏
𝑆→𝑏. 𝑏
SLR Parsing Examples 2
• If we see a S
𝑆
𝑆 𝑎
𝑏
𝑆→𝑏. 𝑏
SLR Parsing Examples 2
• If we see an
𝑆
𝑎
𝑆 𝑎
𝑏
𝑆→𝑏. 𝑏
SLR Parsing Examples 2
Is this an SLR grammar? No reduce conflicts
in states 1 and 2
𝑆
𝑎
𝑆 𝑎
𝑏
𝑆→𝑏. 𝑏
SLR Parsing Examples 2
Is this an SLR grammar? Follow(S’)={$}
and with input a, move state so no conflict there
𝑆
𝑎
𝑆 𝑎
𝑏
𝑆→𝑏. 𝑏
SLR Parsing Examples 2
Is this an SLR grammar? No reduce movements
there
𝑆
𝑎
𝑆 𝑎
𝑏
𝑆→𝑏. 𝑏
SLR Parsing Examples 2
Is this an SLR grammar? First state is a reduction
require evaluate Follow(S)
𝑆
𝑎
𝑆 𝑎
𝑏
𝑆→𝑏. 𝑏
SLR Parsing Examples 2
Is this an SLR grammar? First state is a reduction
require evaluate Follow(S)={$,a}
𝑆
𝑎
𝑆 𝑎
𝑏
𝑆→𝑏. 𝑏
SLR Parsing Examples 2
Is this an SLR grammar? First state is a reduction
require evaluate Follow(S)={$,a}, here is a Shift-reduce
conflict.
𝑆
𝑎
𝑆 𝑎
𝑏
𝑆→𝑏. 𝑏
SLR Parsing Examples 2
Is this an SLR grammar? Grammar is not SLR
𝑆
𝑎
𝑆 𝑎
𝑏
𝑆→𝑏. 𝑏
¡Thank you!
References
• Aho et al. Compilers: principles,
techniques, and tools. Torczon et al. (2014)
(Section 4.5.-4.6)
• Slides are based on the design of Aiken Alex. CS 143.
https://web.stanford.edu/class/cs143/