shift reduce
TRANSCRIPT
-
8/7/2019 shift reduce
1/11
Shift-reduce Parsing
The last lecture introduced shift-reduce parsing for doing right-most derivations in reverse.
Split input into
A stack of processed input (terminals and non-terminals)
A suffix containing only non-terminals. First character in thesuffix is called the lookahead.
Perform reduction(reverse production) at end of stack (to producea new rightmost non-terminal) if the stack is a handle . . .
. . . o r shiftthe lookahead to the end (top) of the stack.
To decide which to do, build a FSA to run over the stack and recog-nize handles.
Last modified: Fri Feb 18 00:48:26 2005 CS164: Lecture #13 1
-
8/7/2019 shift reduce
2/11
Recognizing Handles
We will build a grammar for handles.
FACT: this grammar will be regular! So we can build a FSA for it.
Sample grammar (is end of input, p is start):
A. p e
B. e e + tC. e tD. t ( e )E. t i
Last modified: Fri Feb 18 00:48:26 2005 CS164: Lecture #13 2
-
8/7/2019 shift reduce
3/11
A Handle Grammar
A handle for B(B non-terminal) is the part of a rightmost sententialform created by expanding a B up to and including that expansion.
What does it look like? Lets consider symbol p.
After first step of a rightmost derivation (or last step of reversederivation), stack is e (a handle)
So add
Hp e
In this grammar, e, t, and p as well as +, etc. are all terminals.
Hp means handle occurring during derivation from p.
But this means that at a later step, e will be expanded.
So also add
Hp He
where He is handle occurring during derivation from e.Last modified: Fri Feb 18 00:48:26 2005 CS164: Lecture #13 3
-
8/7/2019 shift reduce
4/11
Handle Grammar II
What about He? Obviously, we first have
He e + tHe t
First production implies that at later step
He e + HtHe He (useless, but harmless)
Likewise for Ht:
Ht ( e )Ht ( He
Ht i
This handle grammar is regular, (any non-terminals are always at the
end) so can produce NFA.Last modified: Fri Feb 18 00:48:26 2005 CS164: Lecture #13 4
-
8/7/2019 shift reduce
5/11
NFA for Handle Grammar
t > ( e . )
e > e + . t
e > e + . t
p > e | .
t > ( . e )
t > ( . e )
t > i .
e > e . + t
e > e . + t
e > t .
p > e . |
t > . ( e )
t > . ( e )
t > . i
e > . e + t
e > . e + t
e > . t
p > . e |
tH
eH
t > ( e ) .
t
e > e + t .
)(
e(
i
+e
+e
t
|e
pH
Last modified: Fri Feb 18 00:48:26 2005 CS164: Lecture #13 5
-
8/7/2019 shift reduce
6/11
Items and Item sets
The state labels on the NFA are called LR(0) items.
For example:
e e + . t
means the state of being in a sentential form immediately after ane + that might be part of the end of a handle producing e.
We can convert the NFA to a DFA using set-of-states constructionwe showed before.
The labels on the DFA states are then item sets.
Last modified: Fri Feb 18 00:48:26 2005 CS164: Lecture #13 6
-
8/7/2019 shift reduce
7/11
DFA for Handle Grammar
i
t
e
i
t
(
t > ( e ) .
e > e + t .
e > e | .
p > e . |
e > e . + t
t > i .
e > t .
t > . ( e )
t > . i
e > e + . t
e > e . + t
e
(
9
8
7
6
5
4
3
2
1
0
)
(
i
t
|
+
+
t > ( e . )
t > . ( e )
t > . i
e > . e + t
e > . t
t > ( . e )
t > . ( e )
t > . i
e > . e + t
e > . t
p > . e |
Last modified: Fri Feb 18 00:48:26 2005 CS164: Lecture #13 7
-
8/7/2019 shift reduce
8/11
Interpretation
An item like
t ( e ) .
means if scanning the stack ends you up here, you could reduce thetop three symbols to a t.
An item like
e e . + t
means if scanning the stack ends you up here, and the lookahead is+, you could shift it.
If there are no choices anywhere, grammar is LR(0) (rare).
Otherwise, must decide in places between shifting and reducing orbetween several possible reductions.
Last modified: Fri Feb 18 00:48:26 2005 CS164: Lecture #13 8
-
8/7/2019 shift reduce
9/11
Deciding Between Shifts and Reductions
At states with a reduction item (dot at the end) and a transitionout, there might be strings in the language allowing either (LR(0)shift-reduce conflict).
At states with two reduction items, there might be strings in thelanguage allowing either (LR(0) reduce-reduce conflict).
If the lookahead symbol suffices to resolve conflict, the grammar
is LALR(1) (LookAhead LR(1)). If some DFA can make the decisionwith on symbol of lookahead, the grammar is LR(1).
Simple way to decide (SLR(1)): For an item like e e + t ., takethis reduction if lookahead is in FOLLOW(e).
Works if FOLLOW sets for a state dont overlap and dont contain
any of the shift symbols. Full LALR(1) strategy is more refined: compute specialized FOLLOW
sets for each state by looking backwards in the machine.
Last modified: Fri Feb 18 00:48:26 2005 CS164: Lecture #13 9
-
8/7/2019 shift reduce
10/11
The LALR(1) machine
, L2
v
f
i
t > v ., L2
t
@
, L1
, L1
(
f
i
v
t
|
v > i .
f > i .
p > e | .
f > . i
v > . i
t > . f ( )
t > . v
e > . t @ e
e > . t
e > t @ . e
, L3
, L3e > t @ e .
9
, L2
10
t > f ( ) .
e
)
(
|, @
|
L3 =
L2 =
L1 =Legend:
, L3
, L2
, L2
, L2
, L1
, L1
, L1
, L2, L2
, L2
6
e
7
8
t > f ( . )
5
t > f . ( )
4
3
e > t . @ e
e > t .
2
1
p > e . |
, L3
, L2
, L2
, L1
, L1
0
f > . i
v > . i
t > . f ( )
t > . v
e > . t @ e
e > . t
p > . e |
Last modified: Fri Feb 18 00:48:26 2005 CS164: Lecture #13 10
-
8/7/2019 shift reduce
11/11
LR(0) Machine for Last Lectures Grammar
int
E E T
E + T
E T
E
T T int
E T
intT T
T T intT T
T T
int
E E T
int
int
T T
E E T
E + T
T int
E
T int
T T
E E T
E +
int
int
T
T
T T
E E + T
T int
int
int
T
int
T int
E E + T
Last modified: Fri Feb 18 00:48:26 2005 CS164: Lecture #13 11