Exemplo n.º 1
0
    def test_simple_case(self):
        # construct nfa
        pattern = "abc|bc|ad"  # "a(b|c)*" | ""
        lexer = Lexer(pattern)
        lexer.analyse()

        parser = Parser(lexer)
        postfix_expr = parser.parse()

        nfa_stack = TokenToNFA(postfix_expr).translate()
        if len(nfa_stack) != 1:
            raise PatternSyntaxError
        nfa = nfa_stack[0]

        # nfa.plot()

        online = Online(nfa)
        dfa = online.construct()

        dfa.plot()

        simplified_dfa = dfa.simplify()

        simplified_dfa.plot("simplified_dfa")

        self.assertTrue(0)
Exemplo n.º 2
0
    def test_simple(self):
        pattern = "abc"
        print("Pattern: {}".format(pattern))

        lexer = Lexer(pattern)
        lexer.analyse()
        parser = Parser(lexer)
        postfix_expr = parser.parse()

        for i in postfix_expr:
            print(i)

        self.assertEqual(1, 2)
Exemplo n.º 3
0
    def test_complicate_case(self):
        pattern = r"(((d|e)?)|(a+))+"
        print("Pattern: {}".format(pattern))

        lexer = Lexer(pattern)
        lexer.analyse()
        parser = Parser(lexer)
        postfix_expr = parser.parse()

        for i in postfix_expr:
            print(i)

        self.assertEqual(1, 2)
Exemplo n.º 4
0
    def test_middle_case(self):
        pattern = r"(ab|a)(bc|c)"
        print("Pattern: {}".format(pattern))

        lexer = Lexer(pattern)
        lexer.analyse()
        parser = Parser(lexer)
        postfix_expr = parser.parse()

        for i in postfix_expr:
            print(i)

        self.assertEqual(1, 2)
Exemplo n.º 5
0
    def compile(self, pattern, use_dfa=False):
        lexer = Lexer(pattern)
        lexer.analyse()

        parser = Parser(lexer)
        postfix_expr = parser.parse()

        nfa_stack = TokenToNFA(postfix_expr).translate()
        if len(nfa_stack) != 1:
            raise PatternSyntaxError

        automaton = nfa_stack[0]

        if use_dfa:
            nfa2dfa = NFA2DFA(automaton)
            automaton = nfa2dfa.convert()

        return automaton
Exemplo n.º 6
0
    def test_simple_case(self):
        # construct nfa

        # "a(b|c)*" | "abc|bc|ad" | "r0|r1|r2|r3|r4|r5|r6|r7|r8|r9" | "(a|b)*abb"
        pattern = "a(b|c*)"
        lexer = Lexer(pattern)
        lexer.analyse()

        parser = Parser(lexer)
        postfix_expr = parser.parse()

        nfa_stack = TokenToNFA(postfix_expr).translate()
        if len(nfa_stack) != 1:
            raise PatternSyntaxError
        nfa = nfa_stack[0]

        # nfa.plot()

        online = Online(nfa)
        dfa = online.construct()

        # dfa.plot()

        simplified_dfa = dfa.simplify()

        simplified_dfa.plot("simplified_dfa")

        brzozowski = Brzozowski(simplified_dfa)

        reversed_nfa = brzozowski.reverse()
        # reversed_nfa.plot("reversed_nfa")

        postfix_minimal_dfa = brzozowski._construct_postfix_minimal_dfa()
        # postfix_minimal_dfa.plot("postfix_minimal_dfa")

        minimal_dfa = brzozowski.construct()

        minimal_dfa.simplify().plot("minimal_dfa")