0% found this document useful (0 votes)
184 views14 pages

Left Recursion and Left Factoring - 2023

The document discusses left recursion and left factoring in grammars. Left recursion occurs when a nonterminal can derive itself, either directly or indirectly through other symbols. This causes issues for top-down parsers. Left recursion can be eliminated by replacing productions of the form A → Aα with A → βA' and A' → αA' | ε. Left factoring involves identifying the longest common prefix between alternatives for a nonterminal and pulling it out to improve predictability for parsers.

Uploaded by

tressapeel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
184 views14 pages

Left Recursion and Left Factoring - 2023

The document discusses left recursion and left factoring in grammars. Left recursion occurs when a nonterminal can derive itself, either directly or indirectly through other symbols. This causes issues for top-down parsers. Left recursion can be eliminated by replacing productions of the form A → Aα with A → βA' and A' → αA' | ε. Left factoring involves identifying the longest common prefix between alternatives for a nonterminal and pulling it out to improve predictability for parsers.

Uploaded by

tressapeel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Left recursion and left factoring

Dr. R Guru
Associate Professor
Dept. of Computer Science and Engineering,
SJCE, JSSSTU, Mysuru
Left recursion
• A grammar is left recursive if it has a non terminal A such that
there is derivation
A + A for some string 
• Top down parsing methods cannot handle left-recursive grammars
so a transformation is needed to eliminate left recursion.
• Left recursion in a production may be removed by transforming
the grammar in the following way.
• Replace A  A │ 

with A  A'
A'  A' │ .
• Suppose there are many number of production
A  A1│A2│….. │Am│1│2│….. │ n

Then replacement of A- Productions by


A  1 A’│2 A’ │….. …│ n A’
A’  1 A’ │2 A’ │……... │m A’ │ 
• Under the original productions, a derivation of 
is
A  A  A  A  .
• Under the new productions, a derivation of  is
A  A'  A'  A'
 A'  .
Example:1
EE + T│T
T T * F│F
F  id│(E)
Left recursion elimination algorithm :
Input :Grammar G with no cycle or Productions
Output: An equivalent grammar with no left recursion.
Method:
1. Arrange the nonterminals in some order A1,A2,…,An.
2. for (each i from 1 to n) {
3 for (each j from 1 to i-1) {
4 Replace each production of the form Ai  Aj γ by the

production Ai  δ1 γ│δ2 γ … │δk γ


where Aj δ1 │ δ2 │ … │δk are all current Aj
productions
5 }
6 Eliminate left recursion among the Ai-productions
7 }
After elimination left recursion

ETE’
E’ + TE’│Є
T FT’
T’  * FT’│ Є
F  id│(E)
Examples:1
S Aa│b
A Ac│Sd │e
Examples:2
A BC│a
S CA│Ab
A AB│CC│a
Left factoring
• Left factoring is a grammar transformation that is useful for
producing a grammar suitable for predictive or top-down
parsing.
• Consider following grammar:
Stmt  if expr stmt else stmt
| if expr stmt
• On seeing input if it is not clear for the parser which
production to use
• We can easily perform left factoring:
– If we have A  αβ1 │ αβ2 then we replace it with
A  αA’
A’  β1 │ β2
Left factoring elimination algorithm:

Input : Grammar G
Output: An equivalent left factored grammar.
Method
– For each non-terminal A, find the longest prefix α common
to two or more of its alternatives. If α≠ ɛ, then replace all
of A-productions
A  αβ1 │ αβ2 │ … │ αβn │ γ
by
A  αA’ │ γ
A’ β1 │ β2 │ … │ βn
Example:1
S iEtS│iEtSeS│ a
E b
Example:2
• S  bSSaaS │ bSSaSb │ bSb │ a

You might also like