예제 #1
0
 def buildDFA(self, nfa):
     allstates = dict()
     eclose = dict()
     count = 1
     state1 = nfa.getEClose(nfa.startState)
     eclose[nfa.startState] = state1
     dfa = Automata(nfa.language)
     dfa.setstartState(count)
     states = [[state1, count]]
     allstates[count] = state1
     count += 1
     while len(states) != 0:
         [state, fromindex] = states.pop()
         for char in dfa.language:
             trstates = nfa.getTransitions(state, char)
             for s in list(trstates)[:]:
                 if s not in eclose:
                     eclose[s] = nfa.getEClose(s)
                 trstates = trstates.union(eclose[s])
             if len(trstates) != 0:
                 if trstates not in allstates.values():
                     states.append([trstates, count])
                     allstates[count] = trstates
                     toindex = count
                     count += 1
                 else:
                     toindex = [
                         k for k, v in allstates.iteritems()
                         if v == trstates
                     ][0]
                 dfa.addTransition(fromindex, toindex, char)
     for value, state in allstates.iteritems():
         if nfa.finalStates[0] in state:
             dfa.addFinalStates(value)
     self.dfa = dfa