-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnfa_and_dfa.py
71 lines (54 loc) · 1.75 KB
/
nfa_and_dfa.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python
# -*- coding: utf-8 -*-
class NFANode(object):
def __init__(self, name=None, _type=0):
super(NFANode, self).__init__()
self.name = name
self._type = _type
self.edge = {}
def add_edge(self, alpha, target):
if alpha not in self.edge:
targets = set()
targets.add(target)
self.edge[alpha] = targets
else:
self.edge[alpha].add(target)
class NFA(object):
def __init__(self, alphabets):
super(NFA, self).__init__()
self.status = {}
self.alphabets = alphabets
def get_target(self, cur_status, alpha):
if cur_status in self.status:
if alpha in self.status[cur_status]:
return self.status[cur_status][alpha]
return None
class DFANode(object):
def __init__(self, name, _type=None):
super(DFANode, self).__init__()
self.name = name
self._type = _type
self.edge = {}
def add_edge(self, alpha, target):
if alpha not in self.edge:
targets = set()
targets.add(target)
self.edge[alpha] = targets
else:
self.edge[alpha].add(target)
class LRDFANode(object):
def __init__(self, set_id):
self.set_id = set_id
self.object_set = set()
self.edge = {}
def add_object_set(self, id, left, right, index, tail):
tmp = (id, left, right, index, tail)
if tmp not in self.object_set:
self.object_set.add(tmp)
def add_object_set_by_set(self, object_set):
self.object_set |= object_set
class DFA(object):
def __init__(self, alphabets):
super(DFA, self).__init__()
self.status = {}
self.alphabets = alphabets