简体   繁体   English

构造CFG

[英]constructing CFG

How can I construct a Context-Free Grammer for the language x^ay^bz^2(a+b) where a>=0, b>=0. 如何为语言x ^ ay ^ bz ^ 2(a + b)构造一个上下文无关的Grammer,其中a> = 0,b> = 0。 Thanks for helps... 感谢您的帮助...

Think of it this way 这样想吧

x^a y^b z^2(a+b) = x^a y^b z^2a z^2b = x^a y^b (z^2)^b (z^2)^a 

Therefore 因此

S -> xSzz | S1
S1 -> yS1zz | e

Observe that for each x and for each y , you need to generate two z 's because of the 2( a + b ). 观察到每个x和每个y ,由于2( a + b ),需要生成两个z Also, observe that each string can be viewed as an "inside" part of y 's and z 's, and an "outside" part of x 's and z 's. 另外,观察到每个字符串能够作为一个“内部”部分被看作y的和z的,和的‘外’部x的和z的。

Since for each y you need two z 's, the inside part can be described by (using capitals to denote non-terminal symbols and [] for the empty string): 由于每个y需要两个z ,因此内部部分可以用(用大写字母表示非终结符,用[]表示空字符串)来描述:

I --> []
I --> y I z z

Now write a grammar for the outside part in the same way, but referring to I in the base case. 现在以相同的方式为外部编写语法,但在基本情况下引用I

There are essentially two cases that you need to treat: 您实际上需要处理两种情况:

  • You can either add an x at the beginning of the string, in which case you need to add two z 's at the end. 您可以在字符串的开头添加x ,在这种情况下,您需要在字符串的末尾添加两个z
  • Or you can add a y in the middle, in which case you also need to add two z 's at the end. 或者,您可以在中间添加y ,在这种情况下,您需要在末尾添加两个z

Try reducing either of these descriptions to a simpler grammer (eg a^nb^n ) for which you know the solution. 尝试将这些描述中的任何一个简化为您知道解决方案的简单语法(例如a^nb^n )。

This hint should be enough to deduce the generative grammer. 这个提示应该足以推断出生成的语法。

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

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