Exemple #1
0
    def test_init(self):
        with self.assertRaises(TypeError):
            finite = fa.FiniteAutomaton({self.st1.name:self.st1, self.st2.name:self.st2},
                                        {'0', '1'}, self.st1)
        with self.assertRaises(TypeError):
            finite = fa.FiniteAutomaton({self.st1.name:self.st1, self.st2.name:self.st2},
                                        {'0', '1'}, self.st1)

        with self.assertRaises(TypeError):
            finite = fa.FiniteAutomaton.factory("""s0,s1
0,1
s1
s0
s0,0->s1
s1,1->s1""", generator.StandardFormatGenerator())

        with self.assertRaises(TypeError):
            finite = nfa.EpsilonNFA({0:0,self.st2.name:self.st2}, {0,1}, self.st1)
        with self.assertRaises(AttributeError):
            finite = nfa.EpsilonNFA({self.st1.name:self.st1,self.st2.name:self.st2},
                                    {0,1}, 4)
        with self.assertRaises(TypeError):
            finite = nfa.EpsilonNFA({self.st1:self.st1,self.st2.name:self.st2},
                                    {0,1}, self.st1)
        with self.assertRaises(TypeError):
            finite = nfa.EpsilonNFA({self.st1.name:self.st1.name,self.st2.name:self.st2},
                                    {0,1}, self.st1)
Exemple #2
0
    def _assemble(self)->nfa.EpsilonNFA:
        if isinstance(self._item, str):
            item_enfa = nfa.EpsilonNFA.factory(
                """qm0,qm1
{0}
qm1
qm0
qm0,{0}->qm1
qm0,$->qm1""".format(*helper.escape_string(self._item)),
            generator.StandardFormatGenerator())
        elif isinstance(self._item, Operator):
            start_enfa = nfa.EpsilonNFA.factory(
                """qm\n\nqm\nqm\n""",
            generator.StandardFormatGenerator())
            end_enfa = start_enfa.deepcopy()
            item_enfa = start_enfa*end_enfa + self._item._assemble()
        return item_enfa
Exemple #3
0
    def setUp(self):
        self.test = nfa.EpsilonNFA.factory(
            """s0,s1
0,1
s1
s0
s0,0->s1
s1,1->s0""", generator.StandardFormatGenerator())
Exemple #4
0
    def load(file):
        """
        Loads a regex.

        :param file: regex filepath
        :return: regex object
        """
        with open(dirname + '/compiled_regexes/' + file + '.regex', 'r') as file:
            data = json.load(file)
            regex = RegEx(data['text'], data['name'], False)
            regex.automaton = dfa.DFA.factory(data['automaton'], generator.StandardFormatGenerator())
        return regex
Exemple #5
0
    def _assemble(self)->nfa.EpsilonNFA:
        if isinstance(self._item, str):
            item_enfa = nfa.EpsilonNFA.factory(
                """ks0,ks1
{0}
ks1
ks0
ks0,{0}->ks1""".format(*helper.escape_string(self._item)),
            generator.StandardFormatGenerator())
        elif isinstance(self._item, Operator):
            item_enfa = self._item._assemble()
        return item_enfa.kleene_operator()
Exemple #6
0
    def _assemble(self)->nfa.EpsilonNFA:
        if isinstance(self._item, str):
            enfa = nfa.EpsilonNFA.factory(
                """s0,s1
{0}
s1
s0
s0,{0}->s1""".format(*helper.escape_string(self._item)),
            generator.StandardFormatGenerator())
        elif isinstance(self._item, Operator):
            enfa = self._item._assemble()
        return enfa
Exemple #7
0
    def setUp(self):
        self.st1 = state.State('test0', 0)
        self.st2 = state.State('test1', 1)
        self.st1.add_function(self.st2, '1')
        self.st2.add_function(self.st1, '0')
        self.test = nfa.NFA.factory("""s0,s1,s2
0,1
s1,s2
s0
s0,0->s1
s1,1->s2
s2,0->s1""", generator.StandardFormatGenerator())
Exemple #8
0
    def _assemble(self)->nfa.EpsilonNFA:
        if isinstance(self._item, str):
            # todo: don't call it through this.
            enfa = nfa.EpsilonNFA.factory(
                """kp0,kp1
{0}
kp1
kp0
kp0,{0}->kp1""".format(*helper.escape_string(self._item)),
            generator.StandardFormatGenerator())
        elif isinstance(self._item, Operator):
            enfa = self._item._assemble()
        # else is not needed because OperatorInputTypeError would already have been raised
        # if item is not a string or an Operator.
        return enfa.deepcopy() * enfa.kleene_operator()
Exemple #9
0
    def _assemble(self)->nfa.EpsilonNFA:
        result = ''
        inputs = ''
        for char in self.all_characters:
            result += 'c0,{}->c1\n'.format(*helper.escape_string(char))
            inputs += char + ','
        result = result[:-1]
        inputs = inputs[:-1]

        enfa = nfa.EpsilonNFA.factory(
            """c0,c1
{}
c1
c0
{}""".format(inputs, result),
        generator.StandardFormatGenerator())
        return enfa
Exemple #10
0
    def _assemble(self)->nfa.EpsilonNFA:
        enfas = []
        for item in self._items:
            if isinstance(item, str):
                enfas.append(nfa.EpsilonNFA.factory(
                    """c0,c1
{0}
c1
c0
c0,{0}->c1""".format(*helper.escape_string(item)),
                    generator.StandardFormatGenerator()))
            elif isinstance(item, Operator):
                enfas.append(item._assemble())

        result = enfas[0]
        for i in range(1, len(enfas)):
            result = result * enfas[i]
        return result
Exemple #11
0
    def kleene_operator(self):

        import form.generators as lex

        # ensuring state names are not identical when doing multiple additions.
        ending = 'end_'
        for state in self.accepted_states:
            ending += str(state.name)

        # ensuring state names are not identical when doing multiple additions.
        starting = 'start_' + str(self.start_state.name)

        # we need a clean epsilon NFA instance. See NFA union.
        new_e_nfa = self.factory("""{0},{1}

{1}
{0}
""".format(starting, ending), lex.StandardFormatGenerator()
                                 )  # starting and ending might need escaping

        starting = new_e_nfa.start_state
        ending = list(new_e_nfa.accepted_states)[0]
        starting.add_function(ending, self._epsilon)

        copied_self = self.deepcopy()

        new_e_nfa.states.update(copied_self.states)
        new_e_nfa.inputs |= copied_self.inputs

        starting.add_function(new_e_nfa.states[self.start_state.name],
                              self._epsilon)
        ending.add_function(new_e_nfa.states[self.start_state.name],
                            self._epsilon)

        for state in new_e_nfa.accepted_states:
            if state != ending:
                state.add_function(ending, self._epsilon)

        for state in new_e_nfa.accepted_states:
            if state != ending:
                state.value = 0

        return new_e_nfa
Exemple #12
0
    def __add__(self, other):
        """
        Allows for epsilon NFA addition.

        :param EpsilonNFA other: other epsilon NFA
        :return EpsilonNFA: resulting NFA
        """
        import form.generators as lex

        #ensuring state names are not identical when doing multiple additions.
        ending = 'end_'
        for state in self.accepted_states:
            ending += str(state.name)
        for state in other.accepted_states:
            ending += str(state.name)

        # ensuring state names are not identical when doing multiple additions.
        starting = 'start_' + str(self.start_state.name) + str(
            other.start_state.name)

        #we need a clean epsilon NFA instance. See NFA union.
        new_e_nfa = self.factory(
            """{0},{1}

{1}
{0}
""".format(starting, ending), lex.StandardFormatGenerator())

        starting = new_e_nfa.start_state
        ending = list(new_e_nfa.accepted_states)[0]

        copied_self = self.deepcopy()
        copied_other = other.deepcopy()
        states = dict()

        for name in list(copied_self.states):
            new_name = 'a_0' + name.name
            state = copied_self.states[name]
            state.name = st.StateName(new_name)
            states[state.name] = state
        for name in list(copied_other.states):
            new_name = 'a_1' + name.name
            state = copied_other.states[name]
            state.name = st.StateName(new_name)
            states[state.name] = state

        # new_e_nfa.states.update(copied_self.states)
        # new_e_nfa.states.update(copied_other.states)
        new_e_nfa.states.update(states)
        new_e_nfa.inputs |= copied_self.inputs | copied_other.inputs

        starting.add_function(new_e_nfa.states[copied_self.start_state.name],
                              self._epsilon)
        starting.add_function(new_e_nfa.states[copied_other.start_state.name],
                              self._epsilon)

        for state in new_e_nfa.accepted_states:
            if state != ending:
                state.add_function(ending, self._epsilon)

        for state in new_e_nfa.accepted_states:
            if state != ending:
                state.value = 0

        return new_e_nfa