0% found this document useful (0 votes)
9 views18 pages

Left Recursion and Left Factoring - 2024

The document discusses techniques for writing grammars, specifically focusing on eliminating ambiguity, left recursion, and left factoring. It explains the definitions and transformations necessary to convert ambiguous and left-recursive grammars into equivalent forms suitable for parsing. The document also provides algorithms and examples for both left recursion elimination and left factoring.

Uploaded by

gnananya2
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)
9 views18 pages

Left Recursion and Left Factoring - 2024

The document discusses techniques for writing grammars, specifically focusing on eliminating ambiguity, left recursion, and left factoring. It explains the definitions and transformations necessary to convert ambiguous and left-recursive grammars into equivalent forms suitable for parsing. The document also provides algorithms and examples for both left recursion elimination and left factoring.

Uploaded by

gnananya2
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/ 18

Left recursion and left factoring

Dr. R Guru
Associate Professor
Dept. of Computer Science and Engineering,
SJCE, JSSSTU, Mysuru
Writing Grammar
• Eliminating Ambiguity
• Eliminating of Left recursion
• Eliminating Left Factoring
Eliminating Ambiguity
• Ambiguous Grammar : A Grammar that produces more than
two parse tree for some sentence is said to be ambiguous.
• Ex:1 Consider the grammar EE + E│E * E│id

we can have two parse tree for the input id + id * id


• After eliminating
E E + T│T
T T * F│F
F  id│(E)

Ex: 2 Consider stmt  if expr then expr stmt


│if expr then expr stmt else stmt
│other

we can have two parse tree for the input


if E1 then if E2 then S1 else S2
• After eliminating
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