-
Notifications
You must be signed in to change notification settings - Fork 15
/
cyk.py
63 lines (32 loc) · 1.05 KB
/
cyk.py
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
class Production(object):
def __init__(self, *terms):
self.terms = terms
def __len__(self):
return len(self.terms)
def __getitem__(self, index):
return self.terms[index]
def __iter__(self):
return iter(self.terms)
def __repr__(self):
return " ".join(str(t) for t in self.terms)
def __eq__(self, other):
if not isinstance(other, Production):
return False
return self.terms == other.terms
def __ne__(self, other):
return not (self == other)
def __hash__(self):
return hash(self.terms)
class Rule(object):
def __init__(self, name, *productions):
self.name = name
self.productions = list(productions)
def __str__(self):
return self.name
def __repr__(self):
return "%s -> %s" % (self.name, " | ".join(repr(p) for p in self.productions))
def add(self, *productions):
self.productions.extend(productions)
grammar =
def cfg_to_cnf(start):
S0 = Rule("S0", Production(start))