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

LRec 1011

Left recursion occurs when a non-terminal symbol can derive itself, either directly or indirectly through other symbols. This causes issues for top-down parsing. Grammars can be transformed to eliminate left recursion by introducing new non-terminals. Immediate left recursion is removed first by grouping productions and replacing the symbol. Remaining indirect left recursion is removed by setting a hierarchy and replacing references to higher symbols. Left factoring rewrites productions that share common prefixes to defer parsing decisions until more input is examined.

Uploaded by

api-3696125
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views2 pages

LRec 1011

Left recursion occurs when a non-terminal symbol can derive itself, either directly or indirectly through other symbols. This causes issues for top-down parsing. Grammars can be transformed to eliminate left recursion by introducing new non-terminals. Immediate left recursion is removed first by grouping productions and replacing the symbol. Remaining indirect left recursion is removed by setting a hierarchy and replacing references to higher symbols. Left factoring rewrites productions that share common prefixes to defer parsing decisions until more input is examined.

Uploaded by

api-3696125
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

LEFT RECURSION:

A grammar is said to be left –recursive if it has a non-terminal A such that there is


a derivation A =>Αα, for some string α. Top–down parsing methods cannot handle left
recursive grammars, so a transformation that eliminates left-recursion is needed.

Consider the grammar:

(i) Α −> Αα|β


The parser can go into an infinite loop.
Corresponding grammar without left recursion:
Α −>βΡ
Ρ−>αΡ|ε

No matter how many A-productions there are we can eliminate immediate left recursion
from them by the following technique .First we group the A-productions as :

A -> Αα1 | Αα2 | Αα3 |...| Ααm | β1 | β2 | β3 |....| βn


where no βi begins with an A. Then we replace the A-productions by

A −> β1Τ | β2Τ | β3Τ |......| βnT

T −> α1Τ | α2Τ | α3Τ |......| αmT | ε

The above process removes all immediate left recursions but does not remove recursions
involving derivations of two or more steps.

Consider the grammar:

S −> Αa
A −> Sb | c..

Here the grammar does not have immediate left recursion .. but has a left recursion
because S =>Aa => Sba

In such cases we set a “hierarchy” among non-terminals and implement the following
algorithm.

1. Arrange the non-terminals n some order A1,A2, …An (setting the hierarchy)
2. for i=1 to n
{
for j=1 to i-1
{
Replace each production of the form Ai -> Ajγ
by the production Αi−>δ1γ | δ2γ | ....| δκγ
where Aj −> δ1 | δ2 | δ3 |..... | δk are all the current Aj positions
}
Eliminate the immediate left recursion among the Ai productions
}

For the above grammar,


S->Aa
A->Aab|c
After removing immediate left recursion :

S->Aa
A->cT
T->abT|ε

LEFT FACTORING:
Left factoring is a grammar transformation that is useful for producing a grammar
suitable for predictive parsing. The basic idea is that 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
A-productions to defer the decision until we have seen enough of the input to make the
right choice.

For example,
A->αβ1 | αβ2 are two A-productions , and the input begins with a non-empty string
derived from α we do not know whether to expand A to αβ1 or αβ2 . However , we may
defer the decision by expanding A to αB . Then , after eeing the input derived from α,
we may expand B to β1 or β2 .The left factored original expression becomes:
Α−>αΒ
Β−>β1|β2

You might also like