LRec 1011
LRec 1011
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 :
The above process removes all immediate left recursions but does not remove recursions
involving derivations of two or more steps.
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
}
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