Unit 3 - Sessions 1, 2 - 3

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 28

18CSC304J

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

A bottom parse for the string id * id is shown below

Step 1 Step 2 Step 3 Step 4 Step 5 Step 6


REDUCTIONS
REDUCTION

• 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

• Key decisions during bottom-up parsing:

• When to reduce

• What production to apply


REDUCTION – Example 1
• Sequence of reductions for the string id * id for the expression grammar

Step 1 Step 2 Step 3 Step 4 Step 5 Step 6

id * id F * id T * id T*F T E

• The sequence starts with the input string id * id

• The first reduction produces F * id by reducing the leftmost id to F using the production F → id

• In every step, a substring is identified and is reduced


REDUCTION – Example 2
Consider the grammar
Rightmost Derivation 1 Rightmost Derivation 2
S → aB | bA S aB aaBB S aB aaBB
A → a | aS | bAA aaBaBB aaBb
aaBaBb aabSb
B → b | bS | aBB aaBabb aabaBb
Reduce the string aababb aababb aababb

aababb aaBabb aaBaBb aaBaBB aaBB aB S


(Productions used) B→b B→b B→b B → aBB B → aBB S → aB

aababb aabaBb aabSb aaBb aaBB aB S


(Productions used) B→b S → aB B → bS B→b B → aBB S → aB
REDUCTION – Example 3

Consider the grammar


Rightmost Derivation
S → aABe S aABe aAde
aAbcde
A → Abc | b
abbcde
B→d

Reduce the string abbcde

abbcde aAbcde aAde aABe S


(Productions used) A→b A → Abc B→d S → aABe
HANDLE PRUNING
Handles
• Informally, a handle is a substring that matches the
body of a production, and whose reduction represents
one step along the reverse of a rightmost derivation

• Formally, 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 γ

• Always, the string w to the right of the handle


contains only terminal symbols

• If a grammar is ambiguous with more than one


rightmost derivation, then a right sentential form may
have more than one handle
Handles – Example 1
• Consider the expression grammar E⇒T
E→E+T|T For reference: ⇒T*F
Rightmost derivation for ⇒ T * id2
T→T*F|F the string id1 * id2 ⇒ F * id2
⇒ id1 * id2
F → ( E ) | 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 handle of each right-sentential form is underlined


Handle Pruning
• A rightmost derivation in reverse can be obtained by handle pruning

• 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

• Start with the string of terminals to be parsed,

To construct this derivation in reverse order

1. Locate the handle βn in γn

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

4. Halt and announce successful completion of parsing


Handle Pruning – Example
• Consider the expression grammar
E → E + E | E * E | ( E ) | id

Show the process of handle pruning for the string id1 + id2 * id3

Rightmost Derivation Right Sentential Form Handle Reducing


E⇒E*E Production
⇒ E * id3 id1 + id2 * id3 id1 E → id
E + id2 * id3 id2 E → id
⇒ E + E * id3
E + E * id3 E+E E→E+E
⇒ E + id2 * id3
E * id3 id3 E → id
⇒ id1 + id2 * id3 E*E E*E E→E*E
E
SHIFT REDUCE
PARSING
Shift Reduce Parsing
• Shift-reduce parsing is a form of bottom-up parsing in which
• A stack is used to hold the grammar symbols
• An input buffer is used to hold the string w to be parsed
• $ is used to mark the bottom of the stack and also the right of the input
• The parser operates as follows:
• Initially, the stack is empty, and the string w is on the input buffer as follows:

Initial Configuration

• Scan the input from left to right and do the following:


1. Shift zero or more input symbols onto the stack until a handle β is on the top of the
stack
2. Reduce β to the left side of the appropriate production
3. Repeat steps 1 & 2 until the stack contains the start symbol and the input is empty

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

3. ACCEPT : Announce successful completion of parsing

4. ERROR : Syntax error is discovered and an error recovery routine is called

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

Parse the string id1 * id2

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

⇒ E + E * id3 $E+E * id3 $ Reduce by E → E + E


$E * id3 $ Shift
⇒ E + id2 * id3
$E* id3 $ Shift
⇒ id1 + id2 * id3
$ E * id3 $ Reduce by E → id
$E*E $ Reduce by E → E * E
$E $ Accept
Shift Reduce Parsing – Example 3
• Consider the expression grammar
E → E + E | E * E | ( E ) | id
Stack Input Output
Parse the string id1 + id2 * id3
$ id1 + id2 * id3 $ Shift
Another Rightmost Derivation $ id1 + id2 * id3 $ Reduce by E → id
$E + id2 * id3 $ Shift
E⇒E+E $E+ id2 * id3 $ Shift
⇒E+E*E $ E + id2 * id3 $ Reduce by E → id

⇒ E + E * id3 $E+E * id3 $ Shift


$E+E* id3 $ Shift
⇒ E + id2 * id3
$ E + E * id3 $ Reduce by E → id
⇒ id1 + id2 * id3
$E+E*E $ Reduce by E → E * E
$E+E $ Reduce by E → E + E
$E $ Accept
Shift Reduce Parsing – Example 4
Consider the grammar
Stack Input Output
S → aABe
$ abbcde$ Shift
A → Abc | b $a bbcde$ Shift

B→d $ab bcde$ Reduce A ->b


$aA bcde$ Shift
Parse the string abbcde
$aAb cde$ Shift
$aAbc de$ Reduce A → Abc
Rightmost Derivation $aA de$ Shift
S aABe $aAd e$ Reduce B → d
aAde $aAB e$ Shift
aAbcde $aABe $ Reduce S → aABe
abbcde $S $ Accept
CONFLICTS DURING SHIFT
REDUCE PARSING
Conflicts during Shift Reduce Parsing

• 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

• If we have a shift-reduce parser in the configuration

• 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

• If the shift reduce parser is in a configuration


• It is evident that the id on the top of the stack must be reduced, but by which production?
• The correct choice is production (5) if p is a procedure, but production (7) if p is an array
• Hence it is a reduce/reduce conflict

You might also like