Module-3 1
Module-3 1
Module-3 1
BOTTOM – UP PARSING
1
Module 3
Bottom-up Parsing
•Handle Pruning.
•Shift Reduce parsing.
•Operator precedence parsing (Concept only).
•LR parsing –
– Constructing SLR, LALR and canonical LR parsing tables
2
BOTTOM-UP PARSING
• Bottom-up parsing starts from the leaf nodes of a tree and
works in upward direction till it reaches the root node.
3
4
SHIFT-REDUCE PARSER
5
SHIFT-REDUCE PARSER
• Bottom-up parsing approach
– Shift reduce parsing attempts to construct a parse tree for an
input string beginning at the leaves (bottom) and working up
towards the root (top).
– At each reduction step parser searches for substrings of the
working string that match the right side of some production.
– When it finds such a substring, it reduces it, i.e., substitutes the
left side non-terminal for the matching right side. The goal is to
reduce all the way up to the start symbol and report a successful
parse
6
Accept
7
Actions in SRP
• Shift
• Reduce
• Accept
• Error
8
Handle Pruning
• Handle
– A handle of a string is a substring that matches the right side of a production,
and whose reduction to the nonterminal on the left side of the production
represents one step along the reverse of a rightmost derivation.
• Handle Pruning
– The process of discovering a handle and reducing it to appropriate LHS is
called handle pruning.
– Right most derivation in reverse can be obtained by handle pruning.
9
10
• Shift/reduce conflict
11
• Reduce/Reduce conflict
12
• Reduce/reduce conflict
13
OPERATOR PRECEDENCE
PARSER
14
Operator-Precedence Parser
• Bottom-up parser
16
Precedence Table
Consider the grammar:
Input String:
E → E+E
E → E*E id + id * id
E → id
id + * $
. . .
id > > >
+ <. .
> <. .
>
* <. .
> .
> .
>
$ <. <. <.
17
• The intention of the precedence relations is to find
the handle :
<. with marking the left end,
=· appearing in the interior of the handle, and
.
> marking the right hand.
18
To Find The Handles
1. Scan the string from left end until the first .> is encountered.
2. Then scan backwards (to the left) over any =· until a <. is
encountered.
3. The handle contains everything to left of the first .> and to
the right of the <. is encountered.
PUSH
20
Operator-Precedence Parsing Algorithm
• Input: Input string w and a table of precedence
relations
• Output: If w is well formed, a parse tree otherwise
an error indication
21
Algorithm
set ip to point to the first symbol of w$ ;
OPP LE PUSH
repeat forever
if ( $ is on top of the stack and ip points to $ ) then return // Successful Completion
else
{
let a be the topmost terminal symbol on the stack;
let b be the symbol pointed to by ip;
23
Operator Precedence Parsing
• Advantages:
– simple
– powerful enough for expressions in programming
languages
– No problem even if grammar is ambiguous
• Disadvantages:
– It cannot handle the unary minus
– Applicable only for small class of grammars.(operator
grammar)
24
25