-
Notifications
You must be signed in to change notification settings - Fork 1
/
DeterministicFA.py
110 lines (101 loc) · 2.98 KB
/
DeterministicFA.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
import sys
import re
dfa = True
fa = []
ss = 0
stn = None
terms = []
symbs = None
eps = False
eps_symb = '@'
def ReadFiniteAutomata(textform):
textline = textform.split("\n")
line = textline[0]
r1 = re.compile(R"\s*(\d+).*")
m1 = r1.search(line)
global stn
stn = int(m1.group(1))
global symbs
symbs = textline[1]
stp = textline[2]
ssn = 0
tn = 0
for i in range(0,stn):
fa.append((int(stp[i]), []))
if stp[i] == '1' or stp[i] == '3':
global ss
ss = i
ssn = ssn + 1
if stp[i] == '2' or stp[i] == '3':
terms.append(i)
tn = tn + 1
if ssn != 1:
sys.stdout.write("ERR, != 1 start st")
if tn < 1:
sys.stdout.write("ERR, 0 term states")
r2 = re.compile(R"\s*(\d+)\s+(\S)\s+(\d+)")
for line in textline[3:]:
m2 = r2.search(line)
st1 = int(m2.group(1))
st2 = int(m2.group(3))
sym = m2.group(2)
if sym == eps_symb:
global eps
eps = True
fa[st1][1].append( (sym, st2) )
global dfa
dfa = CheckDeterministicFA()
def CheckDeterministicFA():
for i in range(0,stn):
for ch in symbs:
t = LookupFunction(i, ch)
if t != None and len(t) >= 2:
return False
return True
def LookupFunction(state, symb):
ret = []
if state < 0 or state >= len(fa):
return None
elif len(fa[state][1]) == 0:
return None
else:
t = len(fa[state][1])
for i in range(0,t):
if fa[state][1][i][0] == symb:
ret.append(fa[state][1][i][1])
return ret
def GetStartState():
return ss
def GetTerminals():
return terms
def CheckIfTerminal(state):
if fa[state][0] == 2 or fa[state][0] == 3:
return True
return False
def CheckIfDeterministicFA():
return dfa
def ObtainFiniteAutomata():
FunctionOutput = "<pre>"
if eps:
FunctionOutput = FunctionOutput + "<b>NFA with epsilon</b>" + "\n"
elif dfa:
FunctionOutput = FunctionOutput + "<b>DFA</b>" + "\n"
else:
FunctionOutput = FunctionOutput + "<b>NFA</b>" + "\n"
FunctionOutput = FunctionOutput + "<b>States: </b>" + str(stn) + "\n" + "<b>Symbols: </b>" + str(symbs) + "\n\n"
for i in range(0,stn):
if i < 10:
FunctionOutput = FunctionOutput + " " + str(i)
else:
FunctionOutput = FunctionOutput + str(i)
if i == ss:
FunctionOutput = FunctionOutput + "s"
else:
FunctionOutput = FunctionOutput + " "
if CheckIfTerminal(i):
FunctionOutput = FunctionOutput + "t:"
else:
FunctionOutput = FunctionOutput + " : "
FunctionOutput = FunctionOutput + str(fa[i]) + "\n"
FunctionOutput = FunctionOutput + "</pre>"
return FunctionOutput