Professional Documents
Culture Documents
Bottom-Up Parsing
E + (int) + (int)
E + (E) + (int)
E + (int)
E + (E)
E
A rightmost derivation
in reverse
Reductions
Bottom-up parsing as the process of
"reducing" a string w to the start symbol
of the grammar.
At each reduction step, a substring that
matches the body of a production is
replaced by the non-terminal at the head
of that production.
Handle
abbcde
Find the handle = b at loc. 2
aAbcde
b at loc. 3 is not a handle:
aAAcde
... blocked.
Handle-pruning
The process of discovering a handle & reducing it to the appropriate left-hand side is
called handle pruning.
Handle pruning forms the basis for a bottom-up parsing method.
To construct a rightmost derivation
S = 0 1 2 … n-1 n = w
Apply the following simple algorithm
for i n to 1 by -1
Find the handle Ai i in i
Replace i with Ai to generate i-1
1. S -> 0 S1|01 indicate the handle in each of the following right-sentential forms:
1. 000111
2. 00S11
2. For the grammar S -> S S + | S S * | a indicate the handle in each of the following
right-sentential forms:
1. SSS + a * +
2. SS + a * a+
3. aaa * a + +.
3. Give bottom-up parses for the following input strings
locate a handle
1. Shift until the top of the stack is the right end of a handle
2. Find the left end of the handle and reduce
Back to x - 2 * y
Stack Input Handle Action
0 Goal Expr
$ id - num * id none shift
1 Expr Expr + Term
$ id - num * id 8,1 reduce 8 2 | Expr - Term
$ Factor - num * id 6,1 reduce 6 3 | Term
1. Shift until the top of the stack is the right end of a handle
2. Find the left end of the handle and reduce
Back to x - 2 * y
Stack Input Handle Action
0 Goal Expr
$ id - num * id none shift
1 Expr Expr + Term
$ id - num * id 8,1 reduce 8 2 | Expr - Term
$ Factor - num * id 6,1 reduce 6 3 | Term
1. Shift until the top of the stack is the right end of a handle
2. Find the left end of the handle and reduce
Back to x - 2 * y
Stack Input Handle Action
0 Goal Expr
$ id - num * id none shift
1 Expr Expr + Term
$ id - num * id 8,1 reduce 8 2 | Expr - Term
$ Factor - num * id 6,1 reduce 6 3 | Term
1. Shift until the top of the stack is the right end of a handle
2. Find the left end of the handle and reduce
Back to x - 2 * y
Stack Input Handle Action
0 Goal Expr
$ id - num * id none shift
1 Expr Expr + Term
$ id - num * id 8,1 reduce 8 2 | Expr - Term
$ Factor - num * id 6,1 reduce 6 3 | Term
$ Expr - Term * id
1. Shift until the top of the stack is the right end of a handle
2. Find the left end of the handle and reduce
Back to x - 2 * y
Stack Input Handle Action
0 Goal Expr
$ id - num * id none shift
1 Expr Expr + Term
$ id - num * id 8,1 reduce 8 2 | Expr - Term
$ Factor - num * id 6,1 reduce 6 3 | Term
1. Shift until the top of the stack is the right end of a handle
2. Find the left end of the handle and reduce
Back to x - 2 * y
Stack Input Handle Action
0 Goal Expr
$ id - num * id none shift
1 Expr Expr + Term
$ id - num * id 8,1 reduce 8 2 | Expr - Term
$ Factor - num * id 6,1 reduce 6 3 | Term
1. Shift until the top of the stack is the right end of a handle
2. Find the left end of the handle and reduce
Back to x - 2 * y
Stack Input Action
$ id - num * id shift
Goal
$ id - num * id reduce 8
$ Factor - num * id reduce 6
$ Term - num * id reduce 3 Expr
$ Expr - num * id shift
Expr – Term
$ Expr - num * id shift
$ Expr - num * id reduce 7
$ Expr - Factor * id reduce 6 Term Term * Fact.
$ Expr - Term * id shift
$ Expr - Term * id shift Fact. Fact. <id,y>
$ Expr - Term * id reduce 8
$ Expr - Term * Factor reduce 4 <id,x> <num,2>
$ Expr - Term reduce 2
$ Expr reduce 0 Corresponding Parse Tree
$ Goal accept
Conflicts During Shift-Reduce Parsing
Conflicts
“shift/reduce” or “reduce/reduce”
Example:
stmt if expr then stmt
We can’t tell
| if expr then stmt else stmt
whether it is a
handle | other (any other statement)
Stack Input
if … then stmt else … Shift/ Reduce Conflict
LR Parsing
Input buffer
A + B $
X
Parsing Program
Y
output
stack Z
$
Parse Table
Action Goto
Techniques for Creating Parse Table
SLR: Construct parsing table for small set of grammars called
SLR(1).
Easy to develop.
CLR(CANONICAL LR) : Most powerful.
Generates a large parse table.
More difficult develop.
Works for all types of CFG
May detect position of error.
LALR(LOOK AHEAD LR) :Widely used method.
Optimizes the size of parse table, by combining some states.
Information may get lost.
SLR Parsers
1. Formation of augmented grammar G’ for
the given grammar G
2. Construction of LR(0) collection of
items.
To find LR(0) collection of items Closure(I)
and Goto(I,X) have to be computed.
3. Finding first and follow of non-terminals
4. Construction of parse table.
Formation of Augmented
Grammar
The
augmented grammar G’, is G with a new start
symbol S’ and an additional production S’ -> S
E->E+T|T
T->T*F|F
The augmented grammar G’ is given by
E’->E
E->E+T|T
T->T*F|F
Items and the LR(O)
Automaton
How does a shift-reduce parser know when
to shift and when to reduce?
How does the parser know that symbol on the
top of the stack is not a handle?
An LR parser makes shift-reduce decisions
by maintaining states to keep track of all the
operations.
States represent sets of "items."
Items and the LR(O)
Automaton
An LR(O) item of a grammar G is a production of G, with a
dot at some position in the body.
Production A -> XYZ yields the four items
A -> ·XYZ
A -> X·YZ
A -> XY·Z
A -> XYZ·
A ->ε generates only one item, A ->.
An item indicates how much of a production we have seen
at a given point in the parsing process.
Items and the LR(O)
Automaton
The item A -> ·XYZ indicates that we hope to see a string derivable from XYZ next
on the input.
Item A -> X·YZ indicates that we have just seen on the input a string derivable from
X and that we hope next to see a string derivable from YZ.
Item A -> XY·Z indicates that we have just seen on the input a string derivable from
XY and that we hope next to see a string derivable from Z.
Item A -> XYZ· indicates that we have seen the body XY Z and that it may be time
to reduce XYZ to A
Items and the LR(O)
Automaton
Collection of sets of LR(0) items, called the
canonical LR(0) collection.
Provides the basis for constructing a deterministic
finite automaton that is used to make parsing
decisions.
Automaton is called an LR(0) automaton.
Each state of the LR(0) automaton represents a
set of items in the canonical LR(0) collection.
Construction of LR(0) Items