【问题标题】:Cannot understand CYK Algorithm pseudo-code看不懂 CYK 算法伪代码
【发布时间】:2013-04-07 00:53:17
【问题描述】:

我正在阅读有关CYK algorithm 的信息,其中一部分伪代码我无法理解。整个伪代码是:

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

这些部分是我很困惑的:

    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

有人能提供一些关于这些伪代码的提示吗?

【问题讨论】:

  • @syb0rg:我故意留下缩进,以便更容易从大块代码中找到较小的sn-p代码。
  • @nhahtdh 已修复(格式化习惯,抱歉)。
  • @syb0rg:小代码sn-p的缩进有点不对(你可以从原始代码复制粘贴)。

标签: java algorithm parsing cyk


【解决方案1】:

伪代码

对于每个产生式 RA → RB RC

如果 P[j,k,B] 和 P[j+k,i-k,C] 则设置 P[j,i,A] = true

应按以下方式解释。假设 P[j, k, B] 为真。这意味着从位置 j 开始的 k 个字符组成的字符串可以从非终结符 RB 派生。如果 P[j + k, i - k, C] 也是如此,则从位置 j + k 开始的 i - k 个字符形成的字符串可以从非终结符 RC。因此,由于 RA → RB RC 是一个产生式,所以从位置 j 开始的 i 个字符组成的字符串是这样的可以从RA推导出来。

我认为将伪代码解释为可能会有所帮助

对于每个产生式 RA → RB RC

如果 P[j,k,B] == true 且 P[j+k,i-k,C] == true,则设置 P[j,i,A] = true

希望这会有所帮助!

【讨论】:

  • 请问A B 和C 的索引是什么?
  • @user2280838- 该算法对所有非终结符 R_1、R_2、...、R_n 进行编号。这里,A、B 和 C 发生是产生式 R_A -> R_B R_C 中非终结符的索引。例如,如果产生式是 S -> TU 并且 S 具有索引 1,T 具有索引 2,U 具有索引 3,那么我们将有 A = 1、B = 2 和 C = 3。这有帮助吗?
  • 它确实有帮助,但是如果 A B 和 C 作为非终结符在语法中被多次定义怎么办? ID 值的索引类型的分配是否有助于其他非终结符区分?
  • @user2280838- 伪代码通过为每个非终结符分配一个唯一 ID 来工作。如果您将多个产生式关联起来,您不会两次“重新定义”同一个非终结符;每次都是同一个非终结符。这意味着即使你有类似 S -> UT 和 S -> XY 之类的东西,S 在两种情况下都是相同的(并且具有相同的索引)。
猜你喜欢
  • 2017-05-16
  • 2021-08-22
  • 2017-10-13
  • 2023-03-28
  • 1970-01-01
  • 2012-05-08
  • 1970-01-01
  • 1970-01-01
  • 2014-06-17
相关资源
最近更新 更多