简体   繁体   English

从 {a, b}* 的 {w 元素构造 CFG : 2#a(w)=3#b(w)}

[英]Construct CFG from {w element of {a, b}* : 2#a(w)=3#b(w)}

If i have following language { x is element of {a,b}*, where 2#a(x)=3#b(x), then the cfg of that language is :如果我有以下语言 { x 是 {a,b}* 的元素,其中 2#a(x)=3#b(x),那么该语言的 cfg 是:

S=>SaSaSaSbSbS |SaSaSbSaSbS|SaSaSbSbSaS | S=>SaSaSaSbSbS |SaSaSbSaSbS|SaSaSbSbSaS | SaSbSaSaSbS| SaSbSaSaSbS| SaSbSaSbSaS | SaSbSaSbSaS | SaSbSbSaSaS |SbSaSaSaSbS |SbSaSaSbSaS |SbSaSbSaSaS |SbSbSaSaSaS | SaSbSbSaS |SbSaSaSbS |SbSaSbSaS |SbSaSbSaSaS |SbSbSaSaS | epsilon/lambda ε/λ

Is this correct?这样对吗? If this isnt correct/there's another more simple form, can you tell it?如果这不正确/还有另一种更简单的形式,你能告诉它吗? I have no clue on another form other than that.我不知道除此之外的另一种形式。

At a glance it looks like this probably works:乍一看,这看起来可能有效:

  1. your base case is good;你的基本情况很好; the empty string is in the language空字符串在语言中

  2. you cover all your inductive cases: you only add 2 a and 3 b and you cover all arrangements你涵盖了你所有的归纳案例:你只添加了 2 a 和 3 b,你就涵盖了所有的安排

I am not seeing a fundamentally simpler solution than this, although you might be able to remove either the leading or the trailing S from the right-hand side of all productions;我没有看到比这更简单的解决方案,尽管您可以从所有作品的右侧删除前导或尾随 S; then, by choosing a production you'd be committing to that first or last terminal symbol, but I think that still works out.然后,通过选择一个作品,您将致力于第一个或最后一个终端符号,但我认为这仍然有效。 Possibly even removing both leading and trailing S so you commit to both the first and the last.甚至可能删除前导和尾随 S,以便您同时提交第一个和最后一个。 Any other simplification seems like it would increase the number of productions or the number of nonterminals, or both, which while possibly reducing the total number of symbols needed to encode the grammar, arguably doesn't make the grammar any simpler (indeed, more nonterminals and productions is typically seen as more complicated, not less).任何其他简化似乎都会增加产生式的数量或非终结符的数量,或两者兼而有之,虽然可能会减少编码语法所需的符号总数,但可以说不会使语法变得更简单(实际上,更多的非终结符并且制作通常被视为更复杂,而不是更少)。 If you want to experiment with adding productions or nonterminals, consider eg T => Sa and R => Sb, just to cut down on repetition.如果您想尝试添加产生式或非终结符,请考虑例如 T => Sa 和 R => Sb,以减少重复。

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

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