简体   繁体   English

构造CFG

[英]Constructing a CFG

I literally can't find the answer to this: 我从字面上找不到答案:

L = {vw | L = {vw | v element of {a,b) , w element of {b,c} , number of a's <= number of c's} {a,b)的v元素,{ b,c}的w元素,a的数量<= c的数量}

V --> aV | V-> aV | bV | bV | e Ë

W --> bW | W-> bW | cW | 连续 e Ë

But I cannot think of how to combine the construction of the words v and w after one another and keeping in mind the mentioned restriction.. Anyone who could lend me a hand? 但是我想不出如何将v和w两个词的构造相互结合起来,并牢记上述限制。任何人都可以伸出援手吗?

As I explained in my answers tips for writing cfg , correct approach is first understand all possible patterns of strings in language then write rules. 正如我在回答cfg的提示中所解释的那样,正确的方法是首先理解语言中所有可能的字符串模式,然后编写规则。

What is this language L? L是什么语言? In strings of language L: 使用语言L的字符串:

1. All 'a' s comes before any 'c' s 1.所有'a'都在任何'c'之前
2. Number of 'a' are less than or equals to number of 'c' , So in grammar of L, if a production rule adds one symbol 'a' then it must also add one or more 'c' . 2.'a'小于或等于的数'c' ,所以在升语法,如果产生式规则增加了一个符号'a'那么它也必须添加一个或多个'c'
3. There is no restriction on occurrence of symbol 'b' , it can appear anywhere any number of times. 3.符号'b'出现没有限制,它可以出现在任意位置多次。 4. Empty (null) string is also belongs to L as number of 'a' = number if 'c' == 0. And for same reason a string consists of only symbol 'b' is also acceptable. 4.空(空)字符串也属于L,因为'a' =数字(如果'c' == 0)。出于同样的原因,字符串仅包含符号'b'也是可以接受的。
5. Any string without 'a' also belongs L (in other words (c + b)* is subset of L). 5.任何没有'a'字符串也属于L(换句话说(c + b)*是L的子集)。

Now writing grammar rules are easy (read comments to understand each production rules): 现在编写语法规则很容易(阅读注释以了解每个生产规则):

S →  BaBSBCB | ^    // add `a` add `C` also, B can be any where so added B   
Z →  CZ | BZ | ^    // to create `(c + b)*`
C →  cC | c         // C always generates one or more `c`s  
B →  bB | ^         // there is no restriction on B it generates `b`s or ^ 

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

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