Prerequisite – Parsing | Set 2 (Bottom Up or Shift Reduce Parsers)
Shift Reduce parser attempts for the construction of parse in a similar manner as done in bottom up parsing i.e. the parse tree is constructed from leaves(bottom) to the root(up). A more general form of shift reduce parser is LR parser.
This parser requires some data structures i.e.
- A input buffer for storing the input string.
- A stack for storing and accessing the production rules.
Basic Operations –
- Shift: This involves moving of symbols from 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 production rule is popped out of stack and LHS of production rule is pushed onto the stack.
- 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 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.
Example 1 – Consider the grammar
S –> S + S
S –> S * S
S –> id
Perform Shift Reduce parsing for input string “id + id + id”.
Example 2 – Consider the grammar
E –> 2E2
E –> 3E3
E –> 4
Perform Shift Reduce parsing for input string “32423”.
Following is the implementation in C-
GRAMMAR is - E->2E2 E->3E3 E->4 stack input action $ 32423$ SHIFT $3 2423$ SHIFT $32 423$ SHIFT $324 23$ REDUCE TO E -> 4 $32E 23$ SHIFT $32E2 3$ REDUCE TO E -> 2E2 $3E 3$ SHIFT $3E3 $ REDUCE TO E -> 3E3 $E $ Accept
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
- Difference between Recursive Predictive Descent Parser and Non-Recursive Predictive Descent Parser
- Bottom Up or Shift Reduce Parsers | Set 2
- Difference between Native compiler and Cross compiler
- Incremental Compiler in Compiler Design
- StAX XML Parser in Java
- Recursive Descent Parser
- Difference between LL and LR parser
- Parsing ambiguos grammars using LR parser
- Working of Bottom up parser
- Working of top down parser
- Operator grammar and precedence parser in TOC
- Shift Registers in Digital Logic
- Shift Micro-Operations in Computer Architecture
- Compiler Theory | Set 1
- Compiler Theory | Set 2
- Introduction to Syntax Analysis in Compiler Design
- Why FIRST and FOLLOW in Compiler Design?
- Runtime Environments in Compiler Design
- Syntax Directed Translation in Compiler Design
- Intermediate Code Generation in Compiler Design
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.