Esempio n. 1
0
    def test_nfa_design(self):
        rulebook = NFARulebook(
            [
                FARule(1, "a", 1),
                FARule(1, "b", 1),
                FARule(1, "b", 2),
                FARule(2, "a", 3),
                FARule(2, "b", 3),
                FARule(3, "a", 4),
                FARule(3, "b", 4),
            ]
        )  # yapf: disable
        nfa = NFADesign(1, [4], rulebook)

        self.assertTrue(nfa.accepts("bab"))
        self.assertTrue(nfa.accepts("bbbbb"))
        self.assertFalse(nfa.accepts("bbabb"))

        rulebook = NFARulebook(
            [
                FARule(1, "a", 1),
                FARule(1, "a", 2),
                FARule(1, None, 2),
                FARule(2, "b", 3),
                FARule(3, "b", 1),
                FARule(3, None, 2),
            ]
        )  # yapf: disable
        self.assertEqual(rulebook.alphabet, set(["a", "b"]))

        nfa_design = NFADesign(1, [3], rulebook)
        self.assertEqual(nfa_design.to_nfa.current_states, set([1, 2]))
        self.assertEqual(nfa_design.to_nfa_from([3]).current_states, set([2, 3]))

        nfa = nfa_design.to_nfa_from([2, 3])
        nfa.read_character("b")
        self.assertEqual(nfa.current_states, set([1, 2, 3]))

        simulation = NFASimulation(nfa_design)
        self.assertEqual(simulation.next_state([1, 2], "a"), set([1, 2]))
        self.assertEqual(simulation.next_state([1, 2], "b"), set([3, 2]))
        self.assertEqual(simulation.next_state([3, 2], "b"), set([1, 3, 2]))
        self.assertEqual(simulation.next_state([1, 3, 2], "b"), set([1, 3, 2]))
        self.assertEqual(simulation.next_state([1, 3, 2], "a"), set([1, 2]))

        dfa_design = simulation.to_dfa_design
        self.assertFalse(dfa_design.accepts("aaa"))
        self.assertTrue(dfa_design.accepts("aab"))
        self.assertTrue(dfa_design.accepts("bbbabb"))
Esempio n. 2
0
def test_nfa_design():
    rulebook = NFARulebook([
        FARule(1, "a", 1),
        FARule(1, "b", 1),
        FARule(1, "b", 2),
        FARule(2, "a", 3),
        FARule(2, "b", 3),
        FARule(3, "a", 4),
        FARule(3, "b", 4),
    ])
    nfa = NFADesign(1, [4], rulebook)

    assert nfa.accepts("bab")
    assert nfa.accepts("bbbbb")
    assert not (nfa.accepts("bbabb"))

    rulebook = NFARulebook([
        FARule(1, "a", 1),
        FARule(1, "a", 2),
        FARule(1, None, 2),
        FARule(2, "b", 3),
        FARule(3, "b", 1),
        FARule(3, None, 2),
    ])
    assert rulebook.alphabet == set(["a", "b"])

    nfa_design = NFADesign(1, [3], rulebook)
    assert nfa_design.to_nfa.current_states == set([1, 2])
    assert nfa_design.to_nfa_from([3]).current_states == set([2, 3])

    nfa = nfa_design.to_nfa_from([2, 3])
    nfa.read_character("b")
    assert nfa.current_states == set([1, 2, 3])

    simulation = NFASimulation(nfa_design)
    assert simulation.next_state([1, 2], "a") == set([1, 2])
    assert simulation.next_state([1, 2], "b") == set([3, 2])
    assert simulation.next_state([3, 2], "b") == set([1, 3, 2])
    assert simulation.next_state([1, 3, 2], "b") == set([1, 3, 2])
    assert simulation.next_state([1, 3, 2], "a") == set([1, 2])

    dfa_design = simulation.to_dfa_design
    assert not (dfa_design.accepts("aaa"))
    assert dfa_design.accepts("aab")
    assert dfa_design.accepts("bbbabb")