简体   繁体   English

a ^ nb ^ m的上下文无关文法

[英]context free grammar for a^n b^m

I'm trying to make a CFG for the language: 我正在尝试为该语言制作CFG:

在此处输入图片说明

I did this: 我这样做:

S -> a S b b 
S -> epsilon

It fulfills the requirement, but is it correct? 它满足要求,但是正确吗?

What if I wanted abbb ? 如果我想要abbb怎么abbb n is 1, therefore 2n is 2 and 4n is 4, m is 3, so 2n <= m < 4n . n为1,因此2n为2而4n为4, m为3,因此2n <= m < 4n If you mean "does it create all the words in the language" as "is it correct", then no, it does not. 如果您的意思是“是否创建了该语言中的所有单词”为“是否正确”,那么不,不是。 Also, you are creating epsilon (zero a and zero b ), but it's not correct that 2*0 <= 0 < 4*0 because zero is not less than zero (second part of the comparison). 另外,您正在创建epsilon (零a和零b ),但是2*0 <= 0 < 4*0并不正确,因为零不小于零(比较的第二部分)。

Your grammar is creating only words where there is twice more b than a . 您的语法仅创建b大于a两倍的单词。 However, the language contains also other words - you need to have between 2 times more b (including) and less than 4 times more b than a . 但是,该语言还包含其他单词-您需要将b (包括)多2倍,并将b小于a 4倍。 Eg for n = 2 your m can be 4, 5, 6 or 7. 例如,对于n = 2您的m可以是4、5、6或7。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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