Page 1
MIT 6 035MIT 6.035 Introduction to Shift-Reduce Parsing
Martin Rinard Laboratory for Computer Science
Massachusetts Institute of Technology
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 1
Page 2
Orientation
• Specify Syntax Using Context-Free Grammar Expr → Expr Op Expr • Nonterminals • Terminals
Expr → Expr Op Expr Expr → (Expr)
Expr → - Expr • Productions
• Given a grammar, Parser Generator produces a
p p Expr → num Op → +
Generator produces a parser • Starts with input string
Op → -Op → *
• Produces parse tree
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 2
Page 3
t
• w•
Today’s Lecture
• How generated parser works H d• How parser generator produces parser
• Central mechanism Pushdown automaton hich implements Pushdown automaton, which implements
• Shift-reduce parser
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 3
Page 4
t
•
t t t t
C i f
Shift:
A h i i
Pushdown Automata • Consists of
• Pushdown stack (can have terminals and nonterminals) Finite state automaton controlFinite 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
•
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 4
Page 5
p
* ( + )
Shift -R e duce P a rser Exam p lep Expr → Expr Op Expr
Expr → ( Expr) Expr → - Expr
Expr → num Op → +
Stack
Op → -Op → *
Input String
* ( + num )numnum
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 5
Page 6
p
* ( + )
Shift -R e duce P a rser Exam p lep Expr → Expr Op Expr
Expr → ( Expr) Expr → - Expr
Expr → num Op → + Op → -Op → *
* ( + num )numnum
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 6
Page 7
p
* ( + )
Shift -R e duce P a rser Exam p lep Expr → Expr Op Expr
Expr → ( Expr) Expr → - Expr
Expr → num Op → + Op → -Op → *
FTSH
IF
* ( + num )numnum
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 7
Page 8
p
* ( + )
Shift -R e duce P a rser Exam p lep Expr → Expr Op Expr
Expr → ( Expr) Expr → - Expr
Expr → num Op → +
num Op → -Op → *
FTSH
IF
* ( + num )num
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 8
Page 9
Shift-Reduce Parser ExamplepExpr → Expr Op Expr
Expr → (Expr)Expr → - Expr
Expr → numOp → +
num
pOp → -Op → *
UC
E
* ( + )RE
DU
* ( + num )numR
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 9
Page 10
Expr → nump
* ( + )
Shift -R e duce P a rser Exam p lep Expr → Expr Op Expr
Expr → ( Expr)
Op → -Op → *Expr
UC
E
num
RE
DU
* ( + num )numR
Expr → - ExprExpr → num
Op → +
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 10
Page 11
p
* ( + )
Shift -R e duce P a rser Exam p lep Expr → Expr Op Expr
Expr → ( Expr) Expr → - Expr
Expr → num Op → + Op → -Op → *Expr
FT
numSHIF
* ( + num )num
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 11
Page 12
p
( + )
Shift -R e duce P a rser Exam p lep Expr → Expr Op Expr
Expr → ( Expr) Expr → - Expr
Expr → num Op → + Op → -Op → *Expr
*
FT
numSHIF
( + num )num
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 12
Page 13
Shift-Reduce Parser ExamplepExpr → Expr Op Expr
Expr → (Expr)Expr → - Expr
Expr → numOp → +pOp → -Op → *Expr
Op
UC
E
( + )
num
RE
DU
*
( + num )numR
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 13
Page 14
p
( + )
Shift -R e duce P a rser Exam p lep Expr → Expr Op Expr
Expr → ( Expr) Expr → - Expr
Expr → num Op → + Op → -Op → *Expr
Op
FT
num *SHIF
( + num )num
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 14
Page 15
p
+ )
Shift -R e duce P a rser Exam p lep Expr → Expr Op Expr
Expr → ( Expr) Expr → - Expr
Expr → num Op → +( Op → -Op → *Expr
Op
FT
num *SHIF
+ num )num
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 15
Page 16
p
+ )
Shift -R e duce P a rser Exam p lep Expr → Expr Op Expr
Expr → ( Expr) Expr → - Expr
Expr → num Op → +( Op → -Op → *Expr
Op
FT
num *SHIF
+ num )num
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 16
Page 17
p
+ )
Shift -R e duce P a rser Exam p lep Expr → Expr Op Expr
Expr → ( Expr) Expr → - Expr
Expr → num Op → +(
num
Op → -Op → *Expr
Op
FT
num *SHIF
+ num )
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 17
Page 18
p
FT
+ )
SHIFShift -R e duce P a rser Exam p lep
Expr → Expr Op Expr Expr → ( Expr) Expr → - Expr
Expr → num Op → +(
Expr
Op → -Op → *Expr
Op
UC
E
num * num
RE
DU
+ num )R
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 18
Page 19
p
+ )
Shift -R e duce P a rser Exam p lep Expr → Expr Op Expr
Expr → ( Expr) Expr → - Expr
Expr → num Op → +(
Expr
Op → -Op → *Expr
Op
FT
num *SHIF
num
+ num )
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 19
Page 20
p
)
Shift -R e duce P a rser Exam p lep Expr → Expr Op Expr
Expr → ( Expr)+ Expr → - Expr
Expr → num Op → +(
Expr
Op → -Op → *Expr
Op
FT
num *SHIF
num
num )
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 20
Page 21
p
FT
)
SHIFShift -R e duce P a rser Exam p lep
Expr → Expr Op Expr Expr → ( Expr)Op Expr → - Expr
Expr → num Op → +(
Expr
Op → -Op → *Expr
Op
UC
E
num * num
RE
DU
+
num )R
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 21
Page 22
p
)
Shift -R e duce P a rser Exam p lep Expr → Expr Op Expr
Expr → ( Expr)Op Expr → - Expr
Expr → num Op → +(
Expr
Op → -Op → *Expr
Op
FT
num *SHIF
num +
num )
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 22
Page 23
p
)
Shift -R e duce P a rser Exam p lep Expr → Expr Op Expr
Expr → ( Expr) num Op
Expr → - Expr Expr → num
Op → +( Expr
Op → -Op → *Expr
Op
FT
num *SHIF
num +
)
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 23
Page 24
p
FT
)
SHIFShift -R e duce P a rser Exam p lep
Expr → Expr Op Expr Expr → ( Expr)Op
Expr
Expr → - Expr Expr → num
Op → +( Expr
Op → -Op → *Expr
Op
UC
E
num * num +
RE
DU
num
)R
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 24
Page 25
Shift-Reduce Parser ExamplepExpr → Expr Op Expr
Expr → (Expr)Expr → - Expr
Expr → numOp → +(
ExprExpr
Expr
pOp → -Op → *Expr
OpExprOp
FTUC
E
)
num *SHIF
num +
RE
DU
num
)R
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 25
Page 26
p
)
Shift -R e duce P a rser Exam p lep Expr → Expr Op Expr
Expr → ( Expr) Expr → - Expr
Expr → num Op → +( Expr
Expr
Op → -Op → *Expr
Op Expr
Op
FT
num *SHIF
num + num
)
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 26
Page 27
p
Shift -R e duce P a rser Exam p lep Expr → Expr Op Expr
Expr → ( Expr)) Expr → - Expr
Expr → num Op → +( Expr
Expr
Op → -Op → *Expr
Op Expr
Op
FT
num *SHIF
num + num
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 27
Page 28
Shift-Reduce Parser ExamplepExpr → Expr Op Expr
Expr → (Expr)Expr → - Expr
Expr → numOp → +E
)Expr p
Op → -Op → *Expr
Op( Op
ExprExpr
UC
E Expr
RE
DU
num * num + num
R
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 28
Page 29
p
Shift -R e duce P a rser Exam p lep Expr → Expr Op Expr
Expr → ( Expr) Expr → - Expr
Expr → num Op → +E
)Expr
Op → -Op → *
Expr
Op
( Op
Expr Expr
Expr
UC
E Expr
RE
DU
num * num + num
R
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 29
Page 30
p
Shift -R e duce P a rser Exam p lep Expr → Expr Op Expr
Expr → ( Expr) Expr → - Expr
Expr → num Op → +E
)Expr
Op → -Op → *
Expr
Op
( Op
Expr Expr
Expr
EPT
! Expr
AC
CE
num * num + num
A
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 30
Page 31
t t t t
• s sequences•
Basic Idea
• Goal: reconstruct parse tree for input string R d i f l f i h• Read input from left to right
• Build tree in a bottom-up fashion Use tack to hold pending of terminals Use stack to hold pending sequences of terminals and nonterminals
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 31
Page 32
•
•
t t t t
Potential Conflicts
• Reduce/Reduce Conflict T f h k h RHS f l i l• Top of the stack may match RHS of multiple productions
• Which production to use in the reduction?Which production to use in the reduction? • Shift/Reduce Conflict
• Stack may match RHS of productionStack may match RHS of production • But that may not be the right match • May need to shift an input and later find aMay need to shift an input and later find a
different reduction
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 32
Page 33
→
+
Conflicts
•Original Grammar •New Grammar Expr → Expr Op Expr Expr → ( Expr)
Expr → Expr Op Expr Expr → Expr - Expr Expr ( Expr )
Expr → - Expr Expr → num O
Expr → ( Expr )
Expr → Expr -Expr → num Op → +
Op → -Op → *
Expr → num Op → + Op → -p Op → *
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 33
Page 34
*
p
Conflicts Expr → Expr Op Expr
Expr → Expr - Expr Expr → ( Expr ) Expr → Expr -
Expr → num Op → + Op → -Op → *Op →
- numnum
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 34
Page 35
*
p
Conflicts Expr → Expr Op Expr
Expr → Expr - Expr Expr → ( Expr ) Expr → Expr -
Expr → num Op → + Op → -Op → *
FT
Op →
SHIF
- numnum
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 35
Page 36
*
p
Conflicts Expr → Expr Op Expr
Expr → Expr - Expr Expr → ( Expr ) Expr → Expr -
Expr → num
num Op → + Op → -Op → *
FT
Op →
SHIF
- num
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 36
Page 37
*
p
FTSH
IFConflicts
Expr → Expr Op Expr Expr → Expr - Expr
Expr → ( Expr ) Expr → Expr -
Expr → num
Expr Op → + Op → -Op → *
UC
E Op →
RE
DU
num
- numR
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 37
Page 38
*
p
Conflicts Expr → Expr Op Expr
Expr → Expr - Expr Expr → ( Expr ) Expr → Expr -
Expr → num
Expr Op → + Op → -Op → *
FT
Op →
SHIF
num
- num
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 38
Page 39
*
p
Conflicts Expr → Expr Op Expr
Expr → Expr - Expr Expr → ( Expr ) Expr → Expr -
Expr → num
Expr
- Op → + Op → -Op → *
FT
Op →
SHIF
num
num
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 39
Page 40
*
p
Shift/Reduce/Reduce Conflict Expr → Expr Op Expr
Expr → Expr - ExprOptions: Expr → (Expr) Expr → Expr -
Expr → num
Options: Reduce Reduce
Expr
- Op → + Op → -Op → *
Shift
Op →
num
num
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 40
Page 41
Shift/Reduce/Reduce ConflictExpr → Expr Op Expr
Expr → Expr - ExprWhat Happens if Expr → (Expr)Expr → Expr -
Expr → num
What Happens if ChooseReduce
Expr-
pOp → +Op → -Op → *Op → *
UC
E
num
RE
DU
numR
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 41
Page 42
*
p
Shift/Reduce/Reduce Conflict Expr → Expr Op Expr
Expr → Expr - ExprWhat Happens if Expr → (Expr) Expr → Expr -
Expr → num
What Happens if Choose Reduce
Op → + Op → -Op → *
Expr
Expr Op →
T
num -
SHIF
T
numS
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 42
Page 43
*
p
Shift/Reduce/Reduce Conflict Expr → Expr Op Expr
Expr → Expr - ExprWhat Happens if Expr → (Expr) Expr → Expr -
Expr → num
What Happens if Choose Reduce
Op → + Op → -Op → *
Expr
num
Expr Op →
T
num -
SHIF
TS
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 43
Page 44
*
p
Shift/Reduce/Reduce Conflict Expr → Expr Op Expr
Expr → Expr - ExprWhat Happens if Expr → (Expr) Expr → Expr -
Expr → num
What Happens if Choose Reduce
Op → + Op → -Op → *
Expr
Expr
Expr Op →
UC
E
num -
RE
DU
num
R
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 44
Page 45
*
p
Shift/Reduce/Reduce Conflict Expr → Expr Op Expr
Expr → Expr - ExprWhat Happens if Expr → (Expr) Expr → Expr -
Expr → num
What Happens if Choose Reduce
Op → + Op → -Op → *
Expr
Expr
Expr Op →
S!
num -
FAIL
S
num
F
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 45
Page 46
*
p
Shift/Reduce/Reduce Conflict Expr → Expr Op Expr
Expr → Expr - ExprBoth of These Expr → (Expr) Expr → Expr -
Expr → num
Both of These Actions Work
Reduce
Expr
- Op → + Op → -Op → *
Shift
Op →
num
num
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 46
Page 47
*
p
Shift/Reduce/Reduce Conflict Expr → Expr Op Expr
Expr → Expr - ExprWhat Happens if Expr → (Expr) Expr → Expr -
Expr → num
What Happens if Choose
Reduce
Expr
- Op → + Op → -Op → *Op →
num
num
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 47
Page 48
*
p
Shift/Reduce/Reduce Conflict Expr → Expr Op Expr
Expr → Expr - ExprWhat Happens if Expr → (Expr) Expr → Expr -
Expr → num
What Happens if Choose
Reduce
Expr
Op Op → + Op → -Op → *Op →
UC
E
num -
RE
DU
numR
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 48
Page 49
*
p
Shift/Reduce/Reduce Conflict Expr → Expr Op Expr
Expr → Expr - ExprWhat Happens if Expr → (Expr) Expr → Expr -
Expr → num
What Happens if Choose
Reduce num
Expr
Op Op → + Op → -Op → *Op →
T
num -
SHIF
TS
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 49
Page 50
*
p
Shift/Reduce/Reduce Conflict Expr → Expr Op Expr
Expr → Expr - ExprWhat Happens if Expr → (Expr) Expr → Expr -
Expr → num
What Happens if Choose
Reduce Expr
Expr
Op Op → + Op → -Op → *Op →
UC
E
num -
RE
DU
num
R
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 50
Page 51
*
p
Shift/Reduce/Reduce Conflict Expr → Expr Op Expr
Expr → Expr - ExprWhat Happens if Expr → (Expr) Expr → Expr -
Expr → num
What Happens if Choose
Reduce
Op Op → + Op → -Op → *
Expr
Expr
Expr Op →
UC
E
num -
RE
DU
num
R
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 51
Page 52
*
p
Shift/Reduce/Reduce Conflict Expr → Expr Op Expr
Expr → Expr - ExprWhat Happens if Expr → (Expr) Expr → Expr -
Expr → num
What Happens if Choose
Reduce
Op Op → + Op → -Op → *
Expr
Expr
Expr Op →
EPT
num -
AC
CE
num
A
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 52
Page 53
*
p
Conflicts Expr → Expr Op Expr
Expr → Expr - ExprWhat Happens if Expr → (Expr) Expr → Expr -
Expr → num
What Happens if Choose
Shift
Expr
- Op → + Op → -Op → *Op →
FT
num
SHIF
num
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 53
Page 54
*
p
Conflicts Expr → Expr Op Expr
Expr → Expr - ExprWhat Happens if Expr → (Expr) Expr → Expr -
Expr → numnum
What Happens if Choose
Shift
Expr
- Op → + Op → -Op → *Op →
FT
num
SHIF
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 54
Page 55
*
p
Conflicts Expr → Expr Op Expr
Expr → Expr - ExprWhat Happens if Expr → (Expr) Expr → Expr -
Expr → numExpr
What Happens if Choose
Shift
Expr
- Op → + Op → -Op → *Op →
UC
E
num
RE
DU
num
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 55
Page 56
*
p
Conflicts Expr → Expr Op Expr
Expr → Expr - ExprWhat Happens if Expr → (Expr) Expr → Expr -
Expr → num
What Happens if Choose
Shift Op → + Op → -Op → *
ExprExpr
Expr
Op →
UC
E
num -
RE
DU
num
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 56
Page 57
*
p
Conflicts Expr → Expr Op Expr
Expr → Expr - ExprWhat Happens if Expr → (Expr) Expr → Expr -
Expr → num
What Happens if Choose
Shift Op → + Op → -Op → *
ExprExpr
Expr
Op →
EPT
num -
AC
CE
num
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 57
Page 58
*
/
p
Shift/Reduce/Reduce Conflict Expr → Expr Op Expr
Expr → Expr - ExprThis Shift/Reduce Conflict Expr → (Expr) Expr → Expr -
Expr → num
Reflects Ambiguity in Grammar
Expr
- Op → + Op → -Op → *Op →
num
num
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 58
Page 59
*
-/
p
Shift/Reduce/Reduce Conflict Expr → Expr Op Expr
Expr → Expr ExprThis Shift/Reduce Conflict Expr → (Expr) Expr → Expr -
Expr → num
Reflects Ambiguity in Grammar
Expr
- Op → + Op → -Op → *Op →
Eliminate by Hacking Grammar
num Grammar
num
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 59
Page 60
*
/
p
Shift/Reduce/Reduce Conflict Expr → Expr Op Expr
Expr → Expr - ExprThis Shift/Reduce Expr → (Expr) Expr → Expr -
Expr → num
Conflict Can Be Eliminated By Lookahead of One
Expr
- Op → + Op → -Op → *
Symbol
Op →
Parser Generator Should Handle It
num
num
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 60
Page 61
Which Production to Reduce
Constructing a Parser
• We will construct version with no lookahead • Key DecisionsKey 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
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 61
Page 62
State Stack finite state
Parser State
• Input Token Sequence ($ for end of input) • Current State from Finite State AutomatonCurrent State from Finite State Automaton • Two Stacks
• State Stack (implements finite state automaton)(implements automaton) • Symbol Stack (terminals from input and
nonterminals from reductions)
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 62
Page 63
•
t t t t
••
• s•
Integrating Finite State Control
• Actions P h S b l d S O S k• Push Symbols and States Onto Stacks
• Reduce According to a Given Production AcceptAccept
• Selected action is a function of Current input Current input
• Current state of finite state control • Each action specifies next stateEach action specifies next state • Implement control using parse table
symbol
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 63
Page 64
Parse Tables
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s33 hift t 4s3 error shift to s4 error
s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
• Implements finite state control• At each step, look up
T bl [ f k] [ i b l]• Table[top of state stack] [ input symbol]• Then carry out the action
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 64
Page 65
Parse Table Example
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s33 hift t 4s3 error shift to s4 error
s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
S → X $ (1)GrammarInputState Stack Symbol Stack
S → X $ (1)X → (X ) (2)X → ( ) (3)
(())
Xs0 Xs0
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 65
Page 66
Parser Tables
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s33 hift t 4s3 error shift to s4 error
s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
• Shift to sn• Push input token into the symbol stack• Push sn into state stack• Advance to next input symbol
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 66
Page 67
Parser Tables
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s33 hift t 4s3 error shift to s4 error
s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
• Reduce (n)• Pop both stacks as many times as the number
f b l th RHS f lof symbols on the RHS of rule n• Push LHS of rule n into symbol stack
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 67
Page 68
Parser Tables
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s33 hift t 4s3 error shift to s4 error
s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
• Reduce (n) (continued)• Look up
T bl [t f th t t t k][t f b l t k]• Table[top of the state stack][top of symbol stack]• Push that state (in goto part of table) onto state
stackstack
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 68
Page 69
Parser Tables
ACTION GotoState ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s33 hift t 4s3 error shift to s4 error
s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
• Accept• Stop parsing and report success
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 69
Page 70
Parse Table In ActionACTION Goto
State ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s33 hift t 4s3 error shift to s4 error
s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
GrammarInputState Stack Symbol StackS → X $ (1)X → (X ) (2)X → ( ) (3)
(())$
X → ( ) (3)s0
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 70
Page 71
Parse Table In ActionACTION Goto
State ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s33 hift t 4s3 error shift to s4 error
s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
GrammarInputState Stack Symbol StackS → X $ (1)X → (X ) (2)X → ( ) (3)
(())$
X → ( ) (3)s0
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 71
Page 72
Parse Table In ActionACTION Goto
State ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s33 hift t 4s3 error shift to s4 error
s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
GrammarInputState Stack Symbol StackS → X $ (1)X → (X ) (2)X → ( ) (3)
())$
s2 X → ( ) (3)s0 (s2
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 72
Page 73
Parse Table In ActionACTION Goto
State ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s33 hift t 4s3 error shift to s4 error
s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
GrammarInputState Stack Symbol StackS → X $ (1)X → (X ) (2)X → ( ) (3)
())$
s2 X → ( ) (3)s0 (s2
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 73
Page 74
Parse Table In ActionACTION Goto
State ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s33 hift t 4s3 error shift to s4 error
s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
GrammarInputState Stack Symbol StackS → X $ (1)X → (X ) (2)X → ( ) (3)
))$
s2 (s2
X → ( ) (3)s0 (s2 (
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 74
Page 75
Parse Table In ActionACTION Goto
State ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s33 hift t 4s3 error shift to s4 error
s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
GrammarInputState Stack Symbol StackS → X $ (1)X → (X ) (2)X → ( ) (3)
))$
s2 (s2
X → ( ) (3)s0 (s2 (
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 75
Page 76
Parse Table In ActionACTION Goto
State ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s33 hift t 4s3 error shift to s4 error
s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
GrammarInputState Stack Symbol StackS → X $ (1)X → (X ) (2)X → ( ) (3)
)$
s2 (s2s5
)X → ( ) (3)
s0 (s2 (
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 76
Page 77
Parse Table In ActionACTION Goto
State ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s33 hift t 4s3 error shift to s4 error
s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
InputState Stack Symbol Stack Grammar
)$
s2 (s2s5
)S → X $ (1)X → (X ) (2)X → ( ) (3)
s0 (s2 ( X → ( ) (3)
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 77
Page 78
Step One: Pop StacksACTION Goto
State ( ) $ X
p p
s0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s33 hift t 4s3 error shift to s4 error
s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
InputState Stack Symbol Stack Grammar
)$
s2 (s2s5
)S → X $ (1)X → (X ) (2)X → ( ) (3)
s0 (s2 ( X → ( ) (3)
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 78
Page 79
Step One: Pop StacksACTION Goto
State ( ) $ X
p p
s0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s33 hift t 4s3 error shift to s4 error
s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
State Stack Symbol Stack Input Grammar
)$ S → X $ (1)X → (X ) (2)X → ( ) (3)s2 X → ( ) (3)
s0 (s2
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 79
Page 80
Step Two: Push NonterminalACTION Goto
State ( ) $ X
p
s0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s33 hift t 4s3 error shift to s4 error
s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
GrammarInputState Stack Symbol StackS → X $ (1)X → (X ) (2)X → ( ) (3)
)$
s2 X → ( ) (3)s0 (s2
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 80
Page 81
Step Two: Push NonterminalACTION Goto
State ( ) $ X
p
s0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s33 hift t 4s3 error shift to s4 error
s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
GrammarInputState Stack Symbol StackS → X $ (1)X → (X ) (2)X → ( ) (3)
)$
s2 X X → ( ) (3)s0 (s2 X
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 81
Page 82
Step Three: Use Goto, Push New StateACTION Goto
State ( ) $ X
p ,
s0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s33 hift t 4s3 error shift to s4 error
s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
GrammarInputState Stack Symbol StackS → X $ (1)X → (X ) (2)X → ( ) (3)
)$
s2 X X → ( ) (3)s0 (s2 X
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 82
Page 83
Step Three: Use Goto, Push New StateACTION Goto
State ( ) $ X
p ,
s0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s33 hift t 4s3 error shift to s4 error
s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
GrammarInputState Stack Symbol StackS → X $ (1)X → (X ) (2)X → ( ) (3)
)$
s2 Xs3
X → ( ) (3)s0 (s2 X
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 83
Page 84
Parse Table In ActionACTION Goto
State ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s33 hift t 4s3 error shift to s4 error
s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
GrammarInputState Stack Symbol StackS → X $ (1)X → (X ) (2)X → ( ) (3)
)$
s2 Xs3
X → ( ) (3)s0 (s2 X
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 84
Page 85
Parse Table In ActionACTION Goto
State ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s33 hift t 4s3 error shift to s4 error
s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
GrammarInputState Stack Symbol StackS → X $ (1)X → (X ) (2)X → ( ) (3)
$
s2 Xs3s4
)X → ( ) (3)
s0 (s2 X
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 85
Page 86
Parse Table In ActionACTION Goto
State ( ) $ Xs0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s33 hift t 4s3 error shift to s4 error
s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
GrammarInputState Stack Symbol StackS → X $ (1)X → (X ) (2)X → ( ) (3)
$
s2 Xs3s4
)X → ( ) (3)
s0 (s2 X
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 86
Page 87
Step One: Pop StacksACTION Goto
State ( ) $ X
p p
s0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s33 hift t 4s3 error shift to s4 error
s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
GrammarInputState Stack Symbol StackS → X $ (1)X → (X ) (2)X → ( ) (3)
$
s2 Xs3s4
)X → ( ) (3)
s0 (s2 X
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 87
Page 88
Step One: Pop StacksACTION Goto
State ( ) $ X
p p
s0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s33 hift t 4s3 error shift to s4 error
s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
GrammarInputState Stack Symbol StackS → X $ (1)X → (X ) (2)X → ( ) (3)
$
X → ( ) (3)s0
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 88
Page 89
Step Two: Push NonterminalACTION Goto
State ( ) $ X
p
s0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s33 hift t 4s3 error shift to s4 error
s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
State Stack Symbol Stack Input Grammar
$ S → X $ (1)X → (X ) (2)X → ( ) (3)X → ( ) (3)
s0
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 89
Page 90
Step Two: Push NonterminalACTION Goto
State ( ) $ X
p
s0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s33 hift t 4s3 error shift to s4 error
s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
GrammarInputState Stack Symbol StackS → X $ (1)X → (X ) (2)X → ( ) (3)
$
X → ( ) (3)s0 X
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 90
Page 91
Step Three: Use Goto, Push New StateACTION Goto
State ( ) $ X
p ,
s0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s33 hift t 4s3 error shift to s4 error
s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
GrammarInputState Stack Symbol StackS → X $ (1)X → (X ) (2)X → ( ) (3)
$
X → ( ) (3)s0 X
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 91
Page 92
Step Three: Use Goto, Push New StateACTION Goto
State ( ) $ X
p ,
s0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s33 hift t 4s3 error shift to s4 error
s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
GrammarInputState Stack Symbol StackS → X $ (1)X → (X ) (2)X → ( ) (3)
$
s1 X → ( ) (3)s0 Xs1
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 92
Page 93
Accept the String!ACTION Goto
State ( ) $ X
p g
s0 shift to s2 error error goto s1s1 error error accept s2 shift to s2 shift to s5 error goto s33 hift t 4s3 error shift to s4 error
s4 reduce (2) reduce (2) reduce (2) s5 reduce (3) reduce (3) reduce (3)
GrammarInputState Stack Symbol StackS → X $ (1)X → (X ) (2)X → ( ) (3)
$
s1 X → ( ) (3)s0 Ss1
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 93
Page 94
•
•
t t t t
• c parser a•
• s n s•
Key Concepts
• Pushdown automaton for parsing St k Fi i l• Stack, Finite state control
• Parse actions: shift, reduce, accept Parse table for Parse table for • Indexed by parser state and input symbol
Entries pecify action and ext tateEntries specify action and next state • Use state stack to help control
• Parse tree constructionParse tree construction • Reads input from left to right • Bottom-up construction of parse treeBottom up construction of parse tree
controlling parser actions
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 94
Page 95
MIT OpenCourseWarehttp://ocw.mit.edu
6.035 Computer Language Engineering Spring 2010
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.
Source: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-035-computer-language-engineering-spring-2010/lecture-notes/ Saylor Course: http://www.saylor.org/courses/cs304/
The Saylor Foundation 95