0% found this document useful (0 votes)
56 views2 pages

RECUSRIVITE2

The document describes an algorithm for eliminating left-recursion from context-free grammars (CFGs) without epsilon productions or cycles. The algorithm involves ordering the variables and then replacing productions of the form A → Bγ with equivalent productions derived from B's productions to eliminate immediate left-recursion, repeating for each variable. It provides an example applying the algorithm. However, the algorithm can fail for grammars containing epsilon productions or cycles, as left-recursion elimination reduces to eliminating immediate left-recursion among variables in cyclic productions.

Uploaded by

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

RECUSRIVITE2

The document describes an algorithm for eliminating left-recursion from context-free grammars (CFGs) without epsilon productions or cycles. The algorithm involves ordering the variables and then replacing productions of the form A → Bγ with equivalent productions derived from B's productions to eliminate immediate left-recursion, repeating for each variable. It provides an example applying the algorithm. However, the algorithm can fail for grammars containing epsilon productions or cycles, as left-recursion elimination reduces to eliminating immediate left-recursion among variables in cyclic productions.

Uploaded by

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

Here’s an algorithm that eliminates all left-recursion for any Arrange the variables in some order A1 , A2, . . . , An .

CFG without ǫ-productions and without cycles. for i := 1 to n do begin


for j := 1 to i − 1 do begin
Arrange the variables in some order A1 , A2, . . . , An . replace each production of the form Ai → Aj γ
by the productions Ai → δ1 γ | δ2 γ | · · · | δk γ
for i := 1 to n do begin where Aj → δ1 | δ2 | · · · | δk are all the current Aj -productions;
for j := 1 to i − 1 do begin end
replace each production of the form Ai → Aj γ eliminate the immediate left recursion among the Ai -productions;
by the productions Ai → δ1 γ | δ2 γ | · · · | δk γ
end
where Aj → δ1 | δ2 | · · · | δk are all the current Aj -productions;
end So at this point we have grammar
eliminate the immediate left recursion among the Ai -productions;
S → XSS ′ | aS ′
end
S ′ → XS ′ | SbS ′ | ǫ
Consider the grammar X → Xb | Sa | b
S → SX | SSb | XS | a and the next obligation is to replace the production
X → Xb | Sa | b
X → Sa
Let’s order the variables S, X:
with the productions
The first time through we simply eliminate immediate left X → XSS ′ a | aS ′ a .
recursion in S-productions, yielding
We then eliminate immediate left recursion among
S → XSS ′ | aS ′
S ′ → XS ′ | SbS ′ | ǫ X → XSS ′ a | aS ′ a | Xb | b .
X → Xb | Sa | b

13 14
Eliminating immediate left recursion among Let’s look at examples showing that this algorithm can fail if
the grammar has ǫ-productions or cycles.
X → XSS ′ a | Xb | b | aS ′ a
In the simplest case, when there is only one variable, call it X,
yields
the presence of a cycle implies that the grammar includes the
X → bX ′ | aS ′ aX ′
production
X ′ → SS ′ aX ′ | bX ′ | ǫ
X →X.
So the final result is Moreover, the whole left recursion elimination algorithm
S → XSS ′ | aS ′ reduces to elimination of immediate left recursion among
′ ′ ′
S → XS | SbS | ǫ X-productions.
′ ′ ′
X → bX | aS aX
And we have previously observed that our construction for
X ′ → SS ′ aX ′ | bX ′ | ǫ
immediate left recursion elimination is no good in the presence
of X → X.

For example, if the grammar is

X→X |a

the construction for eliminating immediate left recursion yields


X → aX ′
X′ → X′

What about more complex cycles?

15 16

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy