-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnfa-to-dfa.py
64 lines (56 loc) · 2.38 KB
/
nfa-to-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
from nfa import *
from dfa import *
def make_dfa(nfa):
initial_state = nfa.get_initial_state()
number_of_input_symbols = nfa.get_number_of_input_symbols()
states = [[initial_state]]
state_id = 0
state_table = {str([initial_state]): state_id}
state_id += 1
transition_table = {}
final_states = []
print('checking initial state', initial_state)
input_dict = {}
state_checker = [[initial_state]]
while states:
for state in states:
input_dict = {}
print('checking state ' + str(state) + ' id ' + str(state_table[str(state)]))
for i in range(number_of_input_symbols):
new_state = []
for s in state:
new_state += nfa.transition_table[s][i]
if None in new_state:
new_state.remove(None)
new_state = list(set(new_state))
print('new state', new_state)
print('state list', state_checker)
if new_state not in state_checker:
print('new state doesnt already exist')
states.append(new_state)
state_checker.append(new_state)
state_table[str(new_state)] = state_id
print('new state list', state_checker)
for s in new_state:
if s in nfa.final_states:
final_states.append(state_id)
break
state_id += 1
input_dict[i] = state_table[str(new_state)]
print('d(q' + str(state_table[str(state)]) + ',' + str(i) + ') = q' + str(state_table[str(new_state)]))
print()
transition_table[state_table[str(state)]] = input_dict
states.remove(state)
print('\n\n')
return DFA(len(state_table), number_of_input_symbols, transition_table, initial_state, len(final_states),
final_states)
if __name__ == '__main__':
no_of_states = int(input('enter number of states'))
no_of_symbols = int(input('enter number of symbols'))
nfa = NFA(no_of_states, no_of_symbols)
nfa.print_state_symbol_list()
nfa.make_transition_table()
nfa.set_initial_final_states()
print(nfa)
dfa = make_dfa(nfa)
print(dfa)