-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeterminize.py
149 lines (117 loc) · 3.61 KB
/
determinize.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
#!/usr/bin/env python3
import sys
import collections
detAutomaton = {}
nonDetAutomaton = {}
acceptStates = set()
detAcceptStates = set()
alphabet = set()
foundState = True
nonDetWalks = False
def prettyPrint(dicto):
print()
for key in dicto:
print(str(key) + " : " + str(dicto[key]))
def prettyPrintOrdered(dicto, acceptStates):
print()
dicto = collections.OrderedDict(sorted(dicto.items()))
for key in dicto:
print(str(key[0]) + " " + str(dicto[key]) + " " + str(key[1]))
for state in acceptStates:
print(state)
def printIter(dicto, i):
print('\n----------------\n')
print('Iteration ' + str(i))
prettyPrint(dicto)
print()
#Read nondeterministic automaton ----
for line in sys.stdin:
if line[0] != '#':
words = line.strip().split(' ')
if len(words) != 1:
pair = (words[0], words[2])
alphabet.add(words[2])
if pair in nonDetAutomaton:
nonDetAutomaton[pair].add(words[1])
#used for detecting not needed single states
nonDetWalks = True
else:
nonDetAutomaton[pair] = set(words[1])
else:
acceptStates.add(line.strip())
detAcceptStates = set(acceptStates)
print('Input: ')
prettyPrint(nonDetAutomaton)
#if the input automaton is nondeterministic
if nonDetWalks:
#initialize values for 0
for symbol in alphabet:
if ('0', symbol) in nonDetAutomaton:
detAutomaton[('0', symbol)] = nonDetAutomaton[('0', symbol)]
#used because we cant iterate throught changing dictionary
detAutomatonCopy = dict(detAutomaton)
i = 0
while foundState == True:
detAutomaton = dict(detAutomatonCopy)
i = i + 1
printIter(detAutomaton, i)
foundState = False
for key in detAutomaton:
compareKey = key
#if its a set, convert it into frozenset (which is hashable)
if len(detAutomaton[key]) > 1:
compareKey = (frozenset(detAutomaton[key]),key[-1])
elif detAutomaton[key]:
compareKey = (next(iter(detAutomaton[key])), key[-1])
if detAutomaton[key] and compareKey not in detAutomaton:
print('Found new key : ' + str(detAutomaton[key]) + ". Adding to automaton...")
foundState = True
for symbol in alphabet:
valueAutoma = set()
for state in detAutomaton[key]:
if (state, symbol) in nonDetAutomaton:
valueAutoma.update(nonDetAutomaton[(state, symbol)])
detAutomatonCopy[compareKey[0], symbol] = valueAutoma
#Delete empty states
toDel = set()
for key in detAutomaton:
if not len(detAutomaton[key]):
toDel.add(key)
for state in toDel:
del detAutomaton[state]
i = i + 1
printIter(detAutomaton, i)
#Create map
keysMap = {}
#Map starting state
keysMap['0'] = '0'
i = 1
#Map lasting states
for key in detAutomaton:
if key[0] not in keysMap:
keysMap[key[0]] = str(i)
i = i + 1
print("--------\n\nMap:")
prettyPrint(keysMap)
print()
detAcceptStates = set()
#Get accepting states
for key in detAutomaton:
for state in frozenset(key[0]):
if state in acceptStates:
detAcceptStates.add(keysMap[key[0]])
detAutomatonCopy = detAutomaton
detAutomaton = {}
#Map values
for key in detAutomatonCopy:
#transfer value set into frozenset, if it is a set or into single element if it is a single element
if len(detAutomatonCopy[key]) > 1:
detAutomaton[(keysMap[key[0]], key[1])] = keysMap[frozenset(detAutomatonCopy[key])]
else:
for e in detAutomatonCopy[key]:
singleElement = e
detAutomaton[(keysMap[key[0]], key[1])] = keysMap[singleElement]
print("--------\n\nFinal deterministic automaton:")
prettyPrintOrdered(detAutomaton, detAcceptStates)
else:
print("\nYour automaton is already deterministic!")