Left Factoring & Examples | Compiler Design - Computer Science Engineering (CSE) PDF Download


  • Example 1: Remove the left recursion from the production: A ⇒ A α|β
                                                                                             Left Factoring & Examples | Compiler Design - Computer Science Engineering (CSE)
     
    Applying the transformation yields:

A→ β A I
A I→ α A I | ∈ 
          ­­­
RemAIning part after A
 

Example 2: 
Remove the left recursion from the productions:
E → E + T | T
T→ T * F | F
Applying the transformation yields:
E→ T EI                                                  T → F TI
E I→ T EI | ∈                                        T I→ * F TI

Example 3:
Remove the left recursion from the productions:
E→ E + T | E – T|T
T→ T * F| T/F | F
Applying the transformation yields:
E→ T E                          T→ F TI
E→ + T EI | - T EI | ∈       T I→ * F T| /F TI | ∈

Example 4: 
Remove the left recursion from the productions:
S→ A a | b
A→ A c | S d | ∈
1. The non terminal S is left recursive because S→ A a→ S d a But it is not immediate left recursive.
2. Substitute S-productions in A→ S d to obtAIn:
A→ A c | A a d | b d | ∈
3. Eliminating the immediate left recursion:
S→ A a | b
A→ b d AI | A 
AI→ c AI | a d A|∈

 

Example 5: 
Consider the following grammar and eliminate left recursion. S → A a | b
                   A → S c | d

The nonterminal S is left recursive in two steps: S → A a → S c a → A a c a → S c a c a - - -

Left recursion causes the parser to loop like this, so remove: Replace A → S c | d by A → A a c | b c | d

and then by using Transformation rules: A → b c AI | d A
AI → a c AI
 

2.5 Left Factoring: Left factoring is a grammar transformation that is useful for producing a grammar suitable for predictive parsing.
When it is not clear which of two alternative productions to use to expand a non-terminal A, we may be able to rewrite the productions to defer the decision until we have some enough of the input to make the right choice.

Algorithm: For all A ∈ non-terminal, find the longest prefix a that occurs in two or more right-hand sides of A.

If a ¹ ∈ then replace all of the A productions, A → a bI | a b2 | - - - | a bn | r
With
A → a AI | r AI → bI | b2| - - - | bn | ∈ Where, AI is a new element of non-terminal. Repeat until no common prefixes remAIn.
It is easy to remove common prefixes by left factoring, creating new non-terminal.

For example consider:
V → a b | a r Change to:
V → a VI V→ b | r

Example 1:
Eliminate Left factoring in the grammar: S → V := int
V → alpha ‘[‘ int ’]’ | alpha
Becomes:
S → V := int
V → alpha VI
VI → ’[‘ int ’] | Î

 

The document Left Factoring & Examples | 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 Left Factoring & Examples - Compiler Design - Computer Science Engineering (CSE)

1. What is left factoring in computer science engineering?
Ans. Left factoring is a technique used in computer science engineering to simplify a grammar by factoring out the common prefixes of two or more productions. This technique is used to remove ambiguity and redundancy in the grammar and make it easier to parse.
2. What are the benefits of left factoring in computer science engineering?
Ans. The benefits of left factoring in computer science engineering include reducing the size of the grammar, making it more efficient to parse, and reducing the number of conflicts that arise during parsing. It also helps in making the grammar more readable and easier to understand.
3. What are some examples of left factoring in computer science engineering?
Ans. An example of left factoring in computer science engineering is converting the following grammar: A -> abcd | abef | abgh To: A -> abX X -> cd | ef | gh Another example is converting the following grammar: A -> abcde | abcef | abcfg To: A -> abcX X -> de | ef | fg
4. How does left factoring help in reducing ambiguity in computer science engineering?
Ans. Left factoring helps in reducing ambiguity in computer science engineering by removing common prefixes from multiple productions. This makes it easier for the parser to determine which production to choose, reducing the number of conflicts that arise during parsing. It also helps in making the grammar more readable and easier to understand.
5. What are some common tools used for left factoring in computer science engineering?
Ans. Some common tools used for left factoring in computer science engineering include parser generators like Yacc and Bison, which have built-in support for left factoring. Other tools include automated grammar transformation tools like ANTLR, which can automatically transform grammars to remove left recursion and left factoring.
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

Objective type Questions

,

video lectures

,

mock tests for examination

,

shortcuts and tricks

,

Left Factoring & Examples | Compiler Design - Computer Science Engineering (CSE)

,

Left Factoring & Examples | Compiler Design - Computer Science Engineering (CSE)

,

Extra Questions

,

Left Factoring & Examples | Compiler Design - Computer Science Engineering (CSE)

,

ppt

,

Exam

,

Sample Paper

,

Important questions

,

Viva Questions

,

past year papers

,

Semester Notes

,

Free

,

practice quizzes

,

MCQs

,

pdf

,

Previous Year Questions with Solutions

,

study material

,

Summary

;