Can removing left recursion introduce ambiguity?

Suppose we have the following CFG G:

A -> A b A
A -> a

That should produce a line a, aba, ababa, abababa, etc. Now I want to remove left recursion to make it suitable for intelligent parsing. The Dragon Book provides the following rule for removing immediate left recursions. Considering,

A -> Aa | b

rewrite how

A  -> b A'
A' -> a A'
    | Ξ΅

If we just apply the rule to the grammar from above, we get the grammar G ':

A  -> a A'
A' -> b A A'
    | Ξ΅

Which looks good to me, but apparently this grammar is not LL (1) due to some ambiguity. I get the following First / Follow sets:

First(A) = { "a" }
First(A') = { Ξ΅, "b" }
Follow(A) = { $, "b" }
Follow(A') = { $, "b" }

From which I build a parsing table

    |   a          |   b            |  $           |
----------------------------------------------------
A   | A -> a A'    |                |              |
A'  |              | A' -> b A A'   | A' -> Ξ΅      |
    |              | A' -> Ξ΅        |              |

T[A',b], LL (1), , , .

, . , S'. ( ), . , , S'.

, LL (1) G '', , :

A  -> a A'
A' -> b A
    | Ξ΅

- ? - ?

, ? , , ?

G ' LL (k) k > 1?

+4
1

, , .

a b a b a. :

A β‡’ A b A
  β‡’ A b a
  β‡’ A b A b a
  β‡’ A b a b a
  β‡’ a b a b a

A β‡’ A b A
  β‡’ A b A b A
  β‡’ A b A b a
  β‡’ A b a b a
  β‡’ a b a b a

, , . :

A β‡’ a              A β‡’ a
A β‡’ a b A          A β‡’ A b a

( , : - , - ).

+3

All Articles