Professional Documents
Culture Documents
Bottom up parsing
Shift reduce parsing
Reduced to
String Start Symbol
Data Structures
Two data structures are required to
implement a shift-reduce parser-
1. A Stack is required to hold the
grammar symbols.
w$
2. An Input buffer is required to
hold the string to be parsed. $ Input buffer
Stack
Initial Configuration
Working
Initially, shift-reduce parser is
present in the following
configuration where-
• Stack contains only the $ w$
symbol.
• Input buffer contains the input $ Input buffer
string w with $ at its end.
Stack
Initial Configuration
Working
Initially, shift-reduce parser is present in the
following configuration where-
• Stack contains only the $ symbol.
• Input buffer contains the input string with $ at
its end.
The parser works by- w$
• Moving the input symbols on the top of the
stack. $ Input buffer
• Until a handle β appears on the top of the stack.
• The parser keeps on repeating this cycle until an Stack
error is detected.
• Or stack is left with only the start symbol and
Initial Configuration
the input buffer becomes empty.
Final Configuration
After achieving this configuration,
• The parser stops / halts.
• It reports the successful
completion of parsing. $
S
$ Input buffer
Stack
Final Configuration
Possible Actions
A shift-reduce parser can possibly make the following four actions-
1. Shift-
In a shift action,
• The next symbol is shifted onto the top of the stack.
2. Reduce-
In a reduce action,
• The handle appearing on the stack top is replaced with the appropriate non-terminal symbol.
3. Accept-
In an accept action,
• The parser reports the successful completion of parsing.
4. Error-
In this state,
• The parser becomes confused and is not able to make any decision.
• It can neither perform shift action nor reduce action nor accept action
Practice Problem # 1
Grammar Stack Contents Input String Actions
1. E-> 2E2 $ 32423$ Shift num
2. E-> 3E3
3. E -> 4 $3 2423$ Reduce S -> num
$ 32 423$ Shift –
$ 324 23$ Shift (
$ 32E 23$ Shift num
$ 32E2 3$ Reduce S -> num
String $ 3E 3$ Shift +
32423 $ 3E3 $ Shift num
$E $ Reduce by S -> num
$E $ Accept
Practice Problem # 2
Grammar Stack Contents Input String Actions
1. E-> E - E $ id – id x id$ Shift id
2. E-> E x E
3. E -> id $ id – id x id$ Reduce E -> id
$E – id x id$ Shift –
$E- id x id$ Shift id
$ E – id x id$ Shift id
$E–E x id$ Reduce E -> id
String $E–Ex id$ Shift x
id – id x id $ E – E x id $ Shift id
$E–ExE $ Reduce by E -> E x E
$E $ Accept
Practice Problem # 3
Stack Contents Input String Actions
1.S → S+S $ num – (num + num) $ Shift num
2.S → S-S
3.S → (S) $ num – (num + num) $ Reduce S -> num
4.S → num $S (num + num) $ Shift –
$S–( num + num) $ Shift (
$ S – ( num + num) $ Shift num
$S–(S num) $ Reduce S -> num
$S–(S+ )$ Shift +
num – (num + num) $ S – ( S + num $ Shift num
$S–(S+S $ Reduce by S -> num
$S–(S+S) $ Shift
$S–(S) $ Reduce S-> S+S
$S– S $ Reduce S-> S-S
$S $ Accept
Stack Contents Input String Actions
Practice Problem # 4 $
$(
( a , ( a , a ) ) $
a,(a,a))$
Shift
Shift
$(a , ( a , a ) ) $ Reduce S → a
$(S , ( a , a ) ) $ Reduce L → S
$(L,(S , a ) ) $ Reduce L → S
$(L,(L , a ) ) $ Shift
( a, ( a, a ) ) $(L,(L, a ) ) $ Shift
$(L,(L,a ) ) $ Reduce S → a
$ ( L, ( L, S ) ) ) $ Reduce L →L, S
$ ( L, ( L ) ) $ Shift
$ ( L, ( L ) ) $ Reduce S → (L)
$ ( L, S ) $ Reduce L → L, S
Stack Contents Input String Actions
$S $ Accept
Grammar
S –> ( L ) | a
L –> L , S | S
( a, ( a, a ) )
Reference
• https://www.gatevidyalay.com/shift-reduce-parsing/