Shift reduce parser

1,575 views 12 slides Apr 16, 2020
Slide 1
Slide 1 of 12
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12

About This Presentation

complete explanation about shift reduce parser in compiler design.


Slide Content

Shift reduce parser SUBMITTED BY TEJVEER SINGH MCA 2 nd Year SUBMITTED TO Dr. VANDANA RANA

Definition and explanation Bottom-up parsing is based on the reverse process to top-down. Instead of expanding successive non-terminals according to production rules, a current string form is collapsed each time until the start non-terminal is reached to predict the legal next symbol. This can be regarded as a series of reductions. This approach is also known as shift-reduce parsing. This is the primary parsing method for many compilers, mainly because of its speed and the tools which automatically generate a parser based on the grammar.

Shift reduce parser requires mainly two data structures: 1. A input buffer for storing the input string. 2. A stack for storing and accessing the production rules.

WORKING Initially , shift-reduce parser is present in the following configuration Stack contains only the $ symbol . 2. Input buffer contains the input string with $ at its end.

. After achieving this configuration, The parser stops / halts. It reports the successful completion of parsing. An error is detected. Or stack is left with only the start symbol and the input buffer becomes empty.

Basic Operations – Shift:   This involves moving of symbols from input buffer onto the top of the stack. Reduce:   The handle appearing on the stack top is replaced with the appropriate non-terminal symbol.

Accept:   If only start symbol is present in the stack and the input buffer is empty then, the parsing action is called accept. When accept action is obtained, it means successful parsing is done. Error:   In this situation parser becomes confused. Since the parser can neither perform shift action nor reduce action and not even accept action .

Rules To Remember If the priority of incoming operator is more than the priority of in stack operator, then shift action is performed. It is important to remember the following rules while performing the shift-reduce action If the priority of in stack operator is same or less than the priority of in stack operator, then reduce action is performed.

Problem-01 Consider the following grammar- E → E – E E → E * E E  → id Parse the input string “ id – id * id “ using a shift-reduce parser. Solution :- The priority order is : id > * > – $ id – id * id $ Shift $ id – id * id $ Reduce E → id $ E – id * id $ Shift $ E – id * id $ Shift $ E – id * id $ Reduce E → id $ E – E * id $ Shift $ E – E * id $ Shift $ E – E * id $ Reduce E → id $ E – E * E $ Reduce E → E * E

Problem 02: –  Consider the grammar         S –> S + S         S –> S * S         S –> id Perform Shift Reduce parsing for input string “id + id + id”. SOLUTION:-

Problem 03: –  Consider the grammar         E –> 2E2         E –> 3E3         E –> 4 Perform Shift Reduce parsing for input string “32423”. Solution:-

THANK YOU