def to_nfa_design(self): pattern_nfa_design = self.pattern.to_nfa_design start_state = State() accept_states = pattern_nfa_design.accept_states + [start_state] rules = pattern_nfa_design.rulebook.rules extra_rules = [ FARule(accept_state, None, pattern_nfa_design.start_state) for accept_state in pattern_nfa_design.accept_states ] + [FARule(start_state, None, pattern_nfa_design.start_state)] rulebook = NFARulebook(rules + extra_rules) return NFADesign(start_state, accept_states, rulebook)
def to_nfa_design(self): first_nfa_design, second_nfa_design = ( self.first.to_nfa_design, self.second.to_nfa_design, ) start_state = State() accept_states = first_nfa_design.accept_states + second_nfa_design.accept_states rules = first_nfa_design.rulebook.rules + second_nfa_design.rulebook.rules extra_rules = [ FARule(start_state, None, nfa_design.start_state) for nfa_design in [first_nfa_design, second_nfa_design] ] rulebook = NFARulebook(rules + extra_rules) return NFADesign(start_state, accept_states, rulebook)
def test_free_move(): rulebook = NFARulebook([ FARule(1, None, 2), FARule(1, None, 4), FARule(2, "a", 3), FARule(3, "a", 2), FARule(4, "a", 5), FARule(5, "a", 6), FARule(6, "a", 4), ]) assert rulebook.next_states([1], None) == set([2, 4]) assert rulebook.follow_free_moves([1]) == set([1, 2, 4]) nfa_design = NFADesign(1, [2, 4], rulebook) assert nfa_design.accepts("aaaaaa") assert nfa_design.accepts("aaa") assert nfa_design.accepts("aa") assert not (nfa_design.accepts("aaaaa")) assert not (nfa_design.accepts("a"))
def test_free_move(self): rulebook = NFARulebook( [ FARule(1, None, 2), FARule(1, None, 4), FARule(2, "a", 3), FARule(3, "a", 2), FARule(4, "a", 5), FARule(5, "a", 6), FARule(6, "a", 4), ] ) # yapf: disable self.assertEqual(rulebook.next_states([1], None), set([2, 4])) self.assertEqual(rulebook.follow_free_moves([1]), set([1, 2, 4])) nfa_design = NFADesign(1, [2, 4], rulebook) self.assertTrue(nfa_design.accepts("aaaaaa")) self.assertTrue(nfa_design.accepts("aaa")) self.assertTrue(nfa_design.accepts("aa")) self.assertFalse(nfa_design.accepts("aaaaa")) self.assertFalse(nfa_design.accepts("a"))
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")
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"))
def to_nfa_design(self): start_state = State() accept_state = State() rule = FARule(start_state, self.character, accept_state) rulebook = NFARulebook([rule]) return NFADesign(start_state, [accept_state], rulebook)
def to_nfa_design(self): start_state = State() accept_states = [start_state] rulebook = NFARulebook([]) return NFADesign(start_state, accept_states, rulebook)