简体   繁体   中英

How can I construct a grammar that generates this language? grammar context-free-grammar

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

Construct a grammar that generates L: L = {a^nb^mc^2n | n>=0, m>=0}

How can I construct a grammar that generates this language? grammar context-free-grammar automata

I think this should do the trick. I verified this on http://mdaines.github.io/grammophone/ .
S -> a B cc | a S cc | .
B -> b B | .

I find it always helps with these kinds of questions to come up with some rules for how to build big strings out of little strings. First, identify the littlest strings in your language. In our case, we can start with the observation that if n = 0, b^m is in our language; that is, w in b* is in our language. We then note that if x is a string in our language we get another string by adding one a on the left and two cs on the right; that is, axcc is a string in our language also. So our rules are:

  1. b* in L
  2. if x in L then axcc in L

Writing this in terms of a CFG is now straightforward:

S -> B
S -> aScc

Here, S generates our language L and B generates the language b*. We complete the grammar by providing a grammar for b* with start symbol B:

(1) S -> B
(2) S -> aScc
(3) B -> e
(4) B -> B

Any string a^nb^mc^2n can be generated using n applications of rule 2, 1 application of rule 1, m applications of rule 4 and 1 application of rule 3. That this grammar generates no strings not in the language is left as an exercise.

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