-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathAutomataTheory.py
453 lines (415 loc) · 17 KB
/
AutomataTheory.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
from os import popen
import time
class Automata:
"""class to represent an Automata"""
def __init__(self, language = set(['0', '1'])):
self.states = set()
self.startstate = None
self.finalstates = []
self.transitions = dict()
self.language = language
@staticmethod
def epsilon():
return ":e:"
def setstartstate(self, state):
self.startstate = state
self.states.add(state)
def addfinalstates(self, state):
if isinstance(state, int):
state = [state]
for s in state:
if s not in self.finalstates:
self.finalstates.append(s)
def addtransition(self, fromstate, tostate, inp):
if isinstance(inp, str):
inp = set([inp])
self.states.add(fromstate)
self.states.add(tostate)
if fromstate in self.transitions:
if tostate in self.transitions[fromstate]:
self.transitions[fromstate][tostate] = self.transitions[fromstate][tostate].union(inp)
else:
self.transitions[fromstate][tostate] = inp
else:
self.transitions[fromstate] = {tostate : inp}
def addtransition_dict(self, transitions):
for fromstate, tostates in transitions.items():
for state in tostates:
self.addtransition(fromstate, state, tostates[state])
def gettransitions(self, state, key):
if isinstance(state, int):
state = [state]
trstates = set()
for st in state:
if st in self.transitions:
for tns in self.transitions[st]:
if key in self.transitions[st][tns]:
trstates.add(tns)
return trstates
def getEClose(self, findstate):
allstates = set()
states = set([findstate])
while len(states)!= 0:
state = states.pop()
allstates.add(state)
if state in self.transitions:
for tns in self.transitions[state]:
if Automata.epsilon() in self.transitions[state][tns] and tns not in allstates:
states.add(tns)
return allstates
def display(self):
print "states:", self.states
print "start state: ", self.startstate
print "final states:", self.finalstates
print "transitions:"
for fromstate, tostates in self.transitions.items():
for state in tostates:
for char in tostates[state]:
print " ",fromstate, "->", state, "on '"+char+"'",
print
def getPrintText(self):
text = "language: {" + ", ".join(self.language) + "}\n"
text += "states: {" + ", ".join(map(str,self.states)) + "}\n"
text += "start state: " + str(self.startstate) + "\n"
text += "final states: {" + ", ".join(map(str,self.finalstates)) + "}\n"
text += "transitions:\n"
linecount = 5
for fromstate, tostates in self.transitions.items():
for state in tostates:
for char in tostates[state]:
text += " " + str(fromstate) + " -> " + str(state) + " on '" + char + "'\n"
linecount +=1
return [text, linecount]
def newBuildFromNumber(self, startnum):
translations = {}
for i in list(self.states):
translations[i] = startnum
startnum += 1
rebuild = Automata(self.language)
rebuild.setstartstate(translations[self.startstate])
rebuild.addfinalstates(translations[self.finalstates[0]])
for fromstate, tostates in self.transitions.items():
for state in tostates:
rebuild.addtransition(translations[fromstate], translations[state], tostates[state])
return [rebuild, startnum]
def newBuildFromEquivalentStates(self, equivalent, pos):
rebuild = Automata(self.language)
for fromstate, tostates in self.transitions.items():
for state in tostates:
rebuild.addtransition(pos[fromstate], pos[state], tostates[state])
rebuild.setstartstate(pos[self.startstate])
for s in self.finalstates:
rebuild.addfinalstates(pos[s])
return rebuild
def getDotFile(self):
dotFile = "digraph DFA {\nrankdir=LR\n"
if len(self.states) != 0:
dotFile += "root=s1\nstart [shape=point]\nstart->s%d\n" % self.startstate
for state in self.states:
if state in self.finalstates:
dotFile += "s%d [shape=doublecircle]\n" % state
else:
dotFile += "s%d [shape=circle]\n" % state
for fromstate, tostates in self.transitions.items():
for state in tostates:
for char in tostates[state]:
dotFile += 's%d->s%d [label="%s"]\n' % (fromstate, state, char)
dotFile += "}"
return dotFile
class BuildAutomata:
"""class for building e-nfa basic structures"""
@staticmethod
def basicstruct(inp):
state1 = 1
state2 = 2
basic = Automata()
basic.setstartstate(state1)
basic.addfinalstates(state2)
basic.addtransition(1, 2, inp)
return basic
@staticmethod
def plusstruct(a, b):
[a, m1] = a.newBuildFromNumber(2)
[b, m2] = b.newBuildFromNumber(m1)
state1 = 1
state2 = m2
plus = Automata()
plus.setstartstate(state1)
plus.addfinalstates(state2)
plus.addtransition(plus.startstate, a.startstate, Automata.epsilon())
plus.addtransition(plus.startstate, b.startstate, Automata.epsilon())
plus.addtransition(a.finalstates[0], plus.finalstates[0], Automata.epsilon())
plus.addtransition(b.finalstates[0], plus.finalstates[0], Automata.epsilon())
plus.addtransition_dict(a.transitions)
plus.addtransition_dict(b.transitions)
return plus
@staticmethod
def dotstruct(a, b):
[a, m1] = a.newBuildFromNumber(1)
[b, m2] = b.newBuildFromNumber(m1)
state1 = 1
state2 = m2-1
dot = Automata()
dot.setstartstate(state1)
dot.addfinalstates(state2)
dot.addtransition(a.finalstates[0], b.startstate, Automata.epsilon())
dot.addtransition_dict(a.transitions)
dot.addtransition_dict(b.transitions)
return dot
@staticmethod
def starstruct(a):
[a, m1] = a.newBuildFromNumber(2)
state1 = 1
state2 = m1
star = Automata()
star.setstartstate(state1)
star.addfinalstates(state2)
star.addtransition(star.startstate, a.startstate, Automata.epsilon())
star.addtransition(star.startstate, star.finalstates[0], Automata.epsilon())
star.addtransition(a.finalstates[0], star.finalstates[0], Automata.epsilon())
star.addtransition(a.finalstates[0], a.startstate, Automata.epsilon())
star.addtransition_dict(a.transitions)
return star
class DFAfromNFA:
"""class for building dfa from e-nfa and minimise it"""
def __init__(self, nfa):
self.buildDFA(nfa)
self.minimise()
def getDFA(self):
return self.dfa
def getMinimisedDFA(self):
return self.minDFA
def displayDFA(self):
self.dfa.display()
def displayMinimisedDFA(self):
self.minDFA.display()
def buildDFA(self, nfa):
allstates = dict()
eclose = dict()
count = 1
state1 = nfa.getEClose(nfa.startstate)
eclose[nfa.startstate] = state1
dfa = Automata(nfa.language)
dfa.setstartstate(count)
states = [[state1, count]]
allstates[count] = state1
count += 1
while len(states) != 0:
[state, fromindex] = states.pop()
for char in dfa.language:
trstates = nfa.gettransitions(state, char)
for s in list(trstates)[:]:
if s not in eclose:
eclose[s] = nfa.getEClose(s)
trstates = trstates.union(eclose[s])
if len(trstates) != 0:
if trstates not in allstates.values():
states.append([trstates, count])
allstates[count] = trstates
toindex = count
count += 1
else:
toindex = [k for k, v in allstates.iteritems() if v == trstates][0]
dfa.addtransition(fromindex, toindex, char)
for value, state in allstates.iteritems():
if nfa.finalstates[0] in state:
dfa.addfinalstates(value)
self.dfa = dfa
def acceptsString(self, string):
currentstate = self.dfa.startstate
for ch in string:
if ch==":e:":
continue
st = list(self.dfa.gettransitions(currentstate, ch))
if len(st) == 0:
return False
currentstate = st[0]
if currentstate in self.dfa.finalstates:
return True
return False
def minimise(self):
states = list(self.dfa.states)
n = len(states)
unchecked = dict()
count = 1
distinguished = []
equivalent = dict(zip(range(len(states)), [{s} for s in states]))
pos = dict(zip(states,range(len(states))))
for i in range(n-1):
for j in range(i+1, n):
if not ([states[i], states[j]] in distinguished or [states[j], states[i]] in distinguished):
eq = 1
toappend = []
for char in self.dfa.language:
s1 = self.dfa.gettransitions(states[i], char)
s2 = self.dfa.gettransitions(states[j], char)
if len(s1) != len(s2):
eq = 0
break
if len(s1) > 1:
raise BaseException("Multiple transitions detected in DFA")
elif len(s1) == 0:
continue
s1 = s1.pop()
s2 = s2.pop()
if s1 != s2:
if [s1, s2] in distinguished or [s2, s1] in distinguished:
eq = 0
break
else:
toappend.append([s1, s2, char])
eq = -1
if eq == 0:
distinguished.append([states[i], states[j]])
elif eq == -1:
s = [states[i], states[j]]
s.extend(toappend)
unchecked[count] = s
count += 1
else:
p1 = pos[states[i]]
p2 = pos[states[j]]
if p1 != p2:
st = equivalent.pop(p2)
for s in st:
pos[s] = p1
equivalent[p1] = equivalent[p1].union(st)
newFound = True
while newFound and len(unchecked) > 0:
newFound = False
toremove = set()
for p, pair in unchecked.items():
for tr in pair[2:]:
if [tr[0], tr[1]] in distinguished or [tr[1], tr[0]] in distinguished:
unchecked.pop(p)
distinguished.append([pair[0], pair[1]])
newFound = True
break
for pair in unchecked.values():
p1 = pos[pair[0]]
p2 = pos[pair[1]]
if p1 != p2:
st = equivalent.pop(p2)
for s in st:
pos[s] = p1
equivalent[p1] = equivalent[p1].union(st)
if len(equivalent) == len(states):
self.minDFA = self.dfa
else:
self.minDFA = self.dfa.newBuildFromEquivalentStates(equivalent, pos)
class NFAfromRegex:
"""class for building e-nfa from regular expressions"""
def __init__(self, regex):
self.star = '*'
self.plus = '+'
self.dot = '.'
self.openingBracket = '('
self.closingBracket = ')'
self.operators = [self.plus, self.dot]
self.regex = regex
self.alphabet = [chr(i) for i in range(65,91)]
self.alphabet.extend([chr(i) for i in range(97,123)])
self.alphabet.extend([chr(i) for i in range(48,58)])
self.buildNFA()
def getNFA(self):
return self.nfa
def displayNFA(self):
self.nfa.display()
def buildNFA(self):
language = set()
self.stack = []
self.automata = []
previous = "::e::"
for char in self.regex:
if char in self.alphabet:
language.add(char)
if previous != self.dot and (previous in self.alphabet or previous in [self.closingBracket,self.star]):
self.addOperatorToStack(self.dot)
self.automata.append(BuildAutomata.basicstruct(char))
elif char == self.openingBracket:
if previous != self.dot and (previous in self.alphabet or previous in [self.closingBracket,self.star]):
self.addOperatorToStack(self.dot)
self.stack.append(char)
elif char == self.closingBracket:
if previous in self.operators:
raise BaseException("Error processing '%s' after '%s'" % (char, previous))
while(1):
if len(self.stack) == 0:
raise BaseException("Error processing '%s'. Empty stack" % char)
o = self.stack.pop()
if o == self.openingBracket:
break
elif o in self.operators:
self.processOperator(o)
elif char == self.star:
if previous in self.operators or previous == self.openingBracket or previous == self.star:
raise BaseException("Error processing '%s' after '%s'" % (char, previous))
self.processOperator(char)
elif char in self.operators:
if previous in self.operators or previous == self.openingBracket:
raise BaseException("Error processing '%s' after '%s'" % (char, previous))
else:
self.addOperatorToStack(char)
else:
raise BaseException("Symbol '%s' is not allowed" % char)
previous = char
while len(self.stack) != 0:
op = self.stack.pop()
self.processOperator(op)
if len(self.automata) > 1:
print self.automata
raise BaseException("Regex could not be parsed successfully")
self.nfa = self.automata.pop()
self.nfa.language = language
def addOperatorToStack(self, char):
while(1):
if len(self.stack) == 0:
break
top = self.stack[len(self.stack)-1]
if top == self.openingBracket:
break
if top == char or top == self.dot:
op = self.stack.pop()
self.processOperator(op)
else:
break
self.stack.append(char)
def processOperator(self, operator):
if len(self.automata) == 0:
raise BaseException("Error processing operator '%s'. Stack is empty" % operator)
if operator == self.star:
a = self.automata.pop()
self.automata.append(BuildAutomata.starstruct(a))
elif operator in self.operators:
if len(self.automata) < 2:
raise BaseException("Error processing operator '%s'. Inadequate operands" % operator)
a = self.automata.pop()
b = self.automata.pop()
if operator == self.plus:
self.automata.append(BuildAutomata.plusstruct(b,a))
elif operator == self.dot:
self.automata.append(BuildAutomata.dotstruct(b,a))
def drawGraph(automata, file = ""):
"""From https://github.com/max99x/automata-editor/blob/master/util.py"""
f = popen(r"dot -Tpng -o graph%s.png" % file, 'w')
try:
f.write(automata.getDotFile())
except:
raise BaseException("Error creating graph")
finally:
f.close()
def isInstalled(program):
"""From http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python"""
import os
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program) or is_exe(program+".exe"):
return True
else:
for path in os.environ["PATH"].split(os.pathsep):
exe_file = os.path.join(path, program)
if is_exe(exe_file) or is_exe(exe_file+".exe"):
return True
return False