You are on page 1of 11

TOPIC TITLE: Left Recursion Removal

NAME – Soumik Maity


ROLL NO. - 13000121013
DEPT - CSE ‘A‘
SUBJECT NAME - COMPILER DESIGN
SUBJECT CODE - PCC-CS501
REG NO:211300100110014 OF 2021-2022
INTRODUCTION

• Left recursion removal is a technique used in formal grammar and parsing to eliminate
left recursion from production rules.
• Left recursion occurs when a non-terminal symbol generates a string that starts with
itself.
• This can lead to infinite loops and parsing ambiguities.
• To remove left recursion, you rearrange the production rules and create new non-
terminals to ensure a more predictable and efficient parsing process.
WHAT IS LEFT RECURSION?

A Grammar G (V, T, P, S) is left recursive if it has a production in the form.


A → A α |β.
The above Grammar is left recursive because the left of production is occurring at a first position on the right
side of production. It can eliminate left recursion by replacing a pair of production with
A → βA′
A → αA′|ϵ
Elimination of Left Recursion Left Recursion
can be
eliminated by introducing new non-terminal A such that. This type of recursion is also called Immediate
Left Recursion.
Problem With Left Recursion

Parser Ambiguity :-
Left-recursive grammars can lead to ambiguous parsing, making it difficult for the parser to know
which production rule to apply.
Performance & Efficiency:-

Parsers with left recursion are typically slower and less efficient, as the parser has to go through
multiple levels of recursion to determine the correct result.
Code Generation :-

Left-recursion can cause problems in code generation, and can be difficult to translate into code
such as compiler compilers or toolkits
EXAMPLE:

Question ;S →ABC, A →Aa/Ad/b, B →Bb/c, C →Cc/g


Ans:-Rue------1 [if A → A α |β then A → βA′ , A → αA′|ϵ]
from question, if we take,A →Aa/b (here as per Rue------1 α=a, β=b)
so, A → bA′ and A′ → aA′|ϵ;
from question, if we take,A →Ad/b (here as per Rue------1 α=d, β=b)
so, A → bA′ and A′ → dA′|ϵ;
from question, if we take,B →Bb/c (here as per Rue------1 α=b, β=c)
so, B → cB′ and B′ → bB′|ϵ;
EXAMPLE(Continued…….)

from question, if we take,C →Cc/g (here as per Rue------1 α=c, β=g)


so, C → gC′ and C′ → cC′|ϵ;
Here S →ABC [put A → bA′ , B → cB′ , C → gC′ ]
So, S → bA′cB′ gC′(here is left recursion)
Answer: , A → bA′, A′ → aA′|ϵ, A′ → dA′|ϵ, B → cB′, B′ → bB′|ϵ, C
→ gC′ , C′ → cC′|ϵ
, S → bA′cB′ gC′[Here, non-terminal symbol generates a string that starts
with itself in every equation] so, left recursion remove.
The Importance Of Removing Left Recursion In Parser
Design
• Efficiency :
Removing left recursion makes parsers more efficient and faster.
• Flexibility Removing:
left recursion can increase the flexibility of the parser and allow it to handle a wider range
of grammars.
• Maintainability:
Non-recursive grammars are easier to understand and maintain than recursive ones.
LIMITATIONS OF LEFT RECURSION REMOVAL

• Restrictions on Grammar Design:


Left recursion removal places limitations on the design of the grammar and can complicate
it.
• Redundancy:
• Left recursion removal can result in the generation of redundant rules, making the
grammar more complex.
CONCLUSION

Left recursion removal is a crucial step in parsing and analyzing context-free grammars. It
involves transforming production rules that lead to left recursive derivations into equivalent
rules that are not left recursive. This is done to avoid infinite loops during parsing.In
conclusion, left recursion removal is a process that helps improve the efficiency and
accuracy of parsing algorithms by restructuring production rules to eliminate left recursion,
ensuring a well-defined and manageable grammar for parsing.
REFERENCES

• https://www.tutorialspoint.com/
• https://www.gatevidyalay.com/
• https://www.geeksforgeeks.org/
• https://www.educative.io/
THANK YOU

You might also like