在无上下文的语法中,我们在替换过程中是否替换所有变量?还是我们只能将替代规则仅适用于同一类型的变量?

发布于 2025-01-24 07:56:24 字数 235 浏览 3 评论 0原文

想象一下,我们有一个免费的语言语法,CFG,如下:

S-> A ...(1)

S-> )

中得出一个字符串,如下:

,我在指定的语言

2

ss ... ( 在1个变量上而不是所有相同变量上的施加规则?

因此,我的问题是我是否要在“ SS”上应用规则(1),我是否可以选择(1)将(1)应用于“ SS”的1秒钟,还是我必须对两个s申请?

Imagine we have a Context Free Grammer, CFG, as follows:

S -> a ...(1)

S -> SS ...(2)

And i derive a string in the specified language as follows:

S ..start state

SS ..using (2)

aS ...using (1) <- is this valid, like only applying the subsititution rule on 1 variable instead of all same variables?

So my question is if i were to apply rule (1) on "SS", do i have the option to apply (1) to only 1 S of the "SS" or do i have to apply to both of them?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深居我梦 2025-01-31 07:56:24

您只能将规则应用于一个S,也可以按照自己的方式应用。这是一个更复杂的例子,可以更好地说明这个想法:

S -> a       (1)
S -> b       (2)
S -> SS      (3)

那么,这种语言中有什么字符串?这是带有派生的前几个字符串:

a: S -> a
b: S -> b
aa: S -> SS -> aS -> aa
    S -> SS -> Sa -> aa
ab: S -> SS -> aS -> ab
    S -> SS -> Sb -> as
ba: S -> SS -> bS -> ba
    S -> SS -> Sa -> ba
bb: S -> SS -> bS -> bb
    S -> SS -> Sb -> bb
aaa: S -> SS -> aS -> aSS -> aaS -> aaa
     S -> SS -> aS -> aSS -> aSa -> aaa
     S -> SS -> Sa -> SSa -> aSa -> aaa
     S -> SS -> Sa -> SSa -> Saa -> aaa
aab
...

无论如何,我们发现语法会产生A和B的所有非空字符串,包括那些混合A和B的弦。如果我们必须用相同的规则替换所有S,那么如果我们有一种(非空)的语言,我们将获得更小得多的语言。

You can apply the rule to only one S, or as many as you like. Here is a slightly more complicated example that maybe better illustrates the idea:

S -> a       (1)
S -> b       (2)
S -> SS      (3)

So, what strings are in this language? Here are the first few strings with derivations:

a: S -> a
b: S -> b
aa: S -> SS -> aS -> aa
    S -> SS -> Sa -> aa
ab: S -> SS -> aS -> ab
    S -> SS -> Sb -> as
ba: S -> SS -> bS -> ba
    S -> SS -> Sa -> ba
bb: S -> SS -> bS -> bb
    S -> SS -> Sb -> bb
aaa: S -> SS -> aS -> aSS -> aaS -> aaa
     S -> SS -> aS -> aSS -> aSa -> aaa
     S -> SS -> Sa -> SSa -> aSa -> aaa
     S -> SS -> Sa -> SSa -> Saa -> aaa
aab
...

Anyway, we find that the grammar generates all non-empty strings of a's and b's, including those with mixed up a's and b's. If we had to replace all S's with the same rule, we would get a much, much smaller language, if we'd get a (non-empty) language at all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文