简体   繁体   English

生成语言L的BNF语法

[英]BNF grammar that generates language L

I spend a lot of time but still can't understand how to solve this. 我花了很多时间,但仍然不明白如何解决这个问题。 I have the answer. 我有答案。 Can you tell me how he come up with it? 你能告诉我他是怎么想的吗?

Is there a specific rules or standard construct I can follow? 我可以遵循特定的规则或标准构造吗? I know the rules for union and concatenation but not when it's reversed. 我知道联合和连接的规则,但不知道相反的规则。

BNF语法无歧义

这是解决方案,但我不明白他是如何提出的。编写此类语法时,我可以遵循特定的规则吗?

If x is a (possibly improper) substring of y , then x^rcy can be written as x^R c wxz where w and z are arbitrary strings over (a+b)* . 如果xy子字符串(可能不合适),则x^rcy可以写成x^R c wxz ,其中wz(a+b)*上的任意字符串。

Now we can try to "slice" the strings in this language to see how they can be generated by CFGs. 现在,我们可以尝试以这种语言对字符串进行“切片”,以查看CFG如何生成它们。 The non-regular part of this is making sure that x^R and x come out right; 此操作的非常规部分是确保x^Rx正确显示; the rest is trivial. 其余的都是微不足道的。 So we need some rules like: 因此,我们需要一些规则,例如:

S -> aXa | bYb | c

This gives us x^R cx . 这给我们x^R cx We are missing the w and the z . 我们缺少wz The w we can add by letting any arbitrary string come after c : 我们可以通过在c之后添加任意字符串来添加w

S -> aSa | bSb | cA
A -> aA | bA | e

The z we can get by changing S to T and adding a new S that produces T followed by anything: 我们可以通过将S更改为T并添加一个新的S来生成z ,该S产生T后跟任何内容:

S -> TA
T -> aTa | bTb | cA
A -> aA | bA | e

All we neglected to ensure was that |x| > 1 我们忽略的只是确保|x| > 1 |x| > 1 . |x| > 1 We do that by changing T -> cA to two productions T -> acAa | bcAb 为此,我们将T -> cA更改为两个产品T -> acAa | bcAb T -> acAa | bcAb . T -> acAa | bcAb This yields the suggested grammar (Not the only right one, just one grammar that works) and explains how we got there. 这产生了建议的语法(不是唯一正确的语法,只是一种有效的语法)并解释了我们如何到达那里。

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

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