1 Shift Shift- -Reduce: The Stack Reduce: The Stack Shift Shift- -Reduce: The Stack Reduce: The Stack Left string can implemented as a stack Top of the stack is the Shift pushes a terminal on the stack
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 1/51
1
ShiftShift--Reduce: The StackReduce: The StackShiftShift--Reduce: The StackReduce: The Stack
Left string can implementedas a stack
Top of the stack is the
Shift pushes a terminal on
the stack
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 2/51
2
ShiftShift--Reduce: The StackReduce: The StackShiftShift--Reduce: The StackReduce: The Stack
Left string can implementedas a stack
Top of the stack is the
Shift pushes a terminal on
the stack
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 3/51
3
ShiftShift--Reduce: The StackReduce: The StackShiftShift--Reduce: The StackReduce: The Stack
Left string can implementedas a stack
Top of the stack is the
Shift pushes a terminal on
the stack
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 4/51
4
int + (int) + (int) $ shift
int + (int) + (int) $ reduce E int E + (int) + (int) $ shift 3 times
E + (int ) + (int) $ reduce E int
int
E
stack
+
(
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 5/51
5
ShiftShift--Reduce: The StackReduce: The StackShiftShift--Reduce: The StackReduce: The Stack
Reduce pops zero or moresymbols from the stack
(production rhs) and pushesa non-terminal on the stack
(production lhs)
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 6/51
6
int + (int) + (int) $ shift
int + (int) + (int) $ reduce E int E + (int) + (int) $ shift 3 times
E + (int ) + (int) $ reduce E int
int
E
stack
+
(
E
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 7/51
7
Discovering HandlesDiscovering HandlesDiscovering HandlesDiscovering Handles
A bottom-up parser buildsthe parse tree starting with
its leaves and workingtoward its root
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 8/51
8
Discovering HandlesDiscovering HandlesDiscovering HandlesDiscovering Handles
The upper edge of thispartially contructed parse
tree is called its upper frontier .
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 9/51
9
int + (int) + (int)
E + (int) + (int)
E + (E) + (int)
E + (int)
E + (E)
int + ( int )int + ( )
E E E
E
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 10/51
10
Discovering HandlesDiscovering HandlesDiscovering HandlesDiscovering Handles
At each step, the parser looks for a section of the
upper frontier that matchesright-hand side of some
production.
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 11/51
11
Discovering HandlesDiscovering HandlesDiscovering HandlesDiscovering Handles
When it finds a match, theparser builds a new tree
node with the production¶sleft-hand non-terminal thus
extending the frontier upwards towards the root.
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 12/51
12
Discovering HandlesDiscovering HandlesDiscovering HandlesDiscovering Handles
The critical step isdeveloping an efficient
mechanism that findsmatches along the tree¶s
current frontier .
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 13/51
13
Discovering HandlesDiscovering HandlesDiscovering HandlesDiscovering Handles
Formally, the parser must findsome substring F, of the upper frontier where
1. F is the right-hand side of someproduction A F, and
2. A F is one step in right-mostderivation of input stream
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 14/51
14
Discovering HandlesDiscovering HandlesDiscovering HandlesDiscovering Handles
Formally, the parser must findsome substring F, of the upper frontier where
1. F is the right-hand side of someproduction A F, and
2. A F is one step in right-mostderivation of input stream
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 15/51
15
Discovering HandlesDiscovering HandlesDiscovering HandlesDiscovering Handles
Formally, the parser must findsome substring F, of the upper frontier where
1. F is the right-hand side of someproduction A F, and
2. A F is one step in right-mostderivation of input stream
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 16/51
16
Discovering HandlesDiscovering HandlesDiscovering HandlesDiscovering Handles
We can represent eachpotential match as a pair
£AF,k ³, where k is theposition on the tree¶s
current frontier of the right-end of F.
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 17/51
17
Discovering HandlesDiscovering HandlesDiscovering HandlesDiscovering Handles
The pair £AF,k ³, is calledthe handle of the bottom-up parse.
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 18/51
18
Handle PruningHandle PruningHandle PruningHandle Pruning
A bottom-up parser operatesby repeatedly locating
handles on the frontier of thepartial parse tree and
performing reductions thatthey specify.
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 19/51
19
HandlesHandlesHandlesHandles
The bottom-up parser uses a stack to hold the
frontier. The stack simplifies the
parsing algorithm in twoways.
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 20/51
20
HandlesHandlesHandlesHandles
The bottom-up parser uses a stack to hold the
frontier. The stack simplifies the
parsing algorithm in twoways.
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 21/51
21
HandlesHandlesHandlesHandles
First, the stack trivializes theproblem of managing space
for the frontier � To extend the frontier, the
parser simply pushes thecurrent input token onto thetop of the stack
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 22/51
22
HandlesHandlesHandlesHandles
First, the stack trivializes theproblem of managing space
for the frontier � To extend the frontier , the
parser simply pushes thecurrent input token onto thetop of the stack
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 23/51
23
HandlesHandlesHandlesHandles
Second, the stack ensuresthat all handles occur with
their right end at the top of the stack
� eliminates the need torepresent handle¶s position
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 24/51
24
HandlesHandlesHandlesHandles
Second, the stack ensuresthat all handles occur with
their right end at the top of the stack
� eliminates the need torepresent handle¶s position
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 25/51
25
E + (int) $ shift 3 times
E + (int ) $ reduce E int
E + (E ) $ shift
E + (E) $ red E E+(E)
E $ accept
handle: £Eint ,4³handle: £EE+(E),5³
1 2 3 4
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 26/51
26
ShiftShift--Reduce Parsing AlgorithmReduce Parsing AlgorithmShiftShift--Reduce Parsing AlgorithmReduce Parsing Algorithm
push $ onto stacksym nextToken()
r epeat unti l (sym == $ and the stack
contains exactly Goal on top of $)if a handle for A F on top of stack
pop |F| symbols off the stack
push A onto the stack
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 27/51
27
ShiftShift--Reduce Parsing AlgorithmReduce Parsing AlgorithmShiftShift--Reduce Parsing AlgorithmReduce Parsing Algorithm
else if (sym { $ ) push sym onto stack
sym nextToken()
else /* no handle, no input */
report error and halt
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 28/51
28
Expression Grammar Expression Grammar Expression Grammar Expression Grammar
1 Goal Expr2 Expr Expr + Term3 | Expr ² Term4 | Term
5 Term Term v Fact or6 | Term ¼ Fact or7 | Fact or
8 Fact or num9 | id10 | ( Expr )
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 29/51
29
The states of the bottom-upparser on input
x ² 2 v y(tokenized as id ² num * id) are
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 30/51
30
word Stack Handle Action
1 id - none - shift
2 ² id £Fact or id,1³ r educe
3 ² Fact or £Term Fact or,1³ r educe
4 ² Term £Expr Term,1³ r educe
5 ² Expr - none - shift
6 num Expr ² - none - shift
7 v Expr ² num £Fact or num,3³ r educe
8 v Expr ² Fac
t o
r £Term Fac
t o
r,3³
r educe
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 31/51
31
word Stack Handle Action
9 v Expr ² Term - none - shift
10 id Expr ² Term v - none - shift
11 $ Expr ² Term v id £Fact or id,5³ r educe
12 $ Expr²Term v Fact or £TermTermvFact or,5³ r educe
13 $ Expr ² Term £Expr Expr ² Term,3³ r educe
14 $ Expr £Goal Expr,1³ r educe
15 $ Goal - none - accept
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 32/51
32
HandlesHandlesHandlesHandles
The handle-findingmechanism is the key toeffiecient bottom-up parsing
As it process an input string,the parser must find and
track all potential handles
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 33/51
33
HandlesHandlesHandlesHandles
The handle-findingmechanism is the key toeffiecient bottom-up parsing
As it process an input string,the parser must find and
track all potential handles
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 34/51
34
HandlesHandlesHandlesHandles
For example, every legalinput eventually reduces theentire frontier to grammar¶sgoal symbol
Thus, £Goal Expr,1³ is a
potential handle at the startof every parse
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 35/51
35
HandlesHandlesHandlesHandles
For example, every legalinput eventually reduces theentire frontier to grammar¶sgoal symbol
Thus, £Goal Expr,1³ is a
potential handle at the startof every parse
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 36/51
36
HandlesHandlesHandlesHandles
As the parser builds aderivation, it discovers other
handles At each step, the set of
potential handles representdifferent suffixes that lead to areduction.
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 37/51
37
HandlesHandlesHandlesHandles
As the parser builds aderivation, it discovers other
handles At each step, the set of
potential handles representdifferent suffixes that lead to areduction.
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 38/51
38
HandlesHandlesHandlesHandles
Each potential handlerepresent a string of
grammar symbols that, if seen, would complete the
right-hand side of someproduction.
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 39/51
39
HandlesHandlesHandlesHandles
For the bottom-up parse of the expression grammar
string, we can representthe potential handles thatthe shift-reduce parser should track
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 40/51
40
HandlesHandlesHandlesHandles
Using the placeholder y torepresent top of the stack, thereare nine handles:
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 41/51
41
Same
Handles
1 £Fact or id y³
2 £Term Fact ory³
3 £Expr Term y³
4 £Fact or numy³5 £Term Fact ory³
6 £Fact or id y³
7£Term Term
vFact or
y³8 £Expr Expr ² Term y³
9 £Goal Expr y³
Same
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 42/51
42
HandlesHandlesHandlesHandles
This notation shows that thesecond and fifth handles areidentical, as are first and sixth
It also create a way torepresent the potential of
discovering a handle infuture.
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 43/51
43
HandlesHandlesHandlesHandles
This notation shows that thesecond and fifth handles areidentical, as are first and sixth
It also create a way torepresent the potential of
discovering a handle infuture.
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 44/51
44
HandlesHandlesHandlesHandles
Consider the parser¶s state in step 6:
6 num Expr ² - none - shift
7 v Expr ² num £Fact or num,3³ r educe
8 v Expr ² Fact or £Term Fact or,3³ shift
9 v Expr ² Term - none - shift
10 id Expr ² Term v - none - shift
11 $ Expr ² Term v id £Fact or id,5³ r educe
12 $ Expr²TermvFact or £TermTermvFact or,5³ r educe
13 $ Expr ² Term £ExprExpr²Term,3³ r educe
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 45/51
45
HandlesHandlesHandlesHandles
The parser has recognized Expr ²
6 num Expr ² - none - shift
7 v Expr ² num £Fact or num,3³ r educe
8 v Expr ² Fact or £Term Fact or,3³ shift
9 v Expr ² Term - none - shift
10 id Expr ² Term v - none - shift
11 $ Expr ² Term v id £Fact or id,5³ r educe
12 $ Expr²TermvFact or £TermTermvFact or,5³ r educe
13 $ Expr ² Term £ExprExpr²Term,3³ r educe
It needs to recognize, eventually, a Termbefore it can reduce this part of the frontier
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 46/51
46
HandlesHandlesHandlesHandles
Using the stack-relativenotation, we can representthe parser¶s state asExpr Expr ² y Term
The parser has already
recognized an Expr and a ²
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 47/51
47
HandlesHandlesHandlesHandles
Using the stack-relativenotation, we can representthe parser¶s state asExpr Expr ² y Term
The parser has already
recognized an Expr and a ²
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 48/51
48
HandlesHandlesHandlesHandles
If the parser reaches a statewhere it shifts a Term on topof Expr and ±, it willcomplete the handleExpr Expr ² Term y
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 49/51
49
HandlesHandlesHandlesHandles
How many potential handlesmust the parser recognize?
The right-hand side of eachproduction can have aplaceholder at its start, at its endand between any twoconsecutive symbols
8/7/2019 Lecture 27012011
http://slidepdf.com/reader/full/lecture-27012011 50/51
50
HandlesHandlesHandlesHandles
How many potential handlesmust the parser recognize?
The right-hand side of eachproduction can have aplaceholder at its start, at its endand between any twoconsecutive symbols