-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcyk.rb
41 lines (40 loc) · 1.47 KB
/
cyk.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class CYK
# checks whether a grammar accepts given string
# assumes input grammar to be in CNF
def self.parse(grammar, string)
nonterminals = grammar.nonterminals.to_a
terminals = grammar.terminals.to_a
n = string.length
r = nonterminals.size
# create n x n x r matrix
tbl = Array.new(n) { |_| Array.new(n) { |_| Array.new(r, false) } }
(0...n).each { |s|
grammar.rules.each { |rule|
# check if rule is unit production: A -> b
next unless rule.rhs.size == 1
unit_terminal = rule.rhs[0]
if unit_terminal.value == string[s]
v = nonterminals.index(rule.lhs)
tbl[0][s][v] = true
end
}
}
(2..n).each { |l|
(0...n - l + 1).each { |s|
(1..l - 1).each { |p|
grammar.rules.each { |rule|
next unless rule.rhs.size == 2
a = nonterminals.index(rule.lhs)
b = nonterminals.index(rule.rhs[0])
c = nonterminals.index(rule.rhs[1])
if tbl[p - 1][s][b] and tbl[l - p - 1][s + p][c]
tbl[l - 1][s][a] = true
end
}
}
}
}
v = nonterminals.index(grammar.start_sym)
return tbl[n - 1][0][v]
end
end