简体   繁体   中英

Context free grammar for {a*b*c*} - {a^n b^n c^n | n >=0}

I'm having trouble with this problem. I believe its telling me that no string can be generated that has even # of a's b's and c's. This is due to the subtraction of the second set.

A good string from a newly formed CFG should be something like aaabbc or abbbcc and so on.

So I tried breaking the problem into three parts...

  1. Single states

    a.) S(1) -> aS(1) | a | ^ b.) S(2) -> bS(2) | b | ^ c.) S(3) -> cS(2) | b | ^
  2. Two States

    a.) S(4) -> aS(4)b | S(1) | S(2) b.) S(5) -> bS(5)c | S(2) c.) S(6) -> aS(6)c | S(3) | S(1)
  3. States w/AB states

    a.) S(7) -> S(1) | S(4)S(6) b.) S(8) -> S(2) | S(5)S(6) c.) S(9) -> S(3) | S(6)S(3)

    with an orginal start state of ...

     S -> S(7) | S(8) | S(9)

However I'm having problems building strings like aaaabbbcc ...

Am I forming CFGs incorrectly? I felt like I was on the right track but now I'm quite lost.

Context Free Grammer for the language of a^nb^nc^n can be constructed using given production.

S -> aSc | X
X -> BX
X -> null

Using the above production you can generate a^nb^nc^n. Suppose we're generating a^2 b^2 c^2. we will start from S.

= aSc
= aaScc ; aSc
= aaXcc ; S -> X
= aabXcc ; X -> bX
= aabbXcc ; X -> bX
= aabb(null)cc ; X -> null
= aabbcc

A simpler expression for this language is:

{a n b m c * | nm } ∪ {a * b n c m | nm }

(Using the Kleene star above is probably an abuse of notation. Originally, I wrote it with a third integer variable. But I think the star is clearer.)

Now, {a n b m | nm } is simply a n (a + |b + )b n

So the full expression could be written as a n (a + |b + )b n c * | a * b n (b + |c + )c n

Putting all that together into a CFG is a little tedious so I left it for the reader. But it's totally mechanical.

Making a DFA (or more properly Deterministic Pushdown Automaton) out of that might be trickier, since the CFG is ambiguous. In fact, every CFG for that language is ambiguous. But there's no problem making an NDPA.

So I split the problem into 2 sub problems

The number of a's is different from the number of b's. Therefore the a's and c's or b's and c's can be the same.

The number of b's is different from the number of c's. Therefore the a's and b's or a's and c's can be the same.

Edit : More formally as stated by Rici {a n b m c * |nm } ∪ {a * b n c m | nm }

//a and b is different
S-->XR
//b and c is different
S-->DY

//Genarates a string where a is more than b
X--> aXb | A
A --> aA  | a

//Genarates a string where b is more than a
X--> bXa | B
B --> bB  | b

//Genarates a string where b is more than c
Y--> bYc | B
B --> bB  | b

//Genarates a string where c is more than b
Y--> cYb | C
C --> cC  | c

R-->Rc|c|^
D-->Da|a|^

The answer above by Murdock is very close but fails some cases. For example it can generate a, b and aabcc as it should, but cannot generate c, abbcc or bc even though it should be able to. (JFlap is a great software I use for quickly testing different strings on grammers I am constructing, it is also helpful for a lot of other concepts in formal languages and computational theory.) Using Murdock's CFG it is also possible for the letters to appear in different order, but the question statement forces all a's to come first, followed by all b's and finally all c's.

Danish Ahmed seems to have misunderstood the question and tried to construct a CFG for the language where a's, b's and c's are all in equal amounts. Off the top of their head, my lecturer has said it is impossible to construct a CFG for this. Danish's CFG is incorrect for a = b = c because it can generate a 'b' by itself through S -> X; X -> BX; X -> null (also assuming the capital B was meant to be a terminal b.)

Here is my answer, which adds to and adjusts some components of Murdock's CFG so that it is valid for the question.

S=XR|DY|WR|DZ
X=aXb|A
A=aA|a
W=aWb|B
B=bB|b
Y=bYc|B
Z=bZc|C
C=cC|c
R=Rc|c|epsilon
D=Da|a|epsilon

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM