0% found this document useful (0 votes)
15 views9 pages

Session 12

Uploaded by

sindhu sarla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views9 pages

Session 12

Uploaded by

sindhu sarla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

COMPILER DESIGN

COURSE CODE:21CS3204R

CO2: TOPIC: SHIFT REDUCE PARSING


 Shift-reduce parsing is a form of bottom-up parsing in which a stack holds grammar
symbols and an input buffer holds the rest of the string to be parsed.
 We use $ to mark the bottom of the stack and also the right end of the input. And it makes
use of the process of shift and reduce actions to accept the input string.
 Here, the parse tree is Constructed bottom up from the leaf nodes towards the root node.
 When we are parsing the given input string, if the match occurs the parser takes the
reduce action otherwise it will go for shift action. And it can accept ambiguous
grammars also.

2
• Basic Operations –
• Shift: This involves moving symbols from the input buffer onto the stack.
• Reduce: If the handle appears on top of the stack then, its reduction by using appropriate
production rule is done i.e. RHS of a production rule is popped out of a stack and LHS of
a production rule is pushed onto the stack.
• Accept: If only the start symbol is present in the stack and the input buffer is empty then,
the parsing action is called accept. When accepted action is obtained, it is means
successful parsing is done.
• Error: This is the situation in which the parser can neither perform shift action nor
reduce action and not even accept action.
3
For example:
consider the below grammar to accept the input string “id * id” using S-R parser
E→E+T|T
T→T*F | F
F→(E)|id
Actions of the Shift-reduce parser using Stack implementation

4
STACK INPUT ACTION

$ Id*id$ Shift

$id *id$ Reduce with F d

$F *id$ Reduce with T F

$T *id$ Shift

$T* id$ Shift

$T*id $ Reduce with F id

$T*F $ Reduce with T T*F

$T $ Reduce with E T

$E $ Accept

5
EX:2

Consider the following grammar:


S→aAcBe
A→Ab|b
B→d Let the input string is “abbcde”.
The series of shift and reductions to the start symbol are as follows.
abbcde→ aAbcde→ aAcde→ aAcBe→ S
Note: in the above example there are two actions possible in the second Step, these are as follows :
Shift action going to 3rd Step
Reduce action, that is A->b
If the parser is taking the 1st action then it can successfully accepts the given input string, if it is going for second action
then it can‘t accept given input string. This is called shift reduce conflict. Where, S-R parser is not able take proper
decision, so it not recommended for parsing.

6
CONFLICTS DURING SHIFTREDUCE PARSING

There is two types of conflicts:


1. shift-reduce conflict (SR conflict)
2. reduce – reduce conflict (RR) conflict.

7
 It is caused when the grammar allows a rule to be reduced for particular token, but, at the
same time, allowing another rule to be shifted for that same token.
 Reduce Conflits:
 the parser always reduces by the rule that was given first in the rules section of the yacc
input.
 Again, the conflict is reported in the yacc output so that users can ensure that the choice
is correct. Precedence is not consulted in reduce-reduce conflicts.

8
Thank you

You might also like