1

I need to generate unambiguous grammar to access the language L= { a^i b^j c^k | i, j, k ≥ 0 , i = j or i = k }

What I already have is:

S : X | Y
X : TC
T : aTb | ԑ
C : cC | ԑ
Y : aYc | F
F : bF | ԑ

but this grammar is ambiguous, it can recognize the strings with equal number of a,b,c in two different ways. Is there any better suggestion to make it unambiguous?

user247702
  • 23,641
  • 15
  • 110
  • 157
Lucy
  • 471
  • 4
  • 12
  • 28
  • Correct me if I'm wrong but this line `X : aXb | T` will result in strings of type `aaaacccbbbb` rather than `aaaabbbbccc`. Is that intentional? According to your language description it is not allowed. – PiotrWolkowski Jun 02 '14 at 08:19
  • Yes You are right. I fixed the line, but the grammar is still an ambiguous grammar. – Lucy Jun 02 '14 at 08:26

2 Answers2

1
S : X | Y | Z
X : aXb | ԑ
Y : aYc | F
Z : Zc | X | ԑ
F : Fb | ԑ

The above has less rules and produces correct strings but I admit it's still ambiguous.

The grammar of the language L in the question can be divided into two sub-grammars producing following strings: a^i b^i c^j and a^i b^j c^j and intersection of these two will result in this kind of expressions: a^i b^i c^i. It can be proved that if intersection of two grammars produces this kind of expressions then the grammar is inherently ambiguous: http://en.wikipedia.org/wiki/Ambiguous_grammar#Inherently_ambiguous_languages

PiotrWolkowski
  • 8,408
  • 6
  • 48
  • 68
0

I think this topics will help you Converting ambiguous grammar to unambiguous and Removing Ambiguity From Grammars

Community
  • 1
  • 1
Karo
  • 273
  • 4
  • 16