Title: Martin Rinard
1MIT 6.035Introduction to Shift-Reduce Parsing
- Martin Rinard
- Laboratory for Computer Science
- Massachusetts Institute of Technology
2Orientation
- Specify Syntax Using Context-Free Grammar
- Nonterminals
- Terminals
- Productions
- Given a grammar, Parser Generator produces a
parser - Starts with input string
- Produces parse tree
Expr ? Expr Op Expr Expr ? (Expr) Expr ? -
Expr Expr ? num Op ? Op ? - Op ?
3Todays Lecture
- How generated parser works
- How parser generator produces parser
- Central mechanism
- Pushdown automaton, which implements
- Shift-reduce parser
4Pushdown Automata
- Consists of
- Pushdown stack (can have terminals and
nonterminals) - Finite state automaton control
- Can do one of three actions (based on state and
input) - Shift
- Shift current input symbol from input onto stack
- Reduce
- If symbols on top of stack match right hand side
of some grammar production NT ? ? - Pop symbols (?) off of the stack
- Push left hand side nonterminal (NT) onto stack
- Accept the input string
5Shift-Reduce Parser Example
Expr ? Expr Op Expr Expr ? (Expr) Expr ? -
Expr Expr ? num Op ? Op ? - Op ?
Stack
Input String
(
num
)
num
num
6Shift-Reduce Parser Example
Expr ? Expr Op Expr Expr ? (Expr) Expr ? -
Expr Expr ? num Op ? Op ? - Op ?
(
num
)
num
num
7Shift-Reduce Parser Example
Expr ? Expr Op Expr Expr ? (Expr) Expr ? -
Expr Expr ? num Op ? Op ? - Op ?
SHIFT
(
num
)
num
num
8Shift-Reduce Parser Example
Expr ? Expr Op Expr Expr ? (Expr) Expr ? -
Expr Expr ? num Op ? Op ? - Op ?
num
SHIFT
(
num
)
num
9Shift-Reduce Parser Example
Expr ? Expr Op Expr Expr ? (Expr) Expr ? -
Expr Expr ? num Op ? Op ? - Op ?
num
REDUCE
(
num
)
num
10Shift-Reduce Parser Example
Expr ? Expr Op Expr Expr ? (Expr) Expr ? -
Expr Expr ? num Op ? Op ? - Op ?
Expr
REDUCE
num
(
num
)
num
11Shift-Reduce Parser Example
Expr ? Expr Op Expr Expr ? (Expr) Expr ? -
Expr Expr ? num Op ? Op ? - Op ?
Expr
SHIFT
num
(
num
)
num
12Shift-Reduce Parser Example
Expr ? Expr Op Expr Expr ? (Expr) Expr ? -
Expr Expr ? num Op ? Op ? - Op ?
Expr
SHIFT
num
(
num
)
num
13Shift-Reduce Parser Example
Expr ? Expr Op Expr Expr ? (Expr) Expr ? -
Expr Expr ? num Op ? Op ? - Op ?
Op
Expr
REDUCE
num
(
num
)
num
14Shift-Reduce Parser Example
Expr ? Expr Op Expr Expr ? (Expr) Expr ? -
Expr Expr ? num Op ? Op ? - Op ?
Op
Expr
SHIFT
num
(
num
)
num
15Shift-Reduce Parser Example
Expr ? Expr Op Expr Expr ? (Expr) Expr ? -
Expr Expr ? num Op ? Op ? - Op ?
(
Op
Expr
SHIFT
num
num
)
num
16Shift-Reduce Parser Example
Expr ? Expr Op Expr Expr ? (Expr) Expr ? -
Expr Expr ? num Op ? Op ? - Op ?
(
Op
Expr
SHIFT
num
num
)
num
17Shift-Reduce Parser Example
Expr ? Expr Op Expr Expr ? (Expr) Expr ? -
Expr Expr ? num Op ? Op ? - Op ?
num
(
Op
Expr
SHIFT
num
num
)
18Shift-Reduce Parser Example
Expr ? Expr Op Expr Expr ? (Expr) Expr ? -
Expr Expr ? num Op ? Op ? - Op ?
Expr
(
Op
Expr
SHIFT
REDUCE
num
num
num
)
19Shift-Reduce Parser Example
Expr ? Expr Op Expr Expr ? (Expr) Expr ? -
Expr Expr ? num Op ? Op ? - Op ?
Expr
(
Op
Expr
SHIFT
num
num
num
)
20Shift-Reduce Parser Example
Expr ? Expr Op Expr Expr ? (Expr) Expr ? -
Expr Expr ? num Op ? Op ? - Op ?
Expr
(
Op
Expr
SHIFT
num
num
num
)
21Shift-Reduce Parser Example
Expr ? Expr Op Expr Expr ? (Expr) Expr ? -
Expr Expr ? num Op ? Op ? - Op ?
Op
Expr
(
Op
Expr
SHIFT
REDUCE
num
num
num
)
22Shift-Reduce Parser Example
Expr ? Expr Op Expr Expr ? (Expr) Expr ? -
Expr Expr ? num Op ? Op ? - Op ?
Op
Expr
(
Op
Expr
SHIFT
num
num
num
)
23Shift-Reduce Parser Example
num
Expr ? Expr Op Expr Expr ? (Expr) Expr ? -
Expr Expr ? num Op ? Op ? - Op ?
Op
Expr
(
Op
Expr
SHIFT
num
num
)
24Shift-Reduce Parser Example
Expr
Expr ? Expr Op Expr Expr ? (Expr) Expr ? -
Expr Expr ? num Op ? Op ? - Op ?
Op
Expr
(
Op
Expr
SHIFT
REDUCE
num
num
num
)
25Shift-Reduce Parser Example
Expr ? Expr Op Expr Expr ? (Expr) Expr ? -
Expr Expr ? num Op ? Op ? - Op ?
Expr
Expr
Expr
(
Op
Op
Expr
Expr
SHIFT
REDUCE
num
num
num
)
26Shift-Reduce Parser Example
Expr ? Expr Op Expr Expr ? (Expr) Expr ? -
Expr Expr ? num Op ? Op ? - Op ?
Expr
Expr
(
Op
Op
Expr
Expr
SHIFT
num
num
num
)
27Shift-Reduce Parser Example
Expr ? Expr Op Expr Expr ? (Expr) Expr ? -
Expr Expr ? num Op ? Op ? - Op ?
)
Expr
Expr
(
Op
Op
Expr
Expr
SHIFT
num
num
num
28Shift-Reduce Parser Example
Expr ? Expr Op Expr Expr ? (Expr) Expr ? -
Expr Expr ? num Op ? Op ? - Op ?
)
Expr
Expr
Op
Expr
Expr
(
Op
Expr
REDUCE
num
num
num
29Shift-Reduce Parser Example
Expr ? Expr Op Expr Expr ? (Expr) Expr ? -
Expr Expr ? num Op ? Op ? - Op ?
)
Expr
Expr
Op
Expr
(
Expr
Op
Expr
Expr
REDUCE
num
num
num
30Shift-Reduce Parser Example
Expr ? Expr Op Expr Expr ? (Expr) Expr ? -
Expr Expr ? num Op ? Op ? - Op ?
)
Expr
Expr
Op
Expr
(
Expr
Op
Expr
Expr
ACCEPT!
num
num
num
31Basic Idea
- Goal reconstruct parse tree for input string
- Read input from left to right
- Build tree in a bottom-up fashion
- Use stack to hold pending sequences of terminals
and nonterminals
32Potential Conflicts
- Reduce/Reduce Conflict
- Top of the stack may match RHS of multiple
productions - Which production to use in the reduction?
- Shift/Reduce Conflict
- Stack may match RHS of production
- But that may not be the right match
- May need to shift an input and later find a
different reduction
33Conflicts
Expr ? Expr Op Expr Expr ? Expr - Expr Expr ?
(Expr) Expr ? Expr - Expr ? num Op ? Op ?
- Op ?
Expr ? Expr Op Expr Expr ? (Expr) Expr ? -
Expr Expr ? num Op ? Op ? - Op ?
34Conflicts
Expr ? Expr Op Expr Expr ? Expr - Expr Expr
? (Expr) Expr ? Expr - Expr ? num Op ? Op ?
- Op ?
-
num
num
35Conflicts
Expr ? Expr Op Expr Expr ? Expr - Expr Expr
? (Expr) Expr ? Expr - Expr ? num Op ? Op ?
- Op ?
SHIFT
-
num
num
36Conflicts
Expr ? Expr Op Expr Expr ? Expr - Expr Expr
? (Expr) Expr ? Expr - Expr ? num Op ? Op ?
- Op ?
num
SHIFT
-
num
37Conflicts
Expr ? Expr Op Expr Expr ? Expr - Expr Expr
? (Expr) Expr ? Expr - Expr ? num Op ? Op ?
- Op ?
Expr
SHIFT
REDUCE
num
-
num
38Conflicts
Expr ? Expr Op Expr Expr ? Expr - Expr Expr
? (Expr) Expr ? Expr - Expr ? num Op ? Op ?
- Op ?
Expr
SHIFT
num
-
num
39Conflicts
Expr ? Expr Op Expr Expr ? Expr - Expr Expr
? (Expr) Expr ? Expr - Expr ? num Op ? Op ?
- Op ?
-
Expr
SHIFT
num
num
40Shift/Reduce/Reduce Conflict
Expr ? Expr Op Expr Expr ? Expr - Expr Expr
? (Expr) Expr ? Expr - Expr ? num Op ? Op ?
- Op ?
Options Reduce Reduce Shift
-
Expr
num
num
41Shift/Reduce/Reduce Conflict
Expr ? Expr Op Expr Expr ? Expr - Expr Expr
? (Expr) Expr ? Expr - Expr ? num Op ? Op ?
- Op ?
What Happens if Choose Reduce
-
Expr
REDUCE
num
num
42Shift/Reduce/Reduce Conflict
Expr ? Expr Op Expr Expr ? Expr - Expr Expr
? (Expr) Expr ? Expr - Expr ? num Op ? Op ?
- Op ?
What Happens if Choose Reduce
Expr
Expr
SHIFT
num
-
num
43Shift/Reduce/Reduce Conflict
Expr ? Expr Op Expr Expr ? Expr - Expr Expr
? (Expr) Expr ? Expr - Expr ? num Op ? Op ?
- Op ?
What Happens if Choose Reduce
num
Expr
Expr
SHIFT
num
-
44Shift/Reduce/Reduce Conflict
Expr ? Expr Op Expr Expr ? Expr - Expr Expr
? (Expr) Expr ? Expr - Expr ? num Op ? Op ?
- Op ?
What Happens if Choose Reduce
Expr
Expr
Expr
REDUCE
num
-
num
45Shift/Reduce/Reduce Conflict
Expr ? Expr Op Expr Expr ? Expr - Expr Expr
? (Expr) Expr ? Expr - Expr ? num Op ? Op ?
- Op ?
What Happens if Choose Reduce
Expr
Expr
Expr
FAILS!
num
-
num
46Shift/Reduce/Reduce Conflict
Expr ? Expr Op Expr Expr ? Expr - Expr Expr
? (Expr) Expr ? Expr - Expr ? num Op ? Op ?
- Op ?
Both of These Actions Work Reduce Shift
-
Expr
num
num
47Shift/Reduce/Reduce Conflict
Expr ? Expr Op Expr Expr ? Expr - Expr Expr
? (Expr) Expr ? Expr - Expr ? num Op ? Op ?
- Op ?
What Happens if Choose Reduce
-
Expr
num
num
48Shift/Reduce/Reduce Conflict
Expr ? Expr Op Expr Expr ? Expr - Expr Expr
? (Expr) Expr ? Expr - Expr ? num Op ? Op ?
- Op ?
What Happens if Choose Reduce
Op
Expr
REDUCE
num
-
num
49Shift/Reduce/Reduce Conflict
Expr ? Expr Op Expr Expr ? Expr - Expr Expr
? (Expr) Expr ? Expr - Expr ? num Op ? Op ?
- Op ?
What Happens if Choose Reduce
num
Op
Expr
SHIFT
num
-
50Shift/Reduce/Reduce Conflict
Expr ? Expr Op Expr Expr ? Expr - Expr Expr
? (Expr) Expr ? Expr - Expr ? num Op ? Op ?
- Op ?
What Happens if Choose Reduce
Expr
Op
Expr
REDUCE
num
-
num
51Shift/Reduce/Reduce Conflict
Expr ? Expr Op Expr Expr ? Expr - Expr Expr
? (Expr) Expr ? Expr - Expr ? num Op ? Op ?
- Op ?
What Happens if Choose Reduce
Expr
Op
Expr
Expr
REDUCE
num
-
num
52Shift/Reduce/Reduce Conflict
Expr ? Expr Op Expr Expr ? Expr - Expr Expr
? (Expr) Expr ? Expr - Expr ? num Op ? Op ?
- Op ?
What Happens if Choose Reduce
Expr
Op
Expr
Expr
ACCEPT
num
-
num
53Conflicts
Expr ? Expr Op Expr Expr ? Expr - Expr Expr
? (Expr) Expr ? Expr - Expr ? num Op ? Op ?
- Op ?
What Happens if Choose Shift
-
Expr
SHIFT
num
num
54Conflicts
Expr ? Expr Op Expr Expr ? Expr - Expr Expr
? (Expr) Expr ? Expr - Expr ? num Op ? Op ?
- Op ?
What Happens if Choose Shift
num
-
Expr
SHIFT
num
55Conflicts
Expr ? Expr Op Expr Expr ? Expr - Expr Expr
? (Expr) Expr ? Expr - Expr ? num Op ? Op ?
- Op ?
What Happens if Choose Shift
Expr
-
Expr
REDUCE
num
num
56Conflicts
Expr ? Expr Op Expr Expr ? Expr - Expr Expr
? (Expr) Expr ? Expr - Expr ? num Op ? Op ?
- Op ?
What Happens if Choose Shift
Expr
Expr
Expr
REDUCE
num
-
num
57Conflicts
Expr ? Expr Op Expr Expr ? Expr - Expr Expr
? (Expr) Expr ? Expr - Expr ? num Op ? Op ?
- Op ?
What Happens if Choose Shift
Expr
Expr
Expr
ACCEPT
num
-
num
58Shift/Reduce/Reduce Conflict
Expr ? Expr Op Expr Expr ? Expr - Expr Expr
? (Expr) Expr ? Expr - Expr ? num Op ? Op ?
- Op ?
This Shift/Reduce Conflict Reflects Ambiguity in
Grammar
-
Expr
num
num
59Shift/Reduce/Reduce Conflict
Expr ? Expr Op Expr Expr ? Expr - Expr Expr
? (Expr) Expr ? Expr - Expr ? num Op ? Op ?
- Op ?
This Shift/Reduce Conflict Reflects Ambiguity in
Grammar
-
Expr
Eliminate by Hacking Grammar
num
num
60Shift/Reduce/Reduce Conflict
Expr ? Expr Op Expr Expr ? Expr - Expr Expr
? (Expr) Expr ? Expr - Expr ? num Op ? Op ?
- Op ?
This Shift/Reduce Conflict Can Be Eliminated By
Lookahead of One Symbol
-
Expr
Parser Generator Should Handle It
num
num
61Constructing a Parser
- We will construct version with no lookahead
- Key Decisions
- Shift or Reduce
- Which Production to Reduce
- Basic Idea
- Build a DFA to control shift and reduce actions
- In effect, convert grammar to pushdown automaton
- Encode finite state control in parse table
62Parser State
- Input Token Sequence ( for end of input)
- Current State from Finite State Automaton
- Two Stacks
- State Stack (implements finite state automaton)
- Symbol Stack (terminals from input and
nonterminals from reductions)
63Integrating Finite State Control
- Actions
- Push Symbols and States Onto Stacks
- Reduce According to a Given Production
- Accept
- Selected action is a function of
- Current input symbol
- Current state of finite state control
- Each action specifies next state
- Implement control using parse table
64Parse Tables
- Implements finite state control
- At each step, look up
- Tabletop of state stack input symbol
- Then carry out the action
65Parse Table Example
Grammar
Input
State Stack
Symbol Stack
- S ? X (1)
- X ? (X ) (2)
- X ? ( ) (3)
(())
X
s0
66Parser Tables
- Shift to sn
- Push input token into the symbol stack
- Push sn into state stack
- Advance to next input symbol
67Parser Tables
- Reduce (n)
- Pop both stacks as many times as the number of
symbols on the RHS of rule n - Push LHS of rule n into symbol stack
68Parser Tables
- Reduce (n) (continued)
- Look up
- Tabletop of the state stacktop of symbol
stack - Push that state (in goto part of table) onto
state stack
69Parser Tables
- Accept
- Stop parsing and report success
70Parse Table In Action
Grammar
Input
State Stack
Symbol Stack
S ? X (1) X ? (X ) (2) X ? ( ) (3)
(())
s0
71Parse Table In Action
Grammar
Input
State Stack
Symbol Stack
S ? X (1) X ? (X ) (2) X ? ( ) (3)
(())
s0
72Parse Table In Action
Grammar
Input
State Stack
Symbol Stack
S ? X (1) X ? (X ) (2) X ? ( ) (3)
())
s2
s0
(
73Parse Table In Action
Grammar
Input
State Stack
Symbol Stack
S ? X (1) X ? (X ) (2) X ? ( ) (3)
())
s2
s0
(
74Parse Table In Action
Grammar
Input
State Stack
Symbol Stack
S ? X (1) X ? (X ) (2) X ? ( ) (3)
))
s2
s2
(
s0
(
75Parse Table In Action
Grammar
Input
State Stack
Symbol Stack
S ? X (1) X ? (X ) (2) X ? ( ) (3)
))
s2
s2
(
s0
(
76Parse Table In Action
Grammar
Input
State Stack
Symbol Stack
S ? X (1) X ? (X ) (2) X ? ( ) (3)
)
s5
s2
)
s2
(
s0
(
77Parse Table In Action
Input
State Stack
Symbol Stack
Grammar
)
s5
S ? X (1) X ? (X ) (2) X ? ( ) (3)
s2
)
s2
(
s0
(
78Step One Pop Stacks
Input
State Stack
Symbol Stack
Grammar
)
s5
S ? X (1) X ? (X ) (2) X ? ( ) (3)
s2
)
s2
(
s0
(
79Step One Pop Stacks
Grammar
Input
State Stack
Symbol Stack
S ? X (1) X ? (X ) (2) X ? ( ) (3)
)
s2
s0
(
80Step Two Push Nonterminal
Grammar
Input
State Stack
Symbol Stack
S ? X (1) X ? (X ) (2) X ? ( ) (3)
)
s2
s0
(
81Step Two Push Nonterminal
Grammar
Input
State Stack
Symbol Stack
S ? X (1) X ? (X ) (2) X ? ( ) (3)
)
s2
X
s0
(
82Step Three Use Goto, Push New State
Grammar
Input
State Stack
Symbol Stack
S ? X (1) X ? (X ) (2) X ? ( ) (3)
)
s2
X
s0
(
83Step Three Use Goto, Push New State
Grammar
Input
State Stack
Symbol Stack
S ? X (1) X ? (X ) (2) X ? ( ) (3)
)
s3
s2
X
s0
(
84Parse Table In Action
Grammar
Input
State Stack
Symbol Stack
S ? X (1) X ? (X ) (2) X ? ( ) (3)
)
s3
s2
X
s0
(
85Parse Table In Action
Grammar
Input
State Stack
Symbol Stack
S ? X (1) X ? (X ) (2) X ? ( ) (3)
s4
s3
)
s2
X
s0
(
86Parse Table In Action
Grammar
Input
State Stack
Symbol Stack
S ? X (1) X ? (X ) (2) X ? ( ) (3)
s4
s3
)
s2
X
s0
(
87Step One Pop Stacks
Grammar
Input
State Stack
Symbol Stack
S ? X (1) X ? (X ) (2) X ? ( ) (3)
s4
s3
)
s2
X
s0
(
88Step One Pop Stacks
Grammar
Input
State Stack
Symbol Stack
S ? X (1) X ? (X ) (2) X ? ( ) (3)
s0
89Step Two Push Nonterminal
Grammar
Input
State Stack
Symbol Stack
S ? X (1) X ? (X ) (2) X ? ( ) (3)
s0
90Step Two Push Nonterminal
Grammar
Input
State Stack
Symbol Stack
S ? X (1) X ? (X ) (2) X ? ( ) (3)
s0
X
91Step Three Use Goto, Push New State
Grammar
Input
State Stack
Symbol Stack
S ? X (1) X ? (X ) (2) X ? ( ) (3)
s0
X
92Step Three Use Goto, Push New State
Grammar
Input
State Stack
Symbol Stack
S ? X (1) X ? (X ) (2) X ? ( ) (3)
s1
s0
X
93Accept the String!
Grammar
Input
State Stack
Symbol Stack
S ? X (1) X ? (X ) (2) X ? ( ) (3)
s1
s0
S
94Key Concepts
- Pushdown automaton for parsing
- Stack, Finite state control
- Parse actions shift, reduce, accept
- Parse table for controlling parser actions
- Indexed by parser state and input symbol
- Entries specify action and next state
- Use state stack to help control
- Parse tree construction
- Reads input from left to right
- Bottom-up construction of parse tree