def test_pda_rulebook(self): configuration = PDAConfiguration(1, Stack(["$"])) rulebook = DPDARulebook( [ PDARule(1, "(", 2, "$", ["b", "$"]), PDARule(2, "(", 2, "b", ["b", "b"]), PDARule(2, ")", 2, "b", []), PDARule(2, None, 1, "$", ["$"]), ] ) # yapf: disable configuration = rulebook.next_configuration(configuration, "(") self.assertEqual(configuration.stack, Stack(["$", "b"]))
def test_dpda_design(self): rulebook = DPDARulebook( [ PDARule(1, "(", 2, "$", ["b", "$"]), PDARule(2, "(", 2, "b", ["b", "b"]), PDARule(2, ")", 2, "b", []), PDARule(2, None, 1, "$", ["$"]), ] ) # yapf: disable dpda_design = DPDADesign(1, "$", [1], rulebook) self.assertTrue(dpda_design.accepts("(((((((((())))))))))")) self.assertTrue(dpda_design.accepts("()(())((()))(()(()))")) self.assertFalse(dpda_design.accepts("(()(()(()()(()()))()")) self.assertFalse(dpda_design.accepts("())")) dpda = DPDA(PDAConfiguration(1, Stack(["$"])), [1], rulebook) dpda.read_string("())") self.assertFalse(dpda.accepting) self.assertTrue(dpda.is_stuck)
def test_dpda(self): rulebook = DPDARulebook( [ PDARule(1, "(", 2, "$", ["b", "$"]), PDARule(2, "(", 2, "b", ["b", "b"]), PDARule(2, ")", 2, "b", []), PDARule(2, None, 1, "$", ["$"]), ] ) # yapf: disable dpda = DPDA(PDAConfiguration(1, Stack(["$"])), [1], rulebook) self.assertTrue(dpda.accepting) self.assertFalse(dpda.read_string("(()").accepting) self.assertEqual(dpda.current_configuration.state, 2) with self.assertRaises(RuntimeError): DPDARulebook([PDARule(1, None, 1, "$", ["$"])]).follow_free_moves( PDAConfiguration(1, Stack(["$"]))) dpda = DPDA(PDAConfiguration(1, Stack(["$"])), [1], rulebook) self.assertFalse(dpda.read_string("(()(").accepting) self.assertTrue(dpda.read_string("))()").accepting)
def test_dpda(): dpda = DPDA(PDAConfiguration(1, Stack(["$"])), [1], rulebook) assert dpda.accepting assert not (dpda.read_string("(()").accepting) assert dpda.current_configuration.state == 2 with pytest.raises(RuntimeError): DPDARulebook([PDARule(1, None, 1, "$", ["$"]) ]).follow_free_moves(PDAConfiguration(1, Stack(["$"]))) dpda = DPDA(PDAConfiguration(1, Stack(["$"])), [1], rulebook) assert not (dpda.read_string("(()(").accepting) assert dpda.read_string("))()").accepting dpda = DPDA(PDAConfiguration(1, Stack(["$"])), [1], rulebook) dpda.read_string("())") assert dpda.current_configuration.state == PDAConfiguration.STUCK_STATE assert not dpda.accepting assert dpda.is_stuck
def test_pda_rule(self): rule = PDARule(1, "(", 2, "$", ["b", "$"]) configuration = PDAConfiguration(1, Stack(["$"])) self.assertTrue(rule.applies_to(configuration, "("))
def test_npda_design(): rulebook = NPDARulebook([ PDARule(1, "a", 1, "$", ["a", "$"]), PDARule(1, "a", 1, "a", ["a", "a"]), PDARule(1, "a", 1, "b", ["a", "b"]), PDARule(1, "b", 1, "$", ["b", "$"]), PDARule(1, "b", 1, "a", ["b", "a"]), PDARule(1, "b", 1, "b", ["b", "b"]), PDARule(1, None, 2, "$", ["$"]), PDARule(1, None, 2, "a", ["a"]), PDARule(1, None, 2, "b", ["b"]), PDARule(2, "a", 2, "a", []), PDARule(2, "b", 2, "b", []), PDARule(2, None, 3, "$", ["$"]), ]) configuration = PDAConfiguration(1, Stack(["$"])) npda = NPDA([configuration], [3], rulebook) assert npda.accepting assert not (npda.read_string("abb").accepting) assert PDAConfiguration( 1, Stack(["$", "a", "b", "b"]) in npda.current_configurations) assert npda.read_character("a").accepting assert PDAConfiguration( 1, Stack(["$", "a", "b", "b", "a"]) in npda.current_configurations) npda_design = NPDADesign(1, "$", [3], rulebook) assert npda_design.accepts("abba") assert npda_design.accepts("babbaabbab") assert not (npda_design.accepts("abb"))
def test_pda_rule(): rule = PDARule(1, "(", 2, "$", ["b", "$"]) configuration = PDAConfiguration(1, Stack(["$"])) assert rule.applies_to(configuration, "(")
from computation.automata.pda import ( DPDA, NPDA, DPDADesign, DPDARulebook, NPDADesign, NPDARulebook, PDAConfiguration, PDARule, Stack, ) # check parentheses rulebook = DPDARulebook([ PDARule(1, "(", 2, "$", ["b", "$"]), PDARule(2, "(", 2, "b", ["b", "b"]), PDARule(2, ")", 2, "b", []), PDARule(2, None, 1, "$", ["$"]), ]) def test_pda_rule(): rule = PDARule(1, "(", 2, "$", ["b", "$"]) configuration = PDAConfiguration(1, Stack(["$"])) assert rule.applies_to(configuration, "(") def test_pda_config(): config1 = PDAConfiguration(3, Stack(["$"])) config2 = PDAConfiguration(3, Stack(["$"]))
def test_npda_design(self): rulebook = NPDARulebook( [ PDARule(1, "a", 1, "$", ["a", "$"]), PDARule(1, "a", 1, "a", ["a", "a"]), PDARule(1, "a", 1, "b", ["a", "b"]), PDARule(1, "b", 1, "$", ["b", "$"]), PDARule(1, "b", 1, "a", ["b", "a"]), PDARule(1, "b", 1, "b", ["b", "b"]), PDARule(1, None, 2, "$", ["$"]), PDARule(1, None, 2, "a", ["a"]), PDARule(1, None, 2, "b", ["b"]), PDARule(2, "a", 2, "a", []), PDARule(2, "b", 2, "b", []), PDARule(2, None, 3, "$", ["$"]), ] ) # yapf: disable configuration = PDAConfiguration(1, Stack(["$"])) npda = NPDA([configuration], [3], rulebook) self.assertTrue(npda.accepting) self.assertFalse(npda.read_string("abb").accepting) self.assertTrue( PDAConfiguration( 1, Stack(["$", "a", "b", "b"]) in npda.current_configurations)) self.assertTrue(npda.read_character("a").accepting) self.assertTrue( PDAConfiguration( 1, Stack(["$", "a", "b", "b", "a"]) in npda.current_configurations)) npda_design = NPDADesign(1, "$", [3], rulebook) self.assertTrue(npda_design.accepts("abba")) self.assertTrue(npda_design.accepts("babbaabbab")) self.assertFalse(npda_design.accepts("abb"))