简体   繁体   English

如何构建生成这种语言的语法?

[英]How can I construct a grammar that generates this language?

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:我相信我的作品应该是 go 沿着这条线:

    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?我的 C 生产如何记住 m 和 n 的数字? I'm guessing this must rather be a context-free grammar, if so, how should it be?我猜这一定是一个上下文无关的语法,如果是这样,它应该是怎样的?

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.您需要在构建过程中强制计算 C'c。 In order to show it's context-free, I would consider to use Pump Lemma .为了表明它是上下文无关的,我会考虑使用Pump Lemma

S -> X
X -> aXc | Y
Y -> bYc | e

where e == epsilon and X is unnecessary but added for clarity其中e == epsilonX是不必要的,但为了清楚起见添加

Yes, this does sound like homework, but a hint:是的,这听起来像家庭作业,但有一个提示:

Every time you match an 'a', you must match a 'c'.每次匹配“a”时,都必须匹配“c”。 Same for matching a 'b'.匹配“b”也是如此。

S->aSc|A A->bAc|λ 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.这意味着当你至少得到 a 时,你至少有 1 c,或者如果你得到 a 和 b,你必须有 2 c。 i hope it has been helpful我希望它有帮助

Well guys, this is how I'll do it:好吧,伙计们,这就是我要做的:

P={S::=X|epsilon,
   X::=aXc|M|epsilon,
   M::=bMc|epsilon}

My answer:我的答案:

S -> aAc | S -> aAc | aSc aSc

A -> bc | A -> bc | bAc乙腈

where S is the start symbol.其中 S 是起始符号。

S-> aBc/epsilon B-> bBc/S/epsilon This takes care of the order of the alphabets as well S-> aBc/epsilon B-> bBc/S/epsilon 这也处理了字母的顺序

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

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