开发者

Designing Context Free Grammar's [HW]

I've been working on this for about 5 hours for homework and I was hoping some of you guys might be able to help since CFG's are a huge part of CS.

My real trouble is with part C.

Design a CFG for each of the following languages:

A. {(a^i)(b^j)(c^k)} WHERE (i != j) AND i,j,k >= 0)

I came up with:

Start-> aAB | AbB
A-> epsilon | aA
B-> epsilon | C | bB
C-> epsilon | cC

This seems to work for bccc, abbcc, aabbb,cc so I think I am good here.

B. {(a^i)(b^j)(c^k)} WHERE (i != k) AND i,j,k >= 0)

Start-> aABC | ABcC
A-> epsilon | aA
B-> epsilon | bB
C-> epsilon | cC

This works for bc, bbcc, ab, abb, aac so all looks well for when i!=k

C. {(a^i)(b^j)(c^k)} WHERE (i != j OR i != k) AND i,j,k >= 0)

Start-> aABC | AbB | ABcC
A-> epsilon | aA
B-> epsilon | bB | C
C-> epsilon | cC

I do not think part C is correct but I believe开发者_C百科 A and B are correct. Can anyone tell me if I am right/wrong or help in anyway? I believe since in the last case I am just doing a OR, and because my A,B,C variables are pretty much the same I can get away with combining. It seems to work as bc works, acc works and many others but I get the feeling I should not simply be combining start states.

Anyone know if I am right or close or have any tips?


Remember that when you are testing grammars, be sure to also test with strings that should be rejected (see @Paulo's answer for some that fail for you currently). To solve the first two, write a grammar that represents A^i B^j where i != j; add a grammar for an arbitrary number of c's onto the end of that for part 1, and add that grammar into the middle for part 2. For part 3, remember that the union of two grammars can be easily written as a grammar.


Your first grammar matches abc (by Start -> AbB -> abB -> abC -> abc), so it is not a right grammar.

So, you need to make sure the as and bs are created in the same quantity (and then some more for one of them).

The same abc is also matched by your second rule, where it shouldn't be permitted, too.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜