LR Parsing | Compiler Design - Computer Science Engineering (CSE) PDF Download

5. LR PARSING INTRODUCTION:
The "L" is for left-to-right scanning of the input and the "R" is for constructing a
rightmost derivation in reverse.
 

                            LR Parsing | Compiler Design - Computer Science Engineering (CSE)
 

5.2 WHY LR PARSING: 

1. LR parsers can be constructed to recognize virtually all programming-language constructs for which context-free grammars can be written. 

2. The LR parsing method is the most general non-backtracking shift-reduce parsing method known, yet it can be implemented as efficiently  as  other  shift-reduce methods.

3. The class of grammars that can be parsed using LR methods is a proper subset of the class of grammars that can be parsed with predictive parsers.

4. An LR parser can detect a syntactic error as soon as it is possible to do so on a left -toright scan of the input. 

The disadvantage is that it takes too much work to constuct an LR parser by hand for a typical programming-language grammar. But there are lots of LR parser generators available to make this 
task easy.
 

5.3 LR PARSERS:

LR(k) parsers are most general non-backtracking shift-reduce parsers. Two cases of interest are k=0 and k=1. LR(1) is of practical relevance

‘L’ stands for “Left-to-right” scan of input.

‘R’ stands for “Rightmost derivation (in reverse)”.

‘K’ stands for number of input symbols of look-a-head that are used in making parsing decisions.

When (K) is omitted, ‘K’ is assumed to be 1.

LR(1) parsers are table-driven, shift-reduce parsers that use a limited right context (1 token) for handle recognition.

LR(1) parsers recognize languages that have an LR(1) grammar. A grammar is LR(1) if, given a right-most derivation

S⇒r0⇒r1⇒r2- - - rn-1⇒rn⇒sentence.

We can isolate the handle of each right-sentential form ri and determine the production by which to reduce, by scanning ri from left-to-right, going atmost 1 symbol beyond the right end of the handle of 
ri.

Parser accepts input when stack contains only the start symbol and no remaining input symbol are 
left.

LR(0) item:   (no lookahead)

Grammar rule combined with a dot that indicates a position in its RHS.

Ex– 1: SI → .S$ S→.x S→.(L)

Ex-2:  A→XYZ generates 4LR(0) items 

A→.XYZ

A→X.YZ
A→XY.Z
A→XYZ.

The ‘.’ Indicates how much of an item we have seen at a given state in the parse.
A→.XYZ indicates that the parser is looking for a string that can be derived from XYZ.
 

A→XY.Z indicates that the parser has seen a string derived from XY and is looking for one

derivable from Z.

→ LR(0) items play a key role in the SLR(1) table construction algorithm.    

→ LR(1) items play a key role in the LR(1) and LALR(1) table construction algorithms. LR parsers have more information available than LL parsers when choosing a production:  

* LR knows everything derived from RHS plus ‘K’ lookahead symbols. 

* LL just knows ‘K’ lookahead symbols into what’s derived from RHS. 

Deterministic context free languages:

 

LR Parsing | Compiler Design - Computer Science Engineering (CSE)
 

5.4 LR PARSING ALGORITHM:

The schematic form of an LR parser is shown below:


LR Parsing | Compiler Design - Computer Science Engineering (CSE)
 

It consists of an input, an output, a stack, a driver program, and a parsing table that has two parts: action and goto.

The LR parser program determines Sm, the current state on the top of the stack, and ai, the current input symbol. It then consults action [Sm, ai], which can have one of four values:
1. Shift S, where S is a state.
2. reduce by a grammar production A→b
 3. accept and
 4. error 


The function goes to takes a state and grammar symbol as arguments and produces a state. The goto function of a parsing table constructed from a grammar G using the SLR, canonical LR or LALR method is the transition function of DFA that recognizes the viable prefixes of G. (Viable prefixes of G are those prefixes of right-sentential forms that can appear on the stack of a shift-reduce parser, because they do not extend past the right-most handle)

 

 

The document LR 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 LR Parsing - Compiler Design - Computer Science Engineering (CSE)

1. What is LR parsing in computer science engineering (CSE)?
Ans. LR parsing, also known as left-to-right parsing with rightmost derivation, is a bottom-up parsing technique used in computer science engineering (CSE) for analyzing and interpreting the structure of a programming language. It involves traversing the input string from left to right and creating a rightmost derivation of the input string using a given grammar.
2. What is the role of LR parsing in compiler design?
Ans. LR parsing plays a crucial role in compiler design as it is used to construct the syntax analyzer, also known as the parser, of a compiler. The parser uses LR parsing to analyze the input program and check if it conforms to the specified grammar rules. It helps in detecting syntax errors, generating parse trees, and facilitating further stages of the compilation process.
3. What are the differences between LR(0), SLR(1), and LR(1) parsing techniques?
Ans. LR(0), SLR(1), and LR(1) are different variants of LR parsing techniques. LR(0) parsing is the simplest form that does not consider any lookahead symbols. SLR(1) parsing uses a simplified version of the LR(1) items to handle lookahead symbols efficiently. LR(1) parsing, on the other hand, uses a more powerful parsing table that takes into account the lookahead symbols to resolve shift-reduce and reduce-reduce conflicts more accurately.
4. How does LR parsing handle shift-reduce conflicts?
Ans. Shift-reduce conflicts occur in LR parsing when the parser encounters a state where it has both shift and reduce actions possible for the same input symbol. LR parsing resolves shift-reduce conflicts by using a fixed precedence between shift and reduce actions. If a shift-reduce conflict occurs, the parser chooses the shift action over the reduce action based on the fixed precedence, ensuring the correct parsing behavior.
5. Can LR parsing handle all types of grammars?
Ans. LR parsing can handle a wide range of grammars, including those with left recursion and ambiguous productions. LR parsing is more powerful than other bottom-up parsing techniques like LL parsing. However, there are certain types of grammars, such as non-LR(1) grammars, that cannot be parsed using LR techniques. In such cases, specialized parsing algorithms like LALR(1) or GLR parsing may be used to handle the specific grammar complexities.
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

shortcuts and tricks

,

past year papers

,

LR Parsing | Compiler Design - Computer Science Engineering (CSE)

,

MCQs

,

Objective type Questions

,

Viva Questions

,

Important questions

,

LR Parsing | Compiler Design - Computer Science Engineering (CSE)

,

Sample Paper

,

video lectures

,

Exam

,

ppt

,

mock tests for examination

,

study material

,

pdf

,

Free

,

Previous Year Questions with Solutions

,

LR Parsing | Compiler Design - Computer Science Engineering (CSE)

,

Semester Notes

,

Summary

,

Extra Questions

,

practice quizzes

;