乔姆斯基范式

发布于 2024-10-16 04:53:24 字数 31 浏览 3 评论 0原文

为什么我们要将语法转换为乔姆斯基范式?有优势吗?

why do we convert the grammar to chomsky normal form ? Is there a advantage ?

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

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

发布评论

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

评论(4

深府石板幽径 2024-10-23 04:53:25

一方面,您可以在乔姆斯基范式语法上使用 CYK 算法

For one thing, you can use the CYK algorithm on Chomsky Normal Form grammars

别理我 2024-10-23 04:53:25

乔姆斯基范式使多项式时间算法能够决定是否可以通过语法生成字符串。
如果您了解动态编程,那么该算法非常流畅...

如果您的输入 (I) 的长度为 n,那么您将采用一个大小为 nxn 的二维数组 (A)。

A[i,j]表示文法G中所有可以导出子串I(i,j)的符号。

所以最后如果 A[1,n] 包含起始符号(S)那么这意味着字符串 I 可以由 S 导出,这就是我们想要检查的。

def decide (string s,grammar G):
    //base case
    for i=1 to n:
        N[i,i]=I[i]    //as the substring of length one can be generated by only a
                       terminal.
    //end base case

    //induction
    for s=1 to n:       //length of substring
        for i=1 to n-s-1: //start index of substring
            for j=i to i+s-1:   //something else
                 if there exists a rule A->BC such that B belongs to N[i,j] and C
                 belongs to N[j+1,i+s-1] then add A to N[i,i+s-1]
    //endInduction

    if S belongs to N[1,n] then accept else reject.

我知道指数看起来相当疯狂。但基本上这就是发生的事情。

- 我认为基本情况非​​常清楚

- 在归纳步骤中,我们从所有长度小于 s 的解决方案中构建长度 s 子串的解决方案。

- 假设您正在寻找从索引 1 开始的长度为 5 的子字符串 (sub) 的解决方案。然后您启动一个循环(其他部分)......它检查是否存在规则(A ->BC),使得 B 和 C 导出 sub 的两个连续且不相交的子串,如果是这样,则将所有此类 A 添加到 N[1,6]。

-最后,如果您在 N[1,n] 中有开始符号,那么您接受!

Chomsky normal form enables a polynomial time algorithm to decide whether a string can be generated by a grammar.
The algorithm is pretty slick if you know dynamic programming...

If the length of your input (I) is n then you take a 2d array (A) of dim nxn.

A[i,j] denotes all the symbols in the grammar G that can derive the sub-string I(i,j).

So finally if A[1,n] contains the start symbol(S) then it means that the string I can be derived by S which is what we wanted to check.

def decide (string s,grammar G):
    //base case
    for i=1 to n:
        N[i,i]=I[i]    //as the substring of length one can be generated by only a
                       terminal.
    //end base case

    //induction
    for s=1 to n:       //length of substring
        for i=1 to n-s-1: //start index of substring
            for j=i to i+s-1:   //something else
                 if there exists a rule A->BC such that B belongs to N[i,j] and C
                 belongs to N[j+1,i+s-1] then add A to N[i,i+s-1]
    //endInduction

    if S belongs to N[1,n] then accept else reject.

I know that the indexes seem pretty crazy. But basically here's whats happening.

-the base case is pretty clear I think

-in the inductive step we build the solution for a length s substring from all the solutions with length less than s.

-say you are finding the solution for length 5 substring (sub) starting at index 1. Then you start a loop (something else part).....which checks whether there is a rule (A->BC) such that B and C derive two contiguous and disjoint substrings of sub and if so add all such A's to N[1,6].

-Finally if you have the start symbol in N[1,n] then you accept!

一杆小烟枪 2024-10-23 04:53:25

例如,CNF 中的语法(或者更确切地说是它的派生树)用于证明上下文无关语言的泵引理。

For example, grammar in CNF (or rather its derivation tree) is used to prove pumping lemma for context-free languages.

眉目亦如画i 2024-10-23 04:53:25

使用乔姆斯基范式的优点是:

  1. 证明简单
    我们有许多上下文无关语法的证明,包括可还原性和与自动机的等价性。但这些是必须处理的更简单且更受限制的语法集。因此,范式是有帮助的。
    例如,Greibach 范式用于表明每个 CFL(不包含 ε)都存在一个无 ε-跃迁的 PDA。

2.启用解析
PDA用于解析任何语法的单词,这很不方便。范式为我们提供了更多的结构可供使用,从而使解析算法变得更容易。

例如,CYK 算法使用 Chomsky 范式。另一方面,Greibach 范式支持递归下降解析;尽管可能需要回溯,但空间复杂度是线性的。

Advantages of using Chomsky Normal Form are:

  1. Simplicity of proof
    We have many proofs for context-free grammars, including reducability and equivalence to automata. But these are the simpler and the more restricted set of grammars have to dealth with.Therefore, normal forms are helpful.
    For example, Greibach normal form is used to show that there is an ε-transition-free PDA for every CFL (that does not contain ε).

2.Enables parsing
The PDAs are used to parse words with any grammar, which is inconvenient. Normal forms give us more structure to work with, resulting in easier parsing algorithms.

For example, the CYK algorithm uses Chomsky normal form. Greibach normal form, on the other hand, enables recursive-descent parsing; even though backtracking may be necessary, space complexity is linear.

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