Professional Documents
Culture Documents
Unit 3 - Sessions 1, 2 - 3
Unit 3 - Sessions 1, 2 - 3
Unit 3 - Sessions 1, 2 - 3
COMPILER DESIGN
UNIT 3
SESSIONS 1 , 2 & 3
Topics that will be covered in this Session
• Bottom-Up Parsing
• Reductions
• Handle Pruning
• Shift Reduce Parsing
• Problems related to Shift Reduce Parsing
• Conflicts during Shift Reduce Parsing
BOTTOM-UP PARSING
Bottom-Up Parsing
• Bottom-up parsing works in the opposite direction of top down parsing
• Bottom-up parsing starts from the leaf nodes of a tree and works in upward direction till it
reaches the root node
• We start from a sentence and then apply production rules in reverse manner in order to reach
the start symbol
• i.e., rightmost derivation is traced out in reverse to reduce the input string to the start
symbol of the grammar
• The general style of bottom-up parsing is also known as shift-reduce parsing
• Some examples of bottom-up parsing are
• Operator Precedence Parsing
• LR Parsing ( L stands for scanning the input from Left to right & R stands for tracing
Rightmost derivation in reverse)
• Simple LR (SLR)
• Canonical LR (CLR)
• Look Ahead LR (LALR)
Bottom-Up Parsing - Example
• Consider the expression grammar
1
E→E+T|T E⇒T
For reference: ⇒T*F 2
T→T*F|F Rightmost derivation & ⇒ T * id
Parse tree for the string 4 3
⇒ F * id
F → ( E ) | id id*id
⇒ id * id 5
• Bottom-up parsing is the process of “reducing” a string w to the start symbol of the grammar
• At each reduction step, a specific substring matching the body of a production is replaced by
the nonterminal at the head of that production
• When to reduce
id * id F * id T * id T*F T E
• The first reduction produces F * id by reducing the leftmost id to F using the production F → id
Find the handles during the parse of the string id1 * id2
Handles – Example 2
• Consider the expression grammar
E → E + E | E * E | ( E ) | id
Find the handles during the parse of the string id1 + id2 * id3
Rightmost Derivation 1 Rightmost Derivation 2
E⇒E+E E⇒E*E
⇒E+E*E ⇒ E * id3
⇒ E + E * id3 ⇒ E + E * id3
⇒ E + id2 * id3 ⇒ E + id2 * id3
⇒ id1 * id2 * id3 ⇒ id1 * id2 * id3
• The process of reducing the given sentence to the start symbol of the grammar by finding the
handles in each step and replacing it with the correct non terminal is called handle pruning
2. Replace βn by the left side of some production An → βn to obtain the (n-1)th right sentential form γn-1
3. Repeat steps 1 and 2 till we get a right sentential form consisting only of the start symbol S
Show the process of handle pruning for the string id1 + id2 * id3
Initial Configuration
Final Configuration
Four Possible Actions of a Shift Reduce Parser
1. SHIFT : The next input symbol is shifted onto the top of the stack
2. REDUCE : When the right end of the handle is at the top of the stack, locate the left end of the
handle within the stack and decide with what non-terminal to relace the handle
Viable Prefixes
• The set of prefixes of the right sentential forms that can appear on the stack of a shift-reduce
parser are called viable prefixes
• It is a prefix of a right-sentential form that does not continue past the right end of the rightmost
handle of that sentential form
PROBLEMS RELATED TO
SHIFT REDUCE PARSING
Shift Reduce Parsing – Example 1
• Consider the expression grammar
E→E+T|T
T→T*F|F
F → ( E ) | id
Rightmost Derivation
E⇒T
⇒T*F
⇒ T * id2
⇒ F * id2
⇒ id1 * id2
Shift Reduce Parsing – Example 2
• Consider the expression grammar
E → E + E | E * E | ( E ) | id
Stack Input Output
Parse the string id1 + id2 * id3
$ id1 + id2 * id3 $ Shift
Rightmost Derivation $ id1 + id2 * id3 $ Reduce by E → id
$E + id2 * id3 $ Shift
E⇒E*E $E+ id2 * id3 $ Shift
⇒ E * id3 $ E + id2 * id3 $ Reduce by E → id
• There are context-free grammars for which shift-reduce parsing cannot be used
• For such a grammar, the shift-reduce parser reaches a configuration in which the parser
knowing the entire stack contents and the next input symbol, faces a conflict
• There are two types of conflicts
1. Shift/Reduce Conflict : The parser reaches a configuration in which it cannot decide
whether to shift or to reduce
2. Reduce/Reduce Conflict : The parser reaches a configuration in which it cannot decide
which of several reduction to make
• Technically, these grammars are not in the LR(k) class of grammars, where k refers to the
number of symbols of lookahead on the input
• Such grammars are called non-LR grammars
• Grammars used in compiling usually fall in the LR(1) class, with one symbol lookahead
Shift/Reduce Conflict : Example
• An ambiguous grammar can never be LR
• For example, consider the dangling-else grammar
• We cannot tell whether if then is the handle, no matter what appears below it on the stack
• Here there is a shift/reduce conflict
Reduce/Reduce Conflict : Example
• Another common setting for conflicts occurs when we know we have a handle, but the stack
contents and the next input symbol are insufficient to determine which production should be
used in a reduction
• Consider the productions involving procedure calls and array references