Beispiel #1
0
 def star_construct(automata_1):
     [automata_1, m1] = automata_1.copy(2)
     state1 = 1
     state2 = m1
     star = Automata()
     star.set_start_state(state1)
     star.add_final_states(state2)
     star.add_transition(star.start_state, automata_1.start_state,
                         Automata.empty())
     star.add_transition(star.start_state, star.final_states[0],
                         Automata.empty())
     star.add_transition(automata_1.final_states[0], star.final_states[0],
                         Automata.empty())
     star.add_transition(automata_1.final_states[0], automata_1.start_state,
                         Automata.empty())
     star.add_transitions(automata_1.transitions)
     return star
Beispiel #2
0
    def construct_NFA(self):
        language = set()
        self.stack = []
        self.automata = []
        previous = Automata.empty()

        for char in self.regex:
            if char in self.alphabets:
                language.add(char)
                if previous != RegexToNFA.dot() and (
                        previous in self.alphabets or previous
                        in [RegexToNFA.closing_bracket(),
                            RegexToNFA.star()]):
                    self.add_operator_to_stack(RegexToNFA.dot())
                self.automata.append(
                    AutomataConstruction.basic_structure(char))
            elif char == RegexToNFA.opening_bracket():
                if previous != RegexToNFA.dot() and (
                        previous in self.alphabets or previous
                        in [RegexToNFA.closing_bracket(),
                            RegexToNFA.star()]):
                    self.add_operator_to_stack(RegexToNFA.dot())
                self.stack.append(char)
            elif char == RegexToNFA.closing_bracket():
                while (1):
                    if len(self.stack) == 0:
                        raise Exception("Empty stack!")
                    operator = self.stack.pop()
                    if operator == RegexToNFA.opening_bracket():
                        break
                    elif operator in self.operators:
                        self.construct_sNFA(operator)
            elif char == RegexToNFA.star():
                if previous in [
                        RegexToNFA.star(),
                        RegexToNFA.plus(),
                        RegexToNFA.opening_bracket()
                ]:
                    raise Exception("No matching!")
                self.construct_sNFA(char)
            elif char in self.operators:
                if previous in self.operators or previous == RegexToNFA.opening_bracket(
                ):
                    raise Exception("No matching!")
                else:
                    self.add_operator_to_stack(char)
            else:
                raise Exception("No matching!")

            previous = char

        while (len(self.stack) > 0):
            operator = self.stack.pop()
            self.construct_sNFA(operator)

        self.nfa = self.automata.pop()
        self.nfa.alphabets = language
Beispiel #3
0
 def plus_construct(automata_1, automata_2):
     [automata_1, m1] = automata_1.copy(2)
     [automata_2, m2] = automata_2.copy(m1)
     state1 = 1
     state2 = m2
     plus = Automata()
     plus.set_start_state(state1)
     plus.add_final_states(state2)
     plus.add_transition(plus.start_state, automata_1.start_state,
                         Automata.empty())
     plus.add_transition(plus.start_state, automata_2.start_state,
                         Automata.empty())
     plus.add_transition(automata_1.final_states[0], plus.final_states[0],
                         Automata.empty())
     plus.add_transition(automata_2.final_states[0], plus.final_states[0],
                         Automata.empty())
     plus.add_transitions(automata_1.transitions)
     plus.add_transitions(automata_2.transitions)
     return plus
Beispiel #4
0
 def dot_construct(automata_1, automata_2):
     [automata_1, m1] = automata_1.copy(1)
     [automata_2, m2] = automata_2.copy(m1)
     state1 = 1
     state2 = m2 - 1
     dot = Automata()
     dot.set_start_state(state1)
     dot.add_final_states(state2)
     dot.add_transition(automata_1.final_states[0], automata_2.start_state,
                        Automata.empty())
     dot.add_transitions(automata_1.transitions)
     dot.add_transitions(automata_2.transitions)
     return dot
    def remove_empty(self, nfa):
        #generate states
        new_states = []
        for state in nfa.states:
            state_group = nfa.get_empty_by_state(state)
            inclusion = False
            for new_state in new_states:
                if state_group & new_state[1] == state_group:
                    inclusion = True
                    break
            if not inclusion:
                new_states.append((state, state_group))

        #generate transitions
        transitions = []
        for state in new_states:
            original_state = state[0]
            new_state = state[1]
            for state_ in new_states:
                if state_ != state:
                    for s in state_[1]:
                        if s in nfa.transitions and original_state in nfa.transitions[
                                s]:
                            alphabet = deepcopy(
                                nfa.transitions[s][original_state])
                            if len(alphabet) > 0:
                                alphabet = alphabet.pop()
                                if alphabet != Automata.empty():
                                    transitions.append(
                                        (state_[1], alphabet, new_state))

        #generate loop transitions
        for state in new_states:
            new_state = state[1]
            for state_ in new_state:
                for s in new_state:
                    if state_ in nfa.transitions and s in nfa.transitions[
                            state_]:

                        alphabet = deepcopy(nfa.transitions[state_][s])
                        if len(alphabet) > 0:
                            alphabet = alphabet.pop()
                            if alphabet != Automata.empty():
                                transitions.append(
                                    (new_state, alphabet, new_state))

        #map states
        map_states = dict()
        for i, state in enumerate(new_states):
            map_states[",".join(map(str, list(state[1])))] = i

        #map transitions
        transitions_ = []
        for transition in transitions:
            transitions_.append(
                (map_states[",".join(map(str,
                                         list(transition[0])))], transition[1],
                 map_states[",".join(map(str, list(transition[2])))]))
        transitions = transitions_

        new_nfa = Automata(alphabets=nfa.alphabets)
        #set start state
        for state in new_states:
            new_state = state[1]
            if nfa.start_state in new_state:
                new_nfa.set_start_state(map_states[",".join(
                    map(str, list(new_state)))])
                break
        #set final states:
        for state in new_states:
            new_state = state[1]
            for final_state in nfa.final_states:
                if final_state in new_state:
                    new_nfa.add_final_states(map_states[",".join(
                        map(str, list(new_state)))])

        #add transitions
        for transition in transitions:
            state1 = transition[0]
            alphabet = transition[1]
            state2 = transition[2]
            new_nfa.add_transition(state1, state2, alphabet)

        return new_nfa