三篇关于“Chomsky normal form”的文章

Chomsky normal form
From Wikipedia, the free encyclopedia
Jump to: navigation, search

In computer science, a formal grammar is in Chomsky normal form iff all production rules are of the form:

    A → BC or
    A → α or
    S → ε

where A, B and C are nonterminal symbols, α is a terminal symbol (a symbol that represents a constant value), S is the start symbol, and ε is the empty string. Also, neither B nor C may be the start symbol.

Every grammar in Chomsky normal form is context-free, and conversely, every context-free grammar can be efficiently transformed into an equivalent one which is in Chomsky normal form.

With the exception of the optional rule S → ε (included when the grammar may generate the empty string), all rules of a grammar in Chomsky normal form are expansive; thus, throughout the derivation of a string, each string of terminals and nonterminals is always either the same length or one element longer than the previous such string. The derivation of a string of length n is always exactly 2n-1 steps long. Furthermore, since all rules deriving nonterminals transform one nonterminal to exactly two nonterminals, a parse tree based on a grammar in Chomsky normal form is a binary tree, and the height of this tree is limited to at most the length of the string.

Because of these properties, many proofs in the field of languages and computability make use of the Chomsky normal form. These properties also yield various efficient algorithms based on grammars in Chomsky normal form; for example, the CYK algorithm that decides whether a given string can be generated by a given grammar uses the Chomsky normal form.

The Chomsky normal form is named after Noam Chomsky, the US linguist who invented the Chomsky hierarchy.

 Chomsky Normal Form
A context free grammar is in chomsky normal form if each production yields a terminal or two nonterminals.  Any context free grammar can be converted to chomsky normal form.

First remove all E productions.  If x → E, then a production like y → AxBxC spins off the productions y → ABC | AxBC | ABxC.  Perform similar substitutions for all nonterminals that lead to the empty string, then remove productions that yield E.  If E is in the language, i.e. s → E, then this production must remain.  This is the only nonstandard production in chomsky normal form; all other productions must yield a terminal or two nonterminals.

Next, remove any productions x → x, as they are pointless.  Given x → y, let x derive everything that y derives, then remove the unit production x → y.

At this point the right side of each production has two or more symbols.  Introduce new symbols to play out the right side.  For instance, x → AyzBC might be replaced with the following.

x → q1q2
q1 → A
q2 → yq3
q3 → zq4
q4 → q5q6
q5 → B
q6 → C

Do this across the board and the resulting grammar is in chomsky normal form.

Prove that a word of length n is derived in 2n-1 steps.  Hint, each terminal implies one step.

CYK algorithm
From Wikipedia, the free encyclopedia
Jump to: navigation, search

The Cocke-Younger-Kasami (CYK) algorithm (alternatively called CKY) determines whether a string can be generated by a given context-free grammar and, if so, how it can be generated. This is known as parsing the string. The algorithm is an example of dynamic programming.

The standard version of CYK recognizes languages defined by context-free grammars written in Chomsky normal form (CNF). Since any context-free grammar can be converted to CNF without too much difficulty, CYK can be used to recognize any context-free language. It is also possible to extend the CYK algorithm to handle some context-free grammars which are not written in CNF; this may be done to improve performance, although at the cost of making the algorithm harder to understand.

The worst case asymptotic time complexity of CYK is Θ(n3), where n is the length of the parsed string. This makes it one of the most efficient (in those terms) algorithms for recognizing any context-free language. However, there are other algorithms that will perform better for certain subsets of the context-free languages.
Contents

    * 1 The algorithm
          o 1.1 Informally
          o 1.2 Algorithm extension
    * 2 References

The algorithm

The CYK algorithm is a bottom up algorithm and is important theoretically, since it can be used to constructively prove that the membership problem for context-free languages is decidable.

The CYK algorithm for the membership problem is as follows:

Let the input string consist of n letters, a1 ... an.
Let the grammar contain r terminal and nonterminal symbols R1 ... Rr.
This grammar contains the subset Rs which is the set of start symbols.
Let P[n,n,r] be an array of booleans. Initialize all elements of P to false.
For each i = 1 to n
  For each unit production Rj → ai, set P[i,1,j] = true.
For each i = 2 to n -- Length of span
  For each j = 1 to n-i+1 -- Start of span
    For each k = 1 to i-1 -- Partition of span
      For each production RA -> RB RC
        If P[j,k,B] and P[j+k,i-k,C] then set P[j,i,A] = true
If any of P[1,n,x] is true (x is iterated over the set s, where s are all the indices for Rs)
  Then string is member of language
  Else string is not member of language

[edit]

Informally

In informal terms, this algorithm considers every possible substring of the string of letters and sets P[i,j,k] to be true if the substring of letters starting from i of length j can be generated from Rk. Once it has considered substrings of length 1, it goes on to substrings of length 2, and so on. For substrings of length 2 and greater, it considers every possible partition of the substring into two halves, and checks to see if there is some production P → Q R such that Q matches the first half and R matches the second half. If so, it records P as matching the whole substring. Once this process is completed, the sentence is recognized by the grammar if the substring containing the entire string is matched by the start symbol.
CYK table S
    VP
       
S            
    VP             PP
S         NP             NP
NP     V, VP     Det.     N     P     Det     N
she     eats     a     fish     with     a     fork
[edit]

Algorithm extension

It is simple to extend the above algorithm to not only determine if a sentence is in a language, but to also construct a parse tree, by storing parse tree nodes as elements of the array, instead of booleans. Since the grammars being recognized can be ambiguous, it is necessary to store a list of nodes (unless one wishes to only pick one possible parse tree); the end result is then a forest of possible parse trees. An alternative formulation employs a second table B[n,n,r] of so-called backpointers.

It is also possible to extend the CYK algorithm to parse strings using weighted and stochastic context-free grammars. Weights (probabilities) are then stored in the table P instead of booleans, so P[i,j,A] will contain the minimum weight (maximum probability) that the substring from i to j can be derived from A. Further extensions of the algorithm allow all parses of a string to be enumerated from lowest to highest weight (highest to lowest probability).
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值