Java CYK算法伪码混淆
因此,我一直在维基百科和许多powerpoints/PDF中阅读有关 在维基百科中,有一部分我并不是100%地理解它想要表达的意思。你们能帮我把它分解一下吗Java CYK算法伪码混淆,java,algorithm,context-free-grammar,cyk,Java,Algorithm,Context Free Grammar,Cyk,因此,我一直在维基百科和许多powerpoints/PDF中阅读有关 在维基百科中,有一部分我并不是100%地理解它想要表达的意思。你们能帮我把它分解一下吗 let the input be a string S consisting of n characters: a1 ... an. let the grammar contain r nonterminal symbols R1 ... Rr. This grammar contains the subset Rs which is th
let the input be a string S consisting of n characters: a1 ... an.
let the grammar contain r 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 S is member of language
else
S is not member of language
真正让我困惑的是,“如果p[1,n,x]中的任何一个为真(x在集合s上迭代,其中s是Rs的所有索引),那么s是语言的成员
其他的
S不是语言的成员“
它是说,对于任何存在的n和x,如果它是真的,那么它就是一个成员?
或者说对于字符串长度n和x,如果它是真的,那么它就是一个成员?还是完全不同的东西
那么X到底是什么呢
编辑:
谢谢你们,我已经学会了怎么做。
希望我能得到你的两个答案作为所选答案。它的意思是,如果p[1,n,x]对于任何起始的非终结符x都是真的,那么整个字符串(从1到n的词法标记)将被解析为非终结符x。在该算法中,P[a,b,c]=true意味着从索引a开始并具有长度b的词法标记的子串可以被解析为非终结符c。
当您执行CYK算法时,您基本上是从底部到最上面的元素填充底部三角形矩阵。当某个元素
(j,i,x)
其中j
是列索引,i
是行索引,x
是非终端符号时,这意味着您能够从符号Rx
生成单词的子序列j
到j+i-1
您的目标是从一个起始符号生成整个单词。与生成整个单词的可能性相对应的元素是(1,n,x)
——矩阵最左边和最上面的元素,其中x
是非终端符号的索引。由于您必须在其中一个开始符号中开始,因此您只需查找所有非端子的子集—s
的子集。如果您设法从一个开始符号生成整个单词,您只需声明该单词是语言的一部分。如果不存在这样的起始符号,则无法生成该单词,并且该单词不是语法描述的语言的一部分