Unit 2 2
Unit 2 2
ii) S aSe | B
B bBe|C
C cCe | d
STAC INPUT
K
$ w$
SHIFT REDUCE PARSING
• Parser operates by shifting 0 or more input symbols
onto stack until a handle β is on top of stack.
• The parser then reduces β to the left side of the
appropriate production.
• Parser repeats this cycle until it has detected an error or
until the stack contains the start symbol and input is
empty.
STAC INPUT STAC INPUT
K K
$ w$ $S $
(In this configuration, parser
halts and announces
successful completion of
parsing)
SHIFT REDUCE PARSING
ACTIONS
• Shift: Shift the next input symbol onto the top of the stack.
• Reduce. The right end of the string to be reduced must be at
the top of the stack. Locate the left end of the string within
the stack and decide with what nonterminal to replace the
string.
• Accept. Announce successful completion of parsing.
• Error. Discover a syntax error and call an error recovery
routine
SHIFT REDUCE PARSER PROBLEMS
1) Construct a shift reduce parser for the input string “id + id*id”
on the grammar: E E+E| E*E | (E) | id
Ans: Shift (push) symbols on stack until handle found on top of stack. Once
handle is found, apply reduction.
STACK INPUT ACTION
Handles: $ id+id*id$ Shift
• id $id +id*id$ Reduce by Eid
• E+E $E +id*id$ Shift
• E*E $E+ id*id$ Shift
• (E) $E+id *id$ Reduce by Eid
$E+E *id$ Reduce by EE+E
$E *id$ Shift by *
$E* id$ Shift by id
$E*id $ Reduce by Eid
$E*E $ Reduce by EE*E
$E $ Accept
SHIFT REDUCE PARSER PROBLEM-2
2) Design a shift reduce parser for the grammar: S0S1 | 01 for the strings
(i) 000111 (ii) 00S11.
Ans: (i) 000111
Stack Input Reducing production Stack Input Reducing
(ii) 00S11
$ 000111$ Shift production
$ 00S11$ Shift
$0 00111$ Shift
$0 0S11$ Shift
$00 0111$ Shift
$00 S11$ Shift
$000 111$ Shift
$00S 11$ Shift
$0001 11$ Reduce by S01
$00S1 1$ Reduce by S0S1
$00S 11$ Shift
$0S 1$ Shift
$00S1 1$ Reduce by S0S1
$0S1 $ Reduce by S0S1
$0S 1$ Shift
$S $ Accept
$0S1 $ Reduce by S0S1
$S $ Accept