6

I'm studying for a finite automata & grammars test and I'm stuck with this question:

Construct a grammar that generates L:
L = {a^n b^m c^m+n|n>=0, m>=0}

I believe my productions should go along this lines:

    S->aA | aB
    B->bB | bC
    C->cC | c Here's where I have doubts

How can my production for C remember the numbers of m and n? I'm guessing this must rather be a context-free grammar, if so, how should it be?

Coral Doe
  • 1,925
  • 3
  • 19
  • 36
andandandand
  • 21,946
  • 60
  • 170
  • 271
  • If it had been homework I would have marked it, like I said, I'm studying for a test. I'm taking away the homework tag. Man, Homework != Test – andandandand Jun 20 '09 at 16:05
  • 5
    Why so defensive on the homework tag? Studying for a test sounds like homework or at least "schoolwork" & the tag helps people looking for such questions find this one. – CoderDennis Jun 20 '09 at 16:27
  • Actually it's the "finite automata & grammars" part that sounds like homework. Doesn't matter if it's for a test or not. – CoderDennis Jun 20 '09 at 16:39
  • people looking for this question would look for "automata", "language" or "grammar" not "homework". Since I'm not asking you to do my homework it would be both a misplaced and meaningless tag. – andandandand Jun 20 '09 at 16:45
  • 2
    Shouldn't such questions be migrated to Theoretical Computer Science? – Ravindra S Jul 16 '12 at 05:47

8 Answers8

8

Seems like it should be like:

A->aAc | aBc | ac | epsilon
B->bBc | bc | epsilon

You need to force C'c to be counted during construction process. In order to show it's context-free, I would consider to use Pump Lemma.

Artem Barger
  • 40,769
  • 9
  • 59
  • 81
  • I may be confusing some definition, but since the production rules all have only a single nonterminal on the left side, isn't this trivially a context-free grammar? – Svante Jun 20 '09 at 19:02
  • Actually, since m and n just need to be >= 0, your grammar is slightly incorrect. Here's one that works: A->aAc | B B->bBc | (epsilon) – rofrankel Aug 24 '09 at 08:17
  • I guess the `ac` and `bc` parts are redundant. `ac` can be constructed by `aAc -> a epsilon c -> ac` and the same goes for `bc`. – Lasse Espeholt Apr 25 '11 at 08:16
4
S -> X
X -> aXc | Y
Y -> bYc | e

where e == epsilon and X is unnecessary but added for clarity

Lasse Espeholt
  • 17,622
  • 5
  • 63
  • 99
2

Yes, this does sound like homework, but a hint:

Every time you match an 'a', you must match a 'c'. Same for matching a 'b'.

0

S->aSc|A A->bAc|λ

This means when ever you get a at least you have 1 c or if you get a and b you must have 2 c. i hope it has been helpful

0

Well guys, this is how I'll do it:

P={S::=X|epsilon,
   X::=aXc|M|epsilon,
   M::=bMc|epsilon}
Shalev Shalit
  • 1,945
  • 4
  • 24
  • 34
slow
  • 1
  • 1
0

My answer:

S -> aAc | aSc

A -> bc | bAc

where S is the start symbol.

0

L = {epsilon, ac, bc, abcc, abbccc, aabbcccc,.....}

We can try to increase c every time either a or b is increased.

S -> aSc|bSc|epsilon

-1

S-> aBc/epsilon B-> bBc/S/epsilon This takes care of the order of the alphabets as well