Presentation is loading. Please wait.

Presentation is loading. Please wait.

REMOVING LEFT RECURSION AND INDIRECT LEFT RECURSION.

Similar presentations


Presentation on theme: "REMOVING LEFT RECURSION AND INDIRECT LEFT RECURSION."— Presentation transcript:

1 REMOVING LEFT RECURSION AND INDIRECT LEFT RECURSION

2 DEFINITIONS IMMEDIATE LEFT RECURSION. A production is immediately left recursive if its left hand side and the head of its right hand side are the same symbol, e.g. B → Bvt A grammar is called immediately left recursive if it possesses an immediately left recursive production.

3 INDIRECT LEFT RECURSION. A grammar is said to posess indirect left recursion if it is possible, starting from any symbol of the grammar, to derive a string whose head is that symbol. Example. A → Br B → Cs C → At Here, starting with A, we can derive Atsr

4 NOTE. 1.Immediate left recursion is a special case of indirect left recursion (in which the derivation involved is just a single step) 2.“Immediate left recursion” is conventionally referred to simply as “left recursion” (leaving out the word “immediate”). But, for our purposes, this is confusing, so we will not follow that practice.

5 Let G be any grammar in which the only production, if any, which contains a symbol that vanishes is S →ε, where S is the goal symbol, and which contains no useless or unit productions. To remove left recursion from G (i.e. produce an equivalant grammar with the same language as G, but which is not left recursive), do the following: For each nonterminal A that occurs as the lhs of a left- recursive production of G, do the following:

6 Let the left-recursive productions in which A occurs as lhs be A  A  1 ………. A  A  r and the remaining productions in which A occurs as lhs be A   1 …………. A   s

7 Let K A denote a symbol which does not already occur in the grammar. Replace the above productions by: A   1 |... |  s |  1 K A |... |  s K A K A   1 |... |  r |  1 K A |... |  r K A Clearly the grammar G' produced is equivalent to G

8 EXAMPLE. S  R a | A a | a R  a b A  A  R | A T | b T  T b | a A non-left recursive grammar equiv. to the above is: S  R a | A a | a R  a b A  b | b K A K A  R | T | R K A | T K A T  a | a K T K T  b | b K T Clearly the grammar G’ obtained is equivalent to G, and has the required two properties

9 REMOVING INDIRECT LEFT RECURSION Let any ordering of the nonterminals of G be A 1,...,A m, we will remove indirect left recursion by constructing an equivalant grammar G’ such that If A i  A j  is any production of G’, then i < j

10 We can eliminate left recursion in productions (if any) in which A 1 occurs as lhs. Assume that this is true for all i  t where t

11 Consider any production of G t with A t+1 as LHS and A j as the head of its rhs, where j < t+1, e.g. A t+1  A j . By our assumption, all productions of the form A j   have as their head either a terminal or A k for some k > j.

12 So if we substitute for A j in A t+1  A j  all the rhs’s with A j as the LHS, then we will get productions of the form: A t+1  a rhs with a terminal as head, or A t+1  a rhs with A k as its head where k > j

13 By iterating the above process, we will end up with a grammar G t ’ in which all productions with A t+1 as lhs either have a terminal as the head of the rhs or a nonterminal A k for some k  t+1.

14 Productions of this kind in which k=t+1 are left recursive, and can be eliminated to produce a grammar which we will call G t+1. Proceeding in this way for increasing values of t, we will obtain a grammar G’ equivalent to G, in which if A i  A j  is any production of G’, then i < j

15 G’ will in addition contain some productions with subscripted K’s as left hand side. These do not introduce any direct or indirect left recursion, since the corresponding right hand sides (such as e.g. b KA ) start with symbols that do not vanish, and no symbol derives a string starting with a subsripted K

16 EXAMPLE 1 A 1  A 2 A 3 A 2  A 3 A 1 | b A 3  A 1 A 1 | a Replace A 3  A 1 A 1 by A 3  A 2 A 3 A 1 and then replace this by A 3  A 3 A 1 A 3 A 1 and A 3  b A 3 A 1 Eliminating direct left recursion in the above, gives: A 3  a | b A 3 A 1 | aK | b A 3 A 1 K k  A 1 A 3 A 1 | A 1 A 3 A 1 K

17 The resulting grammar is then: A 1  A 2 A 3 A 2  A 3 A 1 | b A 3  a | b A 3 A 1 | aK | b A 3 A 1 K k  A 1 A 3 A 1 | A 1 A 3 A 1 K

18 EXAMPLE 2 S  A A | 0 A  S S | 1 Answer Considering the ordering S, A, we get: S  A A | 0 A  S | 0S | 1 and removing immediate left recursion, we get S  A A | 0 A  S | 1 | 0SK A | 1K A K A  AS | ASK A


Download ppt "REMOVING LEFT RECURSION AND INDIRECT LEFT RECURSION."

Similar presentations


Ads by Google