Successfully reported this slideshow.
Upcoming SlideShare
×

# Cs419 lec10 left recursion and left factoring

10,841 views

Published on

Published in: Education
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

### Cs419 lec10 left recursion and left factoring

1. 1. WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Computer Science Department dr.sharaf@from-masr.com
2. 2. Dr. Hussien M. Sharaf TOP-DOWN PARSING A parser is a top-down if it discovers a parse tree top to bottom.  A top-down parse corresponds to a preorder traversal of the parse tree.  A left most derivation is applied to each step. 
3. 3. Dr. Hussien M. Sharaf LL PARSING LL parsing is a technique of top-down parsing.  Consists of:     Parser stack: that holds grammar symbols: non-terminals and tokens. Parsing table: that specifies the parser actions (Match, Predict, Accept, Error). Driver function: that interacts with parser stack, parsing table, and scanner. Scanner Next token Parser driver Parsing table Output Parsing stack
4. 4. Dr. Hussien M. Sharaf PROBLEMS FACING LL(1) PARSERS 1. 2. Left recursion. Left factoring. Both problems prevents any LL parser from deciding deterministically which rule should be fired.
5. 5. Dr. Hussien M. Sharaf LEFT RECURSION We have to eliminate left recursion because top down parsing methods can not handle left recursive grammars.  A grammar is left recursive if it has a nonterminal A such that there is a derivation A  Aα for some string α  Consider the left recursive grammar A  A α1 | A α2 | β1 | β2  +
6. 6. Dr. Hussien M. Sharaf REMOVE LEFT RECURSION 1. 2. 3. Let A  other productions of A not starting with A (i.e. β1, β2), followed by A′ (A  β1A′| β2A′) Then let A′  postfix of the productions starting with A (α1, α2) followed by A′ (A′  α1A′|α2A′) Then add |ε to A′ (A′  α1A′|α2A′| ε) +
7. 7. Dr. Hussien M. Sharaf ELIMINATION OF LEFT RECURSION EXAMPLE E  E+T|T T  T*F|F F  (E)|id  The equivalent non-left recursive grammar is: E  T E′ E′  +TE′|ε T  FT′ T′  *FT′|ε F  (E) | id
8. 8. Dr. Hussien M. Sharaf LEFT FACTORING  In left factoring it is not clear which of two alternative productions to use to expand a nonterminal A. i.e. if A  αβ1 | αβ2 W e don’t know whether to expand A to αβ1 or to αβ2  To remove left factoring for this grammar replace all A productions containing α as prefix by A  αA′ then A′  β1 | β2
9. 9. Dr. Hussien M. Sharaf ELIMINATION OF LEFT FACTORING EXAMPLE S  iEtS | iEtSeS | a Eb  The equivalent non-left factored grammar is: S  iEtSS′ | a S′ eS | ε Eb
10. 10. Dr. Hussien M. Sharaf THANK YOU