Presentation is loading. Please wait.

Presentation is loading. Please wait.

Left Recursion Lecture 7 Fri, Feb 4, 2005.

Similar presentations


Presentation on theme: "Left Recursion Lecture 7 Fri, Feb 4, 2005."— Presentation transcript:

1 Left Recursion Lecture 7 Fri, Feb 4, 2005

2 A Problem with Recursive Descent Parsers
Suppose the grammar were S  A B | C D A  B A | C A B  C A | A D C  B A | A D | a D  A C | B D | b How could a top-down parser decide which production for S to use?

3 Another Problem with Recursive Descent Parsers
Suppose the grammar were S  S S | a How could the parser decide how many times to use the production S  S S before using the production S  a?

4 Futile Attempt #1 void S() { if (token == a) match(a); else S(); }

5 Futile Attempt #2 void S() { if (token != EOF) S(); }

6 Left Recursion The method of recursive descent does not work if the grammar is left recursive. A grammar is left recursive if there is a derivation A + A for some nonterminal A and string . In particular, a production is left recursive if it is of the form A  A.

7 Left Recursion Applying the method of recursive descent would lead to the function void A() { A(); // Process  } which leads to infinite recursion.

8 Left Recursion Recall that in the earlier example, we added the production S'  S S' | , not the production S'  S' S |  Why? Are they equivalent as far as the language of the grammar is concerned?

9 Eliminating Left Recursion
Left recursion in a production may be removed by transforming the grammar in the following way. Replace A  A |  with A  A' A'  A' | .

10 Eliminating Left Recursion
Under the original productions, a derivation of  is A  A  A  A  . Under the new productions, a derivation of  is A  A'  A'  A'  A'  .

11 Example: Eliminating Left Recursion
Consider the left recursive grammar E  E + T | T T  T * F | F F  (E) | id Apply the transformation to E: E  T E' E'  + T E' | .

12 Example: Eliminating Left Recursion
Then apply the transformation to T: T  F T' T'  * F T' | .

13 Example: Eliminating Left Recursion
Now the grammar is E  T E' E'  + T E' | . T  F T' T'  * F T' | . F  (E) | id

14 Example: Eliminating Left Recursion
The function for E' would be void Eprime() { if (token == PLUS) match(PLUS); T(); Eprime(); } return;

15 Advantages of Left Recursion
A left recursive grammar is often more intuitive than the transformed grammar. A left recursive grammar will match expressions earlier, leading to shallow recursion. Consider parsing a + b + c + d + e. Bottom-up parsing takes advantage of the benefits of left recursion.

16 Example Consider the simple grammar E  E + E | num Convert it to
E  num E' E'  + E E' |  Example: SimpleParser

17 Exercise The grammar R  R  R | RR | R* | (R) | a | b
generates all regular expressions on the alphabet {a, b}. Rewrite the grammar to reflect the precedence rules. Eliminate left recursion.


Download ppt "Left Recursion Lecture 7 Fri, Feb 4, 2005."

Similar presentations


Ads by Google