Answers:
Chomsky范式使多项式时间算法可以确定语法是否可以生成字符串。如果您知道动态编程,则该算法非常精巧。
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.
我知道索引看起来很疯狂。但是基本上这是正在发生的事情。
sub