Presentation on theme: "REMOVING LEFT RECURSION AND INDIRECT LEFT RECURSION"— Presentation transcript:
1 REMOVING LEFT RECURSION AND INDIRECT LEFT RECURSION
2 DEFINITIONSIMMEDIATE 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 → BvtA grammar is called immediately left recursive if it possesses an immediately left recursive production.
3 INDIRECT LEFT RECURSION 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 → BrB → CsC → AtHere, starting with A, we can derive Atsr
4 NOTE.Immediate left recursion is a special case of indirect left recursion (in which the derivation involved is just a single step)“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® Aa1 ………. A® Aarand the remaining productions in which A occurs as lhs be A® b1 …………. A® bs
7 Let KA denote a symbol which does not already occur in the grammar. Replace the above productions by: A ® b1 | | bs | b1KA | | bsKA KA ® a1 | ... | ar | a1KA | ... | arKAClearly the grammar G' produced is equivalentto G
8 EXAMPLE.S ® R a | A a | aR ® a bA ® A R | A T | bT ® T b | aA non-left recursive grammar equiv. to the above is:A ® b | b KAKA ® R | T | R KA | T KAT ® a | a KTKT ® b | b KTClearly 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 beA1,...,Am,we will remove indirect left recursion by constructing an equivalant grammar G’ such thatIf Ai ® Aja is any production of G’,then i < j
10 We can eliminate left recursion in productions (if any) in which A1 occurs as lhs. Assume that this is true for all i £ t where t<m, and let the grammar so formed from G be denoted as Gt.
11 Consider any production of Gt with At+1 as LHS and Aj as the head of its rhs,where j < t+1, e.g. At+1 ® Ajb. By our assumption, all productions of the form Aj ® a have as their headeither a terminal or Ak for some k > j.
12 So if we substitute for Aj in At+1 ® Ajb all the rhs’s with Aj as the LHS, then we will get productions of the form: At+1 ® a rhs with a terminal as head,or At+1 ® a rhs with Ak as its head where k > j
13 By iterating the above process, we will end up with a grammar Gt’ in which all productions with At+1 as lhs either have a terminal as thehead of the rhs or a nonterminal Ak forsome 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 Gt+1. Proceeding in this way for increasing values of t, we will obtain a grammar G’ equivalent to G, in whichif Ai ® Aja 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 1A1 ® A2 A3A2 ® A3 A1 | bA3 ® A1 A1 | aReplace A3 ® A1 A1 by A3 ® A2 A3 A1and then replace this byA3 ® A3 A1 A3 A1 and A3 ® b A3 A1Eliminating direct left recursion in the above,gives: A3 ® a | b A3 A1 | aK | b A3 A1Kk ® A1 A3 A1 | A1 A3 A1K
17 The resulting grammar is then: A1 ® A2 A3A2 ® A3 A1 | bA3 ® a | b A3 A1 | aK | b A3 A1Kk ® A1 A3 A1 | A1 A3 A1K
18 EXAMPLE 2S ® A A | 0A ® S S | 1AnswerConsidering the ordering S, A, we get:A ® AAS | 0S | 1and removing immediate left recursion, we getA ® 0S | 1 | 0SKA | 1KAKA ® AS | ASKA