-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq2.py
114 lines (98 loc) · 3.42 KB
/
q2.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
import sys
import json
import itertools
unitStateEpsilonClosure = {}
visited = {}
nfaEpsilonClosureDict = {}
def computeNfaEpsilonClosureDict(nfaState,nfaTransitionMatrix):
if visited[nfaState]:
return nfaEpsilonClosureDict[nfaState]
visited[nfaState]=True
nfaEpsilonClosureDict[nfaState] = [nfaState]
for arc in nfaTransitionMatrix:
[ss, l, fs] = arc
if (l != '$') or (ss != nfaState):
continue
children = computeNfaEpsilonClosureDict(fs,nfaTransitionMatrix)
for child in children:
if child not in nfaEpsilonClosureDict[nfaState]:
nfaEpsilonClosureDict[nfaState].append(child)
return nfaEpsilonClosureDict[nfaState]
def objectFA(s,l,tm,ss,fs):
return {
'states': s,
'letters': l,
'transition_matrix': tm,
'start_states': ss,
'final_states':fs
}
def epsilonClosure(stateList):
returnState = []
for st in stateList:
currentEpsilon = unitStateEpsilonClosure[st]
for cst in currentEpsilon:
if cst not in returnState:
returnState.append(cst)
return returnState
def allStatesCombination(nfaSTATES):
return_state = []
for i in range(len(nfaSTATES)+1):
combination = [ list(tup) for tup in list(itertools.combinations(nfaSTATES,i))]
return_state += combination
return return_state
def getFinalState(nfaFinalStates,dfaStates):
returnState = []
for st in dfaStates:
for fst in nfaFinalStates:
if (st not in returnState)and(fst in st):
returnState.append(st)
break
return returnState
def getTransitionedState(nfaTransitionMatrix, state, letter):
ts = []
for st in state:
for arc in nfaTransitionMatrix:
[ss, l, fs] = arc
if (l != letter) or (ss != st):
continue
if fs not in ts:
ts.append(fs)
return ts
def formTransitionMatrix(nfaTransitionMatrix, dfaStates, dfaLetters):
tm = []
for st in dfaStates:
for l in dfaLetters:
transitionedState = getTransitionedState(nfaTransitionMatrix, st, l)
tm.append([st,l,epsilonClosure(transitionedState)])
return tm
def convertNFAToDFA(NFA):
DFA_L = NFA['letters'][:]
DFA_S = allStatesCombination(NFA['states'])
DFA_FS = getFinalState(NFA['final_states'],DFA_S)
for st in NFA['states']:
for st1 in NFA['states']:
visited[st1] = False
nfaEpsilonClosureDict[st1]=[]
unitStateEpsilonClosure[st] = computeNfaEpsilonClosureDict(st,NFA['transition_matrix'])
DFA_SS = [ epsilonClosure([sst]) for sst in NFA['start_states'] ]
DFA_TM = formTransitionMatrix(NFA['transition_matrix'], DFA_S, DFA_L)
return objectFA(DFA_S,DFA_L,DFA_TM,DFA_SS,DFA_FS)
if __name__=="__main__":
if len(sys.argv) != 3:
print("Unexceptable Format!")
quit()
try:
input_file = open(sys.argv[1])
rawNFA = json.load(input_file)
s = rawNFA['states']
l = rawNFA['letters']
tm = rawNFA['transition_function']
ss = rawNFA['start_states']
fs = rawNFA['final_states']
myNFA = objectFA(s, l, tm, ss, fs)
except:
print("Error accessing `input_file`")
quit()
finalDFA = convertNFAToDFA(myNFA)
with open(sys.argv[2], "w+") as outfile:
json.dump(finalDFA, outfile, indent=4)