-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
114 lines (102 loc) · 3.06 KB
/
config.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
def loadAutomata(path):
f = open(path)
f = f.read()
f = f.lower()
f = f.split('\n')
#Decode header
tmp = []
header = f[0]
header = header.split('|')
for line in header:
element = line.strip()
if element:
tmp.append(element)
header = tmp
#Decode body
tmp = []
body = f[1:]
start = None
finals = set()
for line in body:
if line:
tmp2 = []
splittedLine = line.split('|')
for item in splittedLine:
item = item.strip()
if item:
if ',' in item:
item = item.split(',')
tmp3 = []
for i in item:
tmp3.append(i.strip())
item = sorted(tmp3)
tmp2.append(item)
else:
s = False
f = False
if '*' in item:
item = item[:-1]
f = True
if '--' in item:
item = item[2:]
s = True
if s:
start = item
if f:
finals.add(item)
tmp2.append([item])
tmp.append(tmp2)
body = tmp
tableLength = len(header)
automataTable = {}
for line in body:
source = line[0][0]
automataTable[source] = {}
source = automataTable[source]
for x in range(tableLength):
character = header[x]
target = []
for item in line[x+1]:
if item != '-':
target.append(item)
source[character] = target
return (automataTable, start, finals)
def saveAutomata(recipe, start, finals, path):
firstCol = 'state'
spaces = 30
f = open(path, 'w')
states = recipe.keys()
alphabet = set()
for state in states:
for character in recipe[state]:
alphabet.add(character)
alphabet = sorted(list(alphabet))
#Header
line = '{}'.format(firstCol).center(spaces)
line += '|'
for item in alphabet:
line += '{}'.format(item).center(spaces)
line += '|'
line += '\n'
f.write(line)
#Body
for state in states:
strstate = str(state)
if start in state:
strstate = '--' + strstate
if state in finals:
strstate = strstate + '*'
line = '{}'.format(state).center(spaces)
line += '|'
for character in alphabet:
if isinstance(recipe[state][character], set):
target = str(recipe[state][character])[1:-1]
else:
target = str(recipe[state][character])
if target == '()':
target = '-'
line += '{}'.format(target).center(spaces)
line += '|'
line += '\n'
f.write(line)
f.close()