示例#1
0
    def optional(self):
        newStart = State()
        newAccept = State()
        start = deepcopy(self.start)
        accepts = deepcopy(self.accepts)

        newStart.addTransition(Transition(epsilon, start))
        newStart.addTransition(Transition(epsilon, newAccept))

        for a in accepts:
            a.setToken(-1)
            a.addTransition(Transition(epsilon, newAccept))

        newStates = set([newStart, start, newAccept])

        for a in accepts:
            newStates.add(a)

        for s in self.states:
            if (s.equals(start) == False):
                for a in self.accepts:
                    if (s.equals(a) == False):
                        newStates.add(s)

        #Free memory
        del start
        del accepts
        return NFA(newStates, NFA.addNewAlphabet(newStates), newStart,
                   set([newAccept]))
示例#2
0
 def createBasic(symbol):
     e1 = State()
     e2 = State()
     t1 = Transition(symbol, e2)
     e1.addTransition(t1)
     states = set([e1, e2])
     alphabet = set([symbol])
     return NFA(states, alphabet, e1, set([e2]))
示例#3
0
    def join(self, nfaB):
        #Create a deepcopy of start states from both NFAs
        startA = deepcopy(self.start)
        startB = deepcopy(nfaB.getStart())
        #Create a deepcopy of accept states
        acceptsA = deepcopy(self.accepts)
        acceptsB = deepcopy(nfaB.getAccepts())
        #Create new start, accept state and new states set
        newStart = State()
        newAccept = State()

        #Add epsilon transitions to new start state
        newStart.addTransition(Transition(epsilon, startA))
        newStart.addTransition(Transition(epsilon, startB))
        #Add epsilon transition to new accept state
        for a in acceptsA:
            a.setToken(-1)
            a.addTransition(Transition(epsilon, newAccept))

        for b in acceptsB:
            b.setToken(-1)
            b.addTransition(Transition(epsilon, newAccept))

        #Add updated "accept" states (no longer accepted), new start and new accept
        newStates = set([newStart, newAccept])

        for a in acceptsA:
            newStates.add(a)

        for b in acceptsB:
            newStates.add(b)

        #Add the remaining states
        for e1 in self.states:
            for a in self.accepts:
                if (e1.equals(a) == False):
                    newStates.add(e1)

        for e2 in nfaB.getStates():
            for b in nfaB.getAccepts():
                if (e2.equals(b) == False):
                    newStates.add(e2)

        #Free memory
        del startA
        del startB
        del acceptsA
        del acceptsB
        return NFA(newStates, NFA.addNewAlphabet(newStates), newStart,
                   set([newAccept]))
示例#4
0
    def specialJoin(nfas):
        newStart = State()
        newStates = set([])
        accepts = set([])

        for nfa in nfas:
            start = deepcopy(nfa.getStart())
            newStart.addTransition(Transition(epsilon, start))
            for e in nfa.getAccepts():
                accepts.add(e)
            newStates = newStates.union(nfa.getStates())
            del start

        newStates.add(newStart)
        return NFA(newStates, NFA.addNewAlphabet(newStates), newStart, accepts)
示例#5
0
    def positiveClosure(self):
        #Create new start state
        newStart = State()
        #Create new accept state
        newAccept = State()

        #Create a deepcopy of actual start and accept state
        start = deepcopy(self.start)
        accepts = deepcopy(self.accepts)

        #Add epsilon transitions
        newStart.addTransition(Transition(epsilon, start))

        for a in accepts:
            a.setToken(-1)
            a.addTransition(Transition(epsilon, newAccept))
            a.addTransition(Transition(epsilon, start))

        #Add updated states on new set
        newStates = set([newStart, start, newAccept])

        for a in accepts:
            newStates.add(a)

        #Add remaining states to new set
        for s in self.states:
            if (s.equals(start) == False):
                for a in self.accepts:
                    if (s.equals(a) == False):
                        newStates.add(s)

        #Free memory
        del start
        del accepts
        return NFA(newStates, NFA.addNewAlphabet(newStates), newStart,
                   set([newAccept]))