将语法转换为乔姆斯基范式?
将以下语法转换为乔姆斯基范式。给出所有中间步骤。
S -> AB | aB
A -> aab|lambda
B -> bbA
好吧,我做的第一件事就是添加一个新的起始变量 S0
所以现在我
S0 -> S
S -> AB | aB
A -> aab|lambda
B -> bbA
删除了所有的 lambda 规则:
S0 -> S
S -> AB | aB | B
A -> aab
B -> bbA | bb
然后我检查了 S->S
和 A->B
类型的规则不存在。这就是我得出的答案,我需要做进一步的事情还是我做错了什么?
Convert the grammar below into Chomsky Normal Form. Give all the intermediate steps.
S -> AB | aB
A -> aab|lambda
B -> bbA
Ok so the first thing I did was add a new start variable S0
so now I have
S0 -> S
S -> AB | aB
A -> aab|lambda
B -> bbA
then I removed all of the lambda rules:
S0 -> S
S -> AB | aB | B
A -> aab
B -> bbA | bb
Then I checked for S->S
and A->B
type rules which did not exist. And that was the answer I came up with, do I need to do anything further or did I do anything wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
维基百科说:
继续你的工作:
不要使用
|
来表示不同的选择,而是将一个规则拆分为多个规则。创建新规则
Y -> a
和Z -> b
因为我们很快就会需要它们。<代码>S - > aB 的形式不是
S ->; BC
因为a
是一个终端。因此将a
更改为Y
:对
B -> 执行相同的操作bb
规则:对于
A -> aab
,创建C -> YY
;对于B -> bbA
,创建D -> ZZ:
对于
S -> B
,复制右侧出现S
的一条规则并内联该规则:处理规则
S0 -> B
和S0 -> S
通过将其他规则的右侧连接到左侧。另外,删除孤立的规则(其中 LHS 符号永远不会在 RHS 上使用):我们就完成了。唷!
Wikipedia says:
Continuing your work:
Instead of using
|
to denote different choices, split a rule into multiple rules.Create new rules
Y -> a
andZ -> b
because we will need them soon.S -> aB
is not of the formS -> BC
becausea
is a terminal. So changea
intoY
:Do the same for the
B -> bb
rule:For
A -> aab
, createC -> YY
; forB -> bbA
, createD -> ZZ
:For
S -> B
, duplicate the one rule whereS
occurs on the right hand side and inline the rule:Deal with the rules
S0 -> B
andS0 -> S
by joining the right hand side to the left hand sides of other rules. Also, delete the orphaned rules (where the LHS symbol never gets used on RHS):And we're done. Phew!
在不涉及太多理论和证明的情况下(您可以在维基百科中查看),将上下文无关语法转换为乔姆斯基范式时必须做一些事情,通常必须执行四个范式转换。首先,您需要直接或间接地识别所有可以产生空字符串(lambda/epsilon)的变量 - (Lambda-Free 形式)。其次,您需要删除单元产生式 - (无单元形式)。第三,你需要找到所有活跃/有用(有用性)的变量。四、需要找到所有可达符号(Reachable)。在每一步中,您可能会也可能不会有新的语法。因此,对于你的问题,这就是我想出的...
上下文无关语法
删除 lambda/epsilon
删除单元生成
确定活动符号
删除无法访问
用实心非终结符替换所有混合字符串
乔姆斯基范式
Without getting into too much theory and proofs(you could look at this in Wikipedia), there are a few things you must do when converting a Context Free Grammar to Chomsky Normal Form, you generally have to perform four Normal-Form Transformations. First, you need to identify all the variables that can yield the empty string(lambda/epsilon), directly or indirectly - (Lambda-Free form). Second, you need to remove unit productions - (Unit-Free form). Third, you need to find all the variables that are live/useful (Usefulness). Four, you need to find all the reachable symbols (Reachable). At each step you might or might not have a new grammar. So for your problem this is what I came up with...
Context-Free Grammar
Remove lambda/epsilon
Remove unit produtions
Determine live symbols
Remove unreachable
Replace all mixed strings with solid nonterminals
Chomsky Normal Form
替代答案:语法只能产生有限数量的字符串,即 6 个。
您现在可以手动将其压缩回乔姆斯基范式。
通过替换,我们可以找到所有产生的字符串的集合。你的初始规则:
首先拆分
S
规则:现在将 A 和 B 展开替换为:
再次展开这些得到:
要将这个有限集更改为乔姆斯基范式,只需通过暴力破解即可没有任何智能因素的力量。首先,我们引入两个终端规则:
现在,对于每个字符串,我们使用终端变量使用第一个字母,使用新变量使用其余字母。例如,像这样:
我们只是对所有 6 个字符串进行这个过程,生成很多新的中间变量。
Alternative answer: The grammar can only produce a finite number of strings, namely 6.
You can now condense this back to Chomsky Normal Form by hand.
By substitution, we can find the set of all strings produced. Your initial rules:
First split up the
S
rule:Now substitute what A and B expand into:
Expand these again to get:
To change this finite set to Chomsky Normal Form, it suffices to do it by brute force without any intelligent factoring. First we introduce two terminal rules:
Now for each string, we consume the first letter with a terminal variable and the remaining letters with a new variables. For example, like this:
We just go through this process for all 6 strings, generating a lot of new intermediate variables.