-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
276 lines (239 loc) · 7.38 KB
/
main.py
File metadata and controls
276 lines (239 loc) · 7.38 KB
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
import format_print as fp
import manual_input
''' State Structure
current State # : ( accepting , { symbol : next State # } )
'''
def theorem44(F, dfa, alphabet) -> bool:
'''
1. Mark the start state of A
2. Repeat until no new states get marked
3. Mark any state that has a transition coming into it from
any state that is already marked
4. If no accept state is marked, ACCEPT; otherwise REJECT
#### parameters
dfa, alphabet
#### returns
True or False (Accept/Reject)
'''
print('\n>=== Theorem 4.4 ===<')
marked = {}
start_state = list(dfa.items())[0]
marked.update({start_state[0]: start_state[1][0]}) # mark start state
mark_search(start_state[0], marked, dfa, alphabet)
R = set(marked.keys())
SF = set(F)
F_intersect_R = set(SF).intersection(R)
print('Reachable states ', fp.unicode['R'], ' = ', R, sep='')
print('Accepting (final) states ', fp.unicode['F'], ' = ', \
fp.unicode['empty'] if len(SF) == 0 else SF, sep='')
print(fp.unicode['F'], fp.unicode['cap'], fp.unicode['R'], ' = ', \
fp.unicode['empty'] if len(F_intersect_R)==0 else F_intersect_R, sep='')
return len(F_intersect_R) == 0
def mark_search(state, marked, dfa, alphabet) -> None:
'''
#### parameters
- state: current state
- marked: currently marked states
- dfa
- alphabet
'''
for c in alphabet:
trans_state = dfa[state][1][c]
accepting = dfa[trans_state][0]
if not trans_state in marked:
marked.update({trans_state: accepting})
mark_search(trans_state, marked, dfa, alphabet)
def standardize(name: str, M, alphabet, dfa):
'''
alters M to be proper form and prints that form
if desired
#### parameters
- name: Name of the DFA
- M:
- alphabet
- dfa
'''
M.update({'Q':list(dfa.keys())}) # states
M.update({'Sigma':list(alphabet)}) # alphabet
M.update({'delta':dfa}) # transitions
M.update({'q0':list(dfa.items())[0][0]}) # initial state
F = []
for state, info in dfa.items():
if(info[0]):
F.append(state)
M.update({'F':F}) # accepting states
print( 'DFA', name, '= (' )
fp.dfa_item(fp.unicode['Q'], M['Q'])
fp.dfa_item(fp.unicode['Sigma'], M['Sigma'])
fp.transitions(fp.unicode['delta'], M['delta'], alphabet)
print('\t', fp.unicode['q0'], '= ', M['q0'], sep='')
fp.dfa_item(fp.unicode['F'], M['F'])
print(')')
return M['Q'], M['Sigma'], M['delta'], M['q0'], M['F']
def xor(x, y) -> bool:
return bool((x and not y) or (not x and y))
def read_file(file, eq = False):
dfaA = {}
alphabet = ''
lines = file.read().splitlines()
num_states = int(lines[0])
alphabet_raw = lines[1]
alphabet = ''.join(dict.fromkeys(alphabet_raw))
for i in range(0, num_states):
line = lines[2+i].split(' ')
state = 'S' + str(i)
transitions = {}
accepting = True if line[0] == 'y' else False
j = 0
for c in alphabet:
val = 'S' + line[j+1]
transitions.update({c:val})
j+=1
info = (accepting, transitions)
dfaA.update({state: info})
print(eq)
if eq:
dfaB = {}
start = num_states+2
print(lines[start])
num_states = int(lines[start])
alphabet_raw = lines[start+1]
alphabetB = ''.join(dict.fromkeys(alphabet_raw))
for i in range(start, num_states+start):
line = lines[2+i].split(' ')
state = 'S' + str(i)
transitions = {}
accepting = True if line[0] == 'y' else False
j = 0
for c in alphabetB:
val = 'S' + line[j+1]
transitions.update({c:val})
j+=1
info = (accepting, transitions)
dfaB.update({state: info})
alphabet = ''.join(dict.fromkeys(alphabetB))
return dfaA, dfaB, alphabet
else:
return dfaA, alphabet
def symmetric_difference(QA, dA, q0A, FA, QB, dB, q0B, FB, E):
'''
Symmetric Difference
The Symmetric difference DFA C of DFAs A, B:
L(C) =
L(A) intersect L'(B)
union
L'(A) intersect L(B)
The complement of a DFA is acheived by taking the complement of
the set of final states F.
Thus the DFA C can be constructed as follows:
Q = Q_A X Q_B
Sigma = Sigma_A union Sigma_B
delta = <delta_A(q_A, a), delta_B(q_B, a)>
q0 = <q0_A, q0_B>
F = <q_A, q_B>
s.t. q_A in F_A XOR q_B in F_B
'''
dfa = {}
for qa in QA:
for qb in QB:
state = qa + qb
transitions = {}
qaFA = qa in FA
qbFB = qb in FB
accepting = xor(qaFA, qbFB)
for c in E:
trans_A = dA[qa][1][c]
trans_B = dB[qb][1][c]
trans = trans_A + trans_B
transitions.update({c:trans})
info = (accepting, transitions)
dfa.update({state: info})
M = {}
QC, EC, dC, q0C, FC = standardize('C', M, E, dfa)
return dfa, FC
def test_emptiness(file = None) -> bool:
dfa = {}
alphabet = ''
choice = None
M= {}
if file: choice = '2'
while choice != '2':
choice = input('Import DFA (0) or Manually Input DFA (1)? ')
if(choice == '0' or choice == '1'):
break
else:
print('Invalid input (must be 0 or 1)')
if(choice == '0'):
try: file = open(input('Filename: '))
except FileNotFoundError:
print('The file specified does not exist')
quit()
dfa, alphabet = read_file(file)
if(choice == '1'):
dfa, alphabet = manual_input.dfa()
if(choice == '2'):
try: file = open(file)
except FileNotFoundError:
print('The file specified does not exist')
quit()
dfa, alphabet = read_file(file)
Q, E, d, q, F = standardize('M', M, alphabet, dfa)
T_accept = theorem44(F, dfa, alphabet)
print('ACCEPTED' if T_accept else 'REJECTED')
print('\n>=== Result ===<')
print('DFAs are', '' if T_accept else 'not', 'equivalent')
return T_accept
def test_equivalence(file = None) -> bool:
dfa_a = {}
dfa_b = {}
alphabet = ''
choice = None
M_a = {}
M_b = {}
if file: choice = '2'
while choice != '2':
choice = input('Import DFA (0) or Manually Input DFA (1)? ')
if(choice == '0' or choice == '1'):
break
else:
print('Invalid input (must be 0 or 1)')
if(choice == '0'):
try: file = open(input('Filename: '))
except FileNotFoundError:
print('The file specified does not exist')
quit()
dfa_a, dfa_b, alphabet = read_file(file, True)
if(choice == '1'):
print('DFA A')
dfa_a, alphabet_a = manual_input.dfa()
print('DFA B')
dfa_b, alphabet_b = manual_input.dfa()
alphabet = ''.join(dict.fromkeys((alphabet_a + alphabet_b)))
if(choice == '2'):
try: file = open(file)
except FileNotFoundError:
print('The file specified does not exist')
quit()
dfa_a, dfa_b, alphabet = read_file(file, True)
Qa, Ea, da, qa, Fa = standardize('A', M_a, alphabet, dfa_a)
Qb, Eb, db, qb, Fb = standardize('B', M_b, alphabet, dfa_b)
print('\n>=== Constructed DFA C ===<')
dfa_s, F = symmetric_difference(Qa, da, qa, Fa, Qb, db, qb, Fb, alphabet)
T_accept = theorem44(F, dfa_s, alphabet)
print('ACCEPTED' if T_accept else 'REJECTED')
print('\n>=== Result ===<')
print('DFA is', '' if T_accept else 'not', 'empty')
return T_accept
''' Main
'''
if __name__ == '__main__':
while True:
choice = input('Test Emptiness (0) or Test Equivalence (1)? ')
if(choice == '0' or choice == '1'):
break
else:
print('Invalid input (must be 0 or 1)')
if choice == '0':
test_emptiness()
else:
test_equivalence()