Bottom Up Parsing
Bottom Up Parsing
Bottom Up Parsers
• Bottom-up parsers build parse trees from the leaves and work up to
the root.
• Bottom-up syntax analysis known as shift-reduce parsing.
• An easy-to-implement shift-reduce parser is called operator precedence
parsing.
• General method of shift-reduce parsing is called LR parsing.
• Shift-reduce parsing attempts to construct a parse tree for an input string
beginning at the leaves (the bottom) and working up towards the root
(the top).
• At each reduction step a particular substring matching the right side of a
production is replaced by the symbol on the left of that production, and
if the substring is chosen correctly at each step, a rightmost derivation is
traced out in reverse.
Example
• Consider the grammar
S → aABe
A → Abc | b
B → d
• The sentence abbcde can be reduced to S by the following steps.
abbcde
aAbcde
aAde
aABe
S
• These reductions trace out the following rightmost derivation in
reverse.
Handles
• A handle of a string is a substring that matches the
right side of a production, and whose reduction to
the non-terminal on the left side of the production
represents one step along the reverse of a
rightmost derivation.
• A handle of a right-sentential form γ is a production
A → β and a position of γ where the string β may be
found and replaced by A to produce the previous
right-sentential form in a rightmost derivation of γ.
Example
Right Sentential Form Handle Reducing Productions
abbcde b A->b
aAbcde Abc A->Abc
aAde d B->D
aABe aABe S->aABe
S
S → aABe
A → Abc | b
B → d
(Grammar)
Handle Pruning
• A rightmost derivation in reverse can be
obtained by handle pruning.
– start with a string of terminals w that is to parse.
– If w is a sentence of the grammar at hand, then
w = γn, where γn is the nth right sentential form of
some as yet unknown rightmost derivation.
S L w
0 rm 1 rm 2 rm rm n 1 rm n
Example for right sentential form and handle for grammar
E→E+E
E→E*E
E→(E)
E → id
Shift Reduce Parsing
• Two problems that must be solved to parse by
handle pruning.
– To locate the substring to be reduced in a right
sentential form.
– To determine what production to chose in case
there is more than one production with that
substring on the right side.
Implementation Of Shift-Reduce Parser
• Use a stack to hold grammar symbols and use $ to mark the bottom of the stack
• Use an input buffer to hold the string w to be parsed and $ for marking the right end of
the input
• Initially the stack is empty, and the string w is on the input, as follows:
Stack Input
$ w $
• The parser operates by shifting zero or more input symbols onto the stack until a handle
β is on top of the stack.
• The parser then reduces β to the left side of the appropriate production.
• The parser repeats this cycle until it has detected an error or until the stack contains the
start symbol and the input is empty:
Stack Input
$S $
• After entering this configuration, the parser halts and announces successful completion
of parsing.
Possible Actions
• In a shift action, the next symbol is shifted onto the top
of the stack.
• In a reduce action, the parser knows the right end of
the handle is at the top of the stack. It must then
locate the left end of the handle within the stack and
decide with what non-terminal to replace the handle.
• In an accept action, the parser announces successful
completion of parsing.
• In an error action, the parser discovers that a syntax
error has occurred and calls an error recovery routine.
Example
E→E+E
E→E*E
E→(E)
E → id
Exercise
• Indicate the handle in each of the following
right sentential forms for the grammars
– 000111 for the grammar S-> 0S1 | 01 ,
– aaa*a+a+ for gramamer S-> SS+ | SS* | a
Conflicts
• There are CFGs for which shift-reduce parsing
cannot be used.
• For every shift-reduce parser for such grammar
can reach a configuration in which
– the parser cannot decide whether to shift or to
reduce (a shift-reduce conflict),
– The parser cannot decide which of several reductions
to make (a reduce/reduce conflict), by knowing the
entire stack contents and the next input symbol.
Example of such grammars:
S’->S
S-> AA
S-> AA
A->aA | b
A->aA | b
S’->.S A
A
S-> .AA S-> A.A S->AA.
A->.aA|.b A->.aA|.b
a b
a
b A->a.A A
A->.aA|.b A->aA.
A->b. a
b
Start with augmented production in first state with a dot in the beginning of RHS. i.e. S’->.S. Now for
closure, the production of the non-terminal followed by . is added in the state. Thus S->.AA is added
which further on add the production starting with A i.e. A->.aA|.b. Since there is no non-terminal
after the . In RHS of this production. Closure of the state is complete.
Canonical collection of LR(0) items
S S1
S’->S.
S’->.S A
S0 A
S-> .AA S-> A.A S->AA.
S5
A->.aA|.b S2
A->.aA|.b
a b
a
b A->a.A A
S3
A->.aA|.b S6
A->aA.
A->b.
S4 a
b
Parsing Table for LR(0) Parser
ACTION GOTO Stack Symbols Input Action
a b $ A S 0 aabb$ Shift3
S0 s3 S4 2 1 03 a abb$ Shift3
S1 acc 033 a bb$ Shift4
S2 S3 S4 5 0334 b b$ Reduce A-
>b
S3 s3 S4 6
0336 A b$ Reduce A-
S4 R3 R3 R3 >aA
S5 R1 R1 R1
036 A b$ Reduce A-
S6 R2 R2 R2 >aA
02 A b$ Shift 4
024 $ Reduce A-
1. S-> AA >b
2 .A->aA
3. A->b 025 A $ Reduce S-
And string is aabb >AA
01 S $ accept
Difference between LR(0) and SLR(1)
parser
• While constructing table, difference lies in the
placement of reduce operations.
• For a reduce operation, check the LHS of the
production in final state of canonical collection
of LR(0) items.
• Place the reduce operation only under the
symbols which are in follow of symbol on LHS.
Similarity between LR(0) and SLR(1)
S’->S
S-> AA
S-> AA
A->aA | b
A->aA | b