What Is Shift Reduce Parser
What Is Shift Reduce Parser
Shift Reduce Parser is a type of Bottom-Up Parser. It generates the Parse Tree from Leaves
to the Root. In Shift Reduce Parser, the input string will be reduced to the starting symbol.
This reduction can be produced by handling the rightmost derivation in reverse, i.e., from
starting symbol to the input string.
Input Buffer
Stack
There are the various steps of Shift Reduce Parsing which are as follows −
There are the various steps of Shift Reduce Parsing which are as follows −
Insert $ at the bottom of the stack and the right end of the input string in Input
Buffer.
Shift − Parser shifts zero or more input symbols onto the stack until the handle is
on top of the stack.
Reduce − Parser reduce or replace the handle on top of the stack to the left side
of production, i.e., R.H.S. of production is popped, and L.H.S is pushed.
Accept − Step 3 and Step 4 will be repeated until it has detected an error or until
the stack includes start symbol (S) and input Buffer is empty, i.e., it contains $.
https://www.tutorialspoint.com/what-is-shift-reduce-parser 1/4
13/09/2024, 13:06 What is Shift Reduce Parser?
Example1 − Perform the Bottom-Up Parsing for the given string on the Grammar, i.e.,
shows the reduction for string abbcde on the following Grammar
S→aABe
A→Abc|b
B→d
It can reduce the string abbcde to the starting symbol S by applying the rightmost
derivation in reverse at each step.
Handle − Each replacement of the Right side of production by the left side in the process
above is known as "Reduction" and each replacement is called "Handle."
E→E+E
E→E*E
E → (E)
E → id
Perform Rightmost Derivation string id1 + id2 * id3. Find Handles at each step.
https://www.tutorialspoint.com/what-is-shift-reduce-parser 2/4
13/09/2024, 13:06 What is Shift Reduce Parser?
S → CC
C → cC
C→d
Check whether input string "ccdd" is accepted or not accepted using Shift-Reduce parsing.
https://www.tutorialspoint.com/what-is-shift-reduce-parser 3/4
13/09/2024, 13:06 What is Shift Reduce Parser?
$ ccdd$ Shift
$c cdd$ Shift
$ cc dd$ Shift
$ ccd d$ Reduce by C → id
$ ccC d$ Reduce by C → cC
$cC d$ Reduce by C → cC
$C d$ Shift
$Cd $ Reduce by C → d
$CC $ Reduce by S → CC
$S $ Accept
∴ At last, Stack contains Starting Symbol S, and input Buffer is empty. It will accept the
string.
https://www.tutorialspoint.com/what-is-shift-reduce-parser 4/4