Shift: Reduce Parsing | Compiler Design - Computer Science Engineering (CSE) PDF Download

SHIFT- REDUCE PARSING:
Shift Reduce Parsing uses a stuck to hold grammar symbols and input buffer to hold string to be parsed, because handles always appear at the top of the stack i.e., there’s no need to look deeper into 
the state.

A shift-reduce parser has just four actions: 
1. Shift-next word is shifted onto the stack (input symbols) until a handle is formed.
2. Reduce – right end of handle is at top of stack, locate left end of handle within the stack. Pop handle off stack and push appropriate LHS.
3. Accept – stop parsing on successful completion of parse and report success.
4. Error – call an error reporting/recovery routine.

3.1 Possible Conflicts: Ambiguous grammars lead to parsing conflicts.
1. Shift-reduce: Both a shift action and a reduce action are possible in the same state (should we shift or reduce)

Example: dangling-else problem

2. Reduce-reduce: Two or more distinct reduce actions are possible in the same state. (Which production should we reduce with 2). 
 

Example:

Stmt →id (param) (a(i) is procedure call)

Param→ id Expr → id

(expr) /id (a(i) is array subscript)

Stack input buffer action

$&aa (i ) &.$ Reduce by ?

Should we reduce to param or to expr? Need to know the type of a: is it an array or a function. This information must flow from declaration of a to this use, typically via a symbol table.
 

Shift – reduce parsing example: (Stack implementation)

Grammar: E→E+E/E*E/(E)/id Input: id1+id2+id3
 

One Scheme to implement a handle-pruning, bottom-up parser is called a shift-reduce parser. Shift

reduce parsers use stack and an input buffer.

The sequence of steps is as follows:
1. initialize stack with $.
2. Repeat until the top of the stack is the goal symbol and the input token is “end of life”. a. Find the handle 
If we don’t have a handle on top of stack, shift an input symbol onto the stack.

b.  Prune the handle

if we have a handle (A→b) on the stack, reduce (i) pop /b/ symbols off the stack (ii)push A onto the stack.
 

Shift: Reduce Parsing | Compiler Design - Computer Science Engineering (CSE)
Shift: Reduce Parsing | Compiler Design - Computer Science Engineering (CSE)
 

Example 2:
Goal à Expr
Expr à Expr + term | Expr – Term | Term
Term à Tem & Factor | Term | factor | Factor
Factor à number | id | (Expr) The expression grammar : x – z * y
 

Shift: Reduce Parsing | Compiler Design - Computer Science Engineering (CSE)
 

Shift: Reduce Parsing | Compiler Design - Computer Science Engineering (CSE)
1. shift until the top of the stack is the right end of a handle
2. Find the left end of the handle & reduce.
 

Procedure:
1. Shift until top of stack is the right end of a handle.
2. Find the left end of the handle and reduce.
* Dangling-else problem: 

stmt→if expr then stmt/if expr then stmt/other then example string is: if E1 then if E2 then S1 else S2 has two parse trees (ambiguity) and so this grammar is not of LR(k) type.
 

        Shift: Reduce Parsing | Compiler Design - Computer Science Engineering (CSE)
 

       Shift: Reduce Parsing | Compiler Design - Computer Science Engineering (CSE)

 

The document Shift: Reduce Parsing | Compiler Design - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Compiler Design.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
26 videos|66 docs|30 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Shift: Reduce Parsing - Compiler Design - Computer Science Engineering (CSE)

1. What is parsing in computer science engineering?
Ans. Parsing in computer science engineering refers to the process of analyzing a string of symbols according to the rules of a formal grammar. It involves breaking down the string into its constituent parts and determining their syntactic structure.
2. Why is reducing parsing important in computer science engineering?
Ans. Reducing parsing is important in computer science engineering because it helps optimize the parsing process by minimizing the number of steps required to analyze a string. This can improve the efficiency and speed of parsing algorithms, making them more suitable for real-time or resource-constrained applications.
3. What are the benefits of reducing parsing in computer science engineering?
Ans. Reducing parsing in computer science engineering offers several benefits, including faster parsing times, reduced memory usage, improved error detection and recovery, and increased overall efficiency of parsing algorithms. It can also simplify the development and maintenance of parsing systems.
4. What are some common techniques used for reducing parsing in computer science engineering?
Ans. Some common techniques used for reducing parsing in computer science engineering include bottom-up parsing methods like LR parsing, which reduce the number of parsing steps by constructing a parse tree from the bottom up, and operator precedence parsing, which reduces the number of parsing steps by associating operators with precedence levels.
5. Are there any challenges associated with reducing parsing in computer science engineering?
Ans. Yes, there are challenges associated with reducing parsing in computer science engineering. Some challenges include dealing with ambiguous grammars, handling complex language features or constructs, and ensuring that the reduced parsing technique is compatible with the requirements of the specific language or application being parsed.
26 videos|66 docs|30 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

practice quizzes

,

Free

,

Shift: Reduce Parsing | Compiler Design - Computer Science Engineering (CSE)

,

Exam

,

video lectures

,

Viva Questions

,

Summary

,

Important questions

,

Extra Questions

,

pdf

,

Previous Year Questions with Solutions

,

past year papers

,

ppt

,

Sample Paper

,

Shift: Reduce Parsing | Compiler Design - Computer Science Engineering (CSE)

,

study material

,

Objective type Questions

,

shortcuts and tricks

,

Semester Notes

,

mock tests for examination

,

MCQs

,

Shift: Reduce Parsing | Compiler Design - Computer Science Engineering (CSE)

;