How can I construct a grammar that generates this language?
Asked Answered
G

9

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?

Geaghan answered 20/6, 2009 at 15:48 Comment(5)
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 != TestGeaghan
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.Overfill
Actually it's the "finite automata & grammars" part that sounds like homework. Doesn't matter if it's for a test or not.Overfill
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.Geaghan
Shouldn't such questions be migrated to Theoretical Computer Science?Professed
A
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.

Aberdeen answered 20/6, 2009 at 16:2 Comment(3)
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?Hyperbaton
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)Dissociate
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.Dustin
G
4
S -> X
X -> aXc | Y
Y -> bYc | e

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

Gardell answered 4/12, 2010 at 8:0 Comment(0)
L
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'.

Litharge answered 20/6, 2009 at 15:58 Comment(1)
Thanks. That 'hint' is actually the answer. And no, it's not homework.Geaghan
A
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

Audrey answered 14/5, 2013 at 4:15 Comment(0)
D
0

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

P={S::=X|epsilon,
   X::=aXc|M|epsilon,
   M::=bMc|epsilon}
Discursion answered 16/2, 2016 at 14:58 Comment(0)
S
0

My answer:

S -> aAc | aSc

A -> bc | bAc

where S is the start symbol.

Swat answered 7/12, 2019 at 15:52 Comment(0)
H
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

Hexone answered 24/3, 2023 at 10:14 Comment(0)
N
0
if **L= a^n b^n c^n+m**

means

L={e,ac,bc,aacc,bbcc,----,abcc,aabbcccc,abbccc,aaabbccccc,-------}

where 'e' is epsilon

S-> aSc/e/A
A-> bSc/e

like to generate aabbbccccc now, aSc will make aacc (balance a and c) then A will replace S and generate aabbbccccc (balance b and c)

Niddering answered 5/4 at 6:20 Comment(0)
R
-1

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

Readytowear answered 4/9, 2022 at 17:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.