def test_pda_conversion(self): """ Tests conversions from a PDA """ state_p = State("p") state_q = State("q") state_a = Symbol("a") state_b = Symbol("b") state_c = Symbol("c") terminal_a = Terminal("a") terminal_b = Terminal("b") terminal_c = Terminal("c") stack_symbol_a = StackSymbol("a") stack_symbol_b = StackSymbol("b") stack_symbol_c = StackSymbol("c") stack_symbol_x0 = StackSymbol("X0") pda = PDA(states={state_p, state_q}, input_symbols={state_a, state_b, state_c}, stack_alphabet={ stack_symbol_a, stack_symbol_b, stack_symbol_c, stack_symbol_x0 }, start_state=state_p, start_stack_symbol=stack_symbol_x0, final_states={state_q}) pda.add_transition(state_p, Epsilon(), stack_symbol_x0, state_q, []) pda.add_transition( state_p, Epsilon(), stack_symbol_x0, state_p, [stack_symbol_a, stack_symbol_b, stack_symbol_c, stack_symbol_x0]) pda.add_transition(state_p, state_a, stack_symbol_a, state_p, []) pda.add_transition(state_p, state_b, stack_symbol_b, state_p, []) pda.add_transition(state_p, state_c, stack_symbol_c, state_p, []) cfg = pda.to_empty_stack().to_cfg() self.assertTrue(cfg.contains([])) self.assertTrue(cfg.contains([terminal_a, terminal_b, terminal_c])) self.assertFalse(cfg.contains([terminal_c, terminal_b, terminal_a]))
def test_to_empty_stack(self): """ Test transformation to empty stack """ state_q = State("#STARTTOFINAL#") state_q0 = State("q0") symbol_e = Symbol("e") symbol_i = Symbol("i") symbol_z = StackSymbol("Z") symbol_z0 = StackSymbol("Z0") pda = PDA(states={state_q, state_q0}, input_symbols={symbol_i, symbol_e}, stack_alphabet={symbol_z, symbol_z0}, start_state=state_q, start_stack_symbol=symbol_z0, final_states={state_q0}) pda.add_transition(state_q, symbol_i, symbol_z, state_q, [symbol_z, symbol_z]) pda.add_transition(state_q, symbol_i, symbol_z0, state_q, [symbol_z, symbol_z0]) pda.add_transition(state_q, symbol_e, symbol_z, state_q, []) pda.add_transition(state_q, Epsilon(), symbol_z0, state_q0, []) new_pda = pda.to_empty_stack() self.assertEqual(len(new_pda.states), 4) self.assertEqual(len(new_pda.input_symbols), 2) self.assertEqual(len(new_pda.stack_symbols), 3) self.assertEqual(new_pda.get_number_transitions(), 11) self.assertEqual(len(new_pda.final_states), 0)
def test_example62(self): """ Example from the book """ state0 = State("q0") state1 = State("q1") state2 = State("q2") s_zero = Symbol("0") s_one = Symbol("1") ss_zero = StackSymbol("0") ss_one = StackSymbol("1") ss_z0 = StackSymbol("Z0") pda = PDA(states={state0, state1, state2}, input_symbols={s_zero, s_one}, stack_alphabet={ss_zero, ss_one, ss_z0}, start_state=state0, start_stack_symbol=ss_z0, final_states={state2}) self.assertEqual(len(pda.states), 3) self.assertEqual(len(pda.input_symbols), 2) self.assertEqual(len(pda.stack_symbols), 3) self.assertEqual(pda.get_number_transitions(), 0) pda.add_transition(state0, s_zero, ss_z0, state0, [ss_zero, ss_z0]) pda.add_transition(state0, s_one, ss_z0, state0, [ss_one, ss_z0]) pda.add_transition(state0, s_zero, ss_zero, state0, [ss_zero, ss_zero]) pda.add_transition(state0, s_one, ss_one, state0, [ss_zero, ss_one]) pda.add_transition(state0, s_one, ss_zero, state0, [ss_one, ss_zero]) pda.add_transition(state0, s_one, ss_one, state0, [ss_one, ss_one]) pda.add_transition(state0, Epsilon(), ss_z0, state1, [ss_z0]) pda.add_transition(state0, Epsilon(), ss_zero, state1, [ss_zero]) pda.add_transition(state0, Epsilon(), ss_one, state1, [ss_one]) pda.add_transition(state1, s_zero, ss_zero, state1, []) pda.add_transition(state1, s_one, ss_one, state1, []) pda.add_transition(state1, Epsilon(), ss_z0, state2, [ss_z0]) self.assertEqual(pda.get_number_transitions(), 12) t_zero = Terminal("0") t_one = Terminal("1") cfg = pda.to_empty_stack().to_cfg() self.assertTrue(cfg.contains([])) self.assertTrue(cfg.contains([t_zero, t_zero])) self.assertTrue(cfg.contains([t_zero, t_one, t_one, t_zero])) self.assertFalse(cfg.contains([t_zero])) self.assertFalse(cfg.contains([t_zero, t_one, t_zero]))
def test_pda_paper(self): """ Code in the paper """ pda = PDA() pda.add_transitions([("q0", "0", "Z0", "q1", ("Z1", "Z0")), ("q1", "1", "Z1", "q2", []), ("q0", "epsilon", "Z1", "q2", [])]) pda.set_start_state("q0") pda.set_start_stack_symbol("Z0") pda.add_final_state("q2") pda_final_state = pda.to_final_state() cfg = pda.to_empty_stack().to_cfg() self.assertTrue(cfg.contains(["0", "1"])) pda_networkx = PDA.from_networkx(pda.to_networkx()) self.assertEqual(pda.states, pda_networkx.states) self.assertEqual(pda.start_state, pda_networkx.start_state) self.assertEqual(pda.final_states, pda_networkx.final_states) self.assertEqual(pda.input_symbols, pda_networkx.input_symbols) self.assertEqual(pda.stack_symbols, pda_networkx.stack_symbols) cfg = pda_networkx.to_empty_stack().to_cfg() pda_networkx.write_as_dot("pda.dot") self.assertTrue(cfg.contains(["0", "1"])) self.assertTrue(path.exists("pda.dot"))