1 / 11

LEFT RECURSION

LEFT RECURSION. LEFT RECURSION. A grammar is said to be left –recursive if it has a non-terminal A such that there is a derivation A =>Aa, for some string a. Consider the grammar: (i) A -> Aa|b. Corresponding grammar without left recursion: A ->bR R->aR|e.

galena
Télécharger la présentation

LEFT RECURSION

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. LEFT RECURSION

  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 =>Aa, for some string a. Consider the grammar: (i) A -> Aa|b Corresponding grammar without left recursion: A ->bR R->aR|e

  3. We can eliminate immediate left recursion from them by the following technique . First we group the A-productions as : A -> Aa1 | Aa2 | Aa3 |...| Aam | b1 | b2 | b3 |....| bn where no bi begins with an A. Then we replace the A-productions by A -> b1T | b2T | b3T |......| bnT T -> a1T | a2T | a3T |......| amT | e

  4. The above process removes all immediate left recursions but does not remove recursions involving derivations of two or more steps. Consider the grammar: S -> Aa A -> Sb | c.. Here the grammar does not have immediate left recursion .. but has a left recursion because S =>Aa => Sba

  5. In such cases we set a “hierarchy” among non-terminals and implement the following algorithm. • Arrange the non-terminals n some order A1,A2, …An (setting the hierarchy)

  6. 2. for i=1 to n { for j=1 to i-1 { Replace each production of the form Ai -> Ajg by the production Ai->d1g | d2g | ....| dkg where, Aj -> d1 | d2 | d3 |..... | dk are all the current Aj positions } Eliminate the immediate left recursion among the Ai productions }

  7. For the above grammar, S->Aa A->Aab|c After removing immediate left recursion : S->Aa A->cT T->abT|e

  8. LEFT FACTORING Left factoring is a grammar transformation that is useful for producing a grammar suitable for predictive parsing. Basi Idea : 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.

  9. For example, A->ab1 | ab2 are two A-productions If the input begins with a non-empty string derived from ‘a’ we do not know whether to expand ‘A’ to ‘ab1’ or ‘ab2’ . However , we may defer the decision by expanding A to aB .

  10. Now, we may expand B to b1 or b2 The left factored original expression becomes: A->aB B->b1|b2

  11. END

More Related