Content deleted Content added
Undid revision 824862462 by Special:Contributions/2601:204:CD00:FE8F:0:0:0:B053 |
Citation bot (talk | contribs) Removed URL that duplicated identifier. | Use this bot. Report bugs. | #UCB_CommandLine |
||
(72 intermediate revisions by 53 users not shown) | |||
Line 1:
{{Short description|Parsing algorithm for context-free grammars}}
{{Redirect|CYK||Cyk (disambiguation)}}
{{Infobox algorithm
|name=Cocke–Younger–Kasami algorithm (CYK)
|class=[[Parsing]] with [[context-free grammar]]s
|data=[[String (computer science)|String]]
|time=<math>\mathcal{O}\left( n^3 \cdot \left| G \right| \right)</math>, where:
* <math>n</math> is length of the string
* <math>|G|</math> is the size of the CNF grammar
}}
In [[computer science]], the '''Cocke–Younger–Kasami algorithm''' (alternatively called '''CYK''', or '''CKY''') is a [[parsing]] [[algorithm]] for [[context-free grammar]]s published by Itiroo Sakai in 1961.<ref>{{cite book |last1=Grune |first1=Dick |title=Parsing techniques : a practical guide |date=2008 |publisher=Springer |___location=New York |page=579 |isbn=978-0-387-20248-8 |edition=2nd}}</ref><ref>Itiroo Sakai, “Syntax in universal translation”. In Proceedings 1961 International Conference on Machine Translation of Languages and Applied Language Analysis, Her Majesty’s Stationery Office, London, p. 593-608, 1962.</ref> The algorithm is named after some of its rediscoverers: [[John Cocke (computer scientist)|John Cocke]], Daniel Younger, [[Tadao Kasami]], and [[Jacob T. Schwartz]]. It employs [[bottom-up parsing]] and [[dynamic programming]].
The standard version of CYK operates only on context-free grammars given in [[Chomsky normal form]] (CNF). However any context-free grammar may be transformed to a CNF grammar expressing the same language {{harv|Sipser|1997}}.▼
▲The standard version of CYK operates only on context-free grammars given in [[Chomsky normal form]] (CNF). However any context-free grammar may be algorithmically transformed
The importance of the CYK algorithm stems from its high efficiency in certain situations. Using [[Landau symbol]]s, the [[Analysis of algorithms|worst case running time]] of CYK is '''Ο'''<math>(n^3 \cdot |G|)</math>, where ''n'' is the length of the parsed string and ''|G|'' is the size of the CNF grammar ''G'' {{harv|Hopcroft|Ullman|1979|p=140}}. This makes it one of the most efficient parsing algorithms in terms of worst-case [[asymptotic complexity]], although other algorithms exist with better average running time in many practical scenarios.▼
▲The importance of the CYK algorithm stems from its high efficiency in certain situations. Using [[
==Standard form==
The [[dynamic programming]] algorithm requires the context-free grammar to be rendered
==Algorithm==
Line 17 ⟶ 28:
'''let''' the grammar contain ''r'' nonterminal symbols ''R''<sub>1</sub> ... ''R''<sub>''r''</sub>, with start symbol ''R''<sub>1</sub>.
'''let''' ''P''[''n'',''n'',''r''] be an array of booleans. Initialize all elements of ''P'' to false.
'''let''' ''back''[''n'',''n'',''r''] be an array of lists of backpointing triples. Initialize all elements of ''back'' to the empty list.
'''for each''' ''s'' = 1 to ''n''
'''for each''' unit production ''R''<sub>''v''</sub>
'''set''' ''P''[''1'',''s'',''v''] = true
'''for each''' ''l'' = 2 to ''n'' ''-- Length of span''
'''for each''' ''s'' = 1 to ''n''-''l''+1 ''-- Start of span''
'''for each''' ''p'' = 1 to ''l''-1 ''-- Partition of span''
'''for each''' production ''R''<sub>''a''</sub>
'''if''' ''P''[''p'',''s'',''b''] and ''P''[''l''-''p'',''s''+''p'',''c''] '''then'''
'''set''' ''P''[''l'',''s'',''a''] = true, append <p,b,c> to ''back''[''l'',''s'',''a'']
'''if''' ''P''[n,''1'',''1''] is true '''then'''
''I'' is member of language
'''return''' ''back'' -- by ''retracing the steps through back, one can easily construct all possible parse trees of the string.''
'''else'''
''
<div class="toccolours mw-collapsible mw-collapsed">▼
==== Probabilistic CYK (for finding the most probable parse) ====
▲<div class="toccolours mw-collapsible mw-collapsed">
<div class="mw-collapsible-content">
'''let''' the input be a string ''I'' consisting of ''n'' characters: ''a''<sub>1</sub> ... ''a''<sub>''n''</sub>.
Line 42 ⟶ 59:
'''let''' ''back''[''n'',''n'',''r''] be an array of backpointing triples.
'''for each''' ''s'' = 1 to ''n''
'''for each''' unit production ''R''<sub>''v''</sub>
'''set''' ''P''[''1'',''s'',''v''] = Pr(''R''<sub>''v''</sub>
'''for each''' ''l'' = 2 to ''n'' ''-- Length of span''
'''for each''' ''s'' = 1 to ''n''-''l''+1 ''-- Start of span''
'''for each''' ''p'' = 1 to ''l''-1 ''-- Partition of span''
'''for each''' production ''R''<sub>''a''</sub>
prob_splitting = Pr(''R''<sub>''a''</sub>
'''if'''
'''set''' ''P''[''l'',''s'',''a''] = prob_splitting
'''set''' ''back''[''l'',''s'',''a''] = <p,b,c>
'''if''' ''P''[n,''1'',''1''] > 0 '''then'''
find the parse tree by retracing through ''back''
'''return''' the parse tree
'''else'''
'''return''' "not a member of language"
</div>
</div>
===As prose===
In informal terms, this algorithm considers every possible substring of the input string and sets <math>P[l,s,v]</math> to be true if the substring of length <math>l</math> starting from <math>s</math> can be generated from
==Example==
Line 77 ⟶ 100:
\end{align}</math>
Now the sentence ''she eats a fish with a fork'' is analyzed using the CYK algorithm. In the following table,
{| class="wikitable" style="text-align:center"
Line 105 ⟶ 128:
===Generating a parse tree===
The above algorithm is a [[recognizer]] that will only determine if a sentence is in the language. It is simple to extend it into a [[parser]] that also
The end result is then a shared-forest of possible parse trees, where common trees parts are factored between the various parses. This shared forest can conveniently be read as an [[ambiguous grammar]] generating only the sentence parsed, but with the same ambiguity as the original grammar, and the same parse trees up to a very simple renaming of non-terminals, as shown by {{harvtxt|Lang|1994}}.
===Parsing non-CNF context-free grammars===
Line 114 ⟶ 137:
===Parsing weighted context-free grammars===
It is also possible to extend the CYK algorithm to parse strings using [[weighted context-free grammar|weighted]] and [[stochastic context-free grammar]]s. 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).
==== Numerical stability ====
When the probabilistic CYK algorithm is applied to a long string, the splitting probability can become very small due to multiplying many probabilities together. This can be dealt with by summing log-probability instead of multiplying probabilities.
===Valiant's algorithm===
Line 119 ⟶ 145:
as the CYK algorithm; yet he showed that [[Matrix multiplication algorithm#Sub-cubic algorithms|algorithms for efficient multiplication]] of [[Boolean matrix|matrices with 0-1-entries]] can be utilized for performing this computation.
Using the [[Coppersmith–Winograd algorithm]] for multiplying these matrices, this gives an asymptotic worst-case running time of <math>O(n^{2.38} \cdot |G|)</math>. However, the constant term hidden by the [[Big O Notation]] is so large that the Coppersmith–Winograd algorithm is only worthwhile for matrices that are too large to handle on present-day computers {{harv|Knuth|1997}}, and this approach requires subtraction and so is only suitable for recognition. The dependence on efficient matrix multiplication cannot be avoided altogether: {{harvtxt|Lee|2002}} has proved that any parser for context-free grammars working in time <math>O(n^{3-\varepsilon} \cdot |G|)</math> can be effectively converted into an algorithm computing the product of <math>(n \times n)</math>-matrices with 0-1-entries in time <math>O(n^{3 - \varepsilon/3})</math>, and this was extended by Abboud et al.<ref>{{cite arXiv|last1=Abboud|first1=Amir|last2=Backurs|first2=Arturs|last3=Williams|first3=Virginia Vassilevska|date=2015-11-05|title=If the Current Clique Algorithms are Optimal, so is Valiant's Parser|class=cs.CC|eprint=1504.01431}}</ref> to apply to a constant-size grammar.
==See also==
Line 125 ⟶ 151:
* [[Earley parser]]
* [[Packrat parser]]
* [[Inside–outside algorithm]]
==References==
{{reflist}}
*{{cite techreport |last1=Cocke |first1=John |authorlink1=John Cocke |last2=Schwartz |first2=Jacob T. |date=April 1970 |title=Programming languages and their compilers: Preliminary notes |edition=2nd revised |publisher=[[Courant Institute of Mathematical Sciences|CIMS]], [[New York University|NYU]] |url=http://www.softwarepreservation.org/projects/FORTRAN/CockeSchwartz_ProgLangCompilers.pdf}}▼
* {{cite book | isbn=0-201-02988-X | first1=John E. |last1=Hopcroft |author1link=John E. Hopcroft |first2= Jeffrey D. |last2=Ullman |author2link= Jeffrey D. Ullman| title=Introduction to Automata Theory, Languages, and Computation | ___location=Reading/MA | publisher=Addison-Wesley | year=1979 |ref=harv }} ▼
== Sources ==
*{{cite techreport |last1=Kasami |first1=T. |authorlink1=Tadao Kasami |year=1965 |title=An efficient recognition and syntax-analysis algorithm for context-free languages |number=65-758 |publisher=[[Air Force Cambridge Research Laboratories|AFCRL]]}}▼
*{{cite conference |title= Syntax in universal translation |last= Sakai |first= Itiroo |date= 1962 |___location= London |publisher= Her Majesty’s Stationery Office |volume= II |pages= 593–608 |conference= 1961 International Conference on Machine Translation of Languages and Applied Language Analysis, Teddington, England}}
▲*{{cite
▲* {{cite book | isbn=0-201-02988-X | first1=John E. | last1=Hopcroft |
*{{cite journal |last1=Lange |first1=Martin |last2=Leiß |first2=Hans |title=To CNF or not to CNF? An Efficient Yet Presentable Version of the CYK Algorithm |year=2009 |journal=Informatica Didactica |volume=8 |url=http://www.informatica-didactica.de/cmsmadesimple/index.php?page=LangeLeiss2009 |ref=harv}}▼
▲*{{cite
*{{cite journal |last1=Lee |first1=Lillian |title=Fast context-free grammar parsing requires fast Boolean matrix multiplication |journal=[[Journal of the ACM|J. ACM]] |volume=49 |issue=1 |pages=1–15 |year=2002 |doi=10.1145/505241.505242 |ref=harv|url=https://arxiv.org/pdf/cs/0112018 }}▼
*{{cite book |last1=
*{{cite journal |last1=
▲*{{cite journal |last1=Lange |first1=Martin |last2=Leiß |first2=Hans |title=To CNF or not to CNF? An Efficient Yet Presentable Version of the CYK Algorithm |year=2009 |journal=Informatica Didactica |volume=8 |url=http://www.informatica-didactica.de
*{{cite journal |last1=Younger |first1=Daniel H. |date=February 1967 |title=Recognition and parsing of context-free languages in time ''n''<sup>3</sup> |journal=[[Information and Computation|Inform. Control]] |volume=10 |issue=2 |pages=189–208 |doi=10.1016/s0019-9958(67)80007-x}}▼
▲*{{cite journal |last1=Lee |first1=Lillian |author-link=Lillian Lee (computer scientist)|title=Fast context-free grammar parsing requires fast Boolean matrix multiplication |journal=[[Journal of the ACM|J. ACM]] |volume=49 |issue=1 |pages=1–15 |year=2002 |doi=10.1145/505241.505242 |
*{{cite book |last1=Sipser |first1=Michael |author-link1=Michael Sipser |title=Introduction to the Theory of Computation |publisher=IPS |year=1997 |edition=1st |page=[https://archive.org/details/introductiontoth00sips/page/99 99] |isbn=0-534-94728-X |url=https://archive.org/details/introductiontoth00sips/page/99 }}
*{{cite journal |last1=Valiant |first1=Leslie G. |author-link1=Leslie Valiant |title=General context-free recognition in less than cubic time |journal=[[Journal of Computer and System Sciences|J. Comput. Syst. Sci.]] |volume=10 |issue=2 |year=1975 |pages=308–314 |doi=10.1016/s0022-0000(75)80046-8 |doi-access=free }}
▲*{{cite journal |last1=Younger |first1=Daniel H. |date=February 1967 |title=Recognition and parsing of context-free languages in time ''n''<sup>3</sup> |journal=[[Information and Computation|Inform. Control]] |volume=10 |issue=2 |pages=189–208 |doi=10.1016/s0019-9958(67)80007-x|doi-access=free }}
==External links==
* [https://raw.org/tool/cyk-algorithm/ Interactive Visualization of the CYK algorithm]
* [https://martinlaz.github.io/demos/cky.html CYK parsing demo in JavaScript]
* [
▲* [http://www.swisseduc.ch/compscience/exorciser/ Exorciser is a Java application to generate exercises in the CYK algorithm as well as Finite State Machines, Markov algorithms etc]
{{Parsers}}
|