简体   繁体   English

为最多两个 0 的偶数长度字构造 CFG

[英]Constructing CFG for even length words with maximum of two 0's

I am struggling to construct a good CFG for a L={xE{0,1}* |我正在努力为L={xE{0,1}* |构建一个好的 CFG that is of even length and have a maximum of two 0s}它的长度是偶数,最多有两个 0}

So words like L={11, 10, 0011...}所以像L={11, 10, 0011...}这样的词

I am trying with the following attempt.我正在尝试以下尝试。

S -> E | E0A | A0E | E0E0E | 00EA | EA00

E-> 1A | e

A -> 1E

I am running different derivations and they seem to make sense, but I am still unsure if my grammar is correct, or is there is a better way to improve it?我正在运行不同的推导,它们似乎很有意义,但我仍然不确定我的语法是否正确,或者是否有更好的方法来改进它? Thank you very much, I have been struggling with CFG and I am trying to practice more to help me understand.非常感谢,我一直在努力学习 CFG,我正在努力练习更多来帮助我理解。

Your CFG does not appear to be correct since I do not see a derivation for 1001. Probably there are other issues with it.您的 CFG 似乎不正确,因为我没有看到 1001 的推导。可能还有其他问题。

We can begin with a CFG for the language of even-length strings of 0s and 1s:我们可以从 0 和 1 的偶数长度字符串语言的 CFG 开始:

S -> 0S0 | 0S1 | 1S0 | 1S1 | e

The only problem with this grammar is that it can produce two many 0s.这个语法的唯一问题是它可以产生两个许多 0。 We can avoid this by introducing new nonterminals after every production that adds a zero.我们可以通过在每次添加零的产生式之后引入新的非终结符来避免这种情况。 Then, those other nonterminals can "remember" that we've seen the 0s already and have fewer valid productions.然后,那些其他非终结符可以“记住”我们已经看到了 0,并且有效产生式更少。 So...所以...

S -> 0A0 | 0B1 | 1B0 | 1S1 | e
A -> 1A1 | e
B -> 0A1 | 1A0 | 1B1 | e

In this grammar, S represents having introduced no zeroes so far;在这个文法中,S 表示到目前为止还没有引入零; A to having introduced both allowed zeroes; A 引入了两个允许的零; and B to having introduced just one. B 只介绍了一个。

Let X stand for "even number of 1s" and Y for "odd number of 1s".让 X 代表“偶数个 1”,Y 代表“奇数个 1”。

You have 0, 1 or 2 zeroes, separating 1, 2 or 3 groups of 1s.您有 0、1 或 2 个零,分隔 1、2 或 3 组 1。 These groups of ones, together with the zeroes, must have an even number of characters.这些 1 组以及 0 必须有偶数个字符。 Since every X has an even number of characters, we must have an even number of 0s and Ys.由于每个 X 有偶数个字符,我们必须有偶数个 0 和 Y。

Now write down all the possible cases.现在写下所有可能的情况。

Another option here: the language you're describing here is regular.这里的另一种选择:您在此处描述的语言是常规的。 That means that you could...这意味着你可以...

  • ... write a regular expression for the language, then convert that into a CFG. ...为该语言编写一个正则表达式,然后将其转换为 CFG。 There's a really nice way to do this - concatenations get preserved as concatenations, unions turn into different production options, and stars get rewritten in terms of a new nonterminal that expands to "any number of copies of the thing being starred."有一个非常好的方法可以做到这一点 - 连接被保留为连接,联合变成不同的生产选项,并且星号被重写为一个新的非终结符,该非终结符扩展到“被加星标的任何数量的副本”。
  • ... design a DFA or NFA for the language, then convert it to a right-linear grammar. ...为该语言设计一个 DFA 或 NFA,然后将其转换为右线性语法。 That is, make one nonterminal for each state.也就是说,为每个 state 创建一个非终结符。 Add productions of the form A → bB for each transition in the automaton from state A to state B on the character b, then add productions of the form A → ε for each accepting state.在字符 b 上为自动机中从 state A 到 state B 的每个转换添加形式 A → bB 的产生式,然后为每个接受 Z9ED39E2EA9314586B6A985A6E 添加形式 A → ε 的产生式

My suspicion is that the second of these options will give you a fairly straightforward CFG with a manageable number of nonterminals.我怀疑这些选项中的第二个将为您提供一个相当简单的 CFG,其中包含可管理数量的非终结符。 Specifically, form the DFA as the cross product of a simple DFA that counts the number of 0s and a simple DFA that tracks parity, then convert that to a CFG.具体来说,将 DFA 形成为计算 0 数量的简单 DFA 和跟踪奇偶校验的简单 DFA 的叉积,然后将其转换为 CFG。

Hope this helps!希望这可以帮助!

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

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