Esempio n. 1
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]))
Esempio n. 2
0
    def test_creation(self):
        """ Test of creation """
        pda = PDA()
        self.assertIsNotNone(pda)
        self.assertEqual(len(pda.states), 0)
        self.assertEqual(len(pda.final_states), 0)
        self.assertEqual(len(pda.input_symbols), 0)
        self.assertEqual(len(pda.stack_symbols), 0)

        pda = PDA(start_state=State("A"))
        self.assertIsNotNone(pda)
        self.assertEqual(len(pda.states), 1)
        self.assertEqual(len(pda.stack_symbols), 0)
        self.assertEqual(len(pda.final_states), 0)

        pda = PDA(final_states={
            State("A"), State("A"),
            State("B"), Symbol("B")
        })
        self.assertIsNotNone(pda)
        self.assertEqual(len(pda.states), 3)
        self.assertEqual(len(pda.input_symbols), 0)
        self.assertEqual(len(pda.stack_symbols), 0)
        self.assertEqual(len(pda.final_states), 3)

        pda = PDA(input_symbols={
            Symbol("A"), Symbol("B"),
            Symbol("A"), State("A")
        })
        self.assertIsNotNone(pda)
        self.assertEqual(len(pda.states), 0)
        self.assertEqual(len(pda.input_symbols), 3)
        self.assertEqual(len(pda.stack_symbols), 0)
        self.assertEqual(len(pda.final_states), 0)

        pda = PDA(start_stack_symbol=StackSymbol("A"))
        self.assertIsNotNone(pda)
        self.assertEqual(len(pda.input_symbols), 0)
        self.assertEqual(len(pda.states), 0)
        self.assertEqual(len(pda.stack_symbols), 1)
        self.assertEqual(len(pda.final_states), 0)

        pda = PDA(stack_alphabet={
            StackSymbol("A"),
            StackSymbol("A"),
            StackSymbol("B")
        })
        self.assertIsNotNone(pda)
        self.assertEqual(len(pda.states), 0)
        self.assertEqual(len(pda.input_symbols), 0)
        self.assertEqual(len(pda.stack_symbols), 2)
        self.assertEqual(len(pda.final_states), 0)

        pda = PDA(input_symbols={Epsilon()})
        self.assertIsNotNone(pda)
        self.assertEqual(len(pda.states), 0)
        self.assertEqual(len(pda.input_symbols), 1)
        self.assertEqual(len(pda.stack_symbols), 0)
        self.assertEqual(pda.get_number_transitions(), 0)
        self.assertEqual(len(pda.final_states), 0)
Esempio n. 3
0
 def test_transition(self):
     """ Tests the creation of transition """
     pda = PDA()
     pda.add_transition(
         State("from"), Symbol("input symbol"), StackSymbol("stack symbol"),
         State("to"), [StackSymbol("A"), StackSymbol("B")])
     self.assertEqual(len(pda.states), 2)
     self.assertEqual(len(pda.input_symbols), 1)
     self.assertEqual(len(pda.stack_symbols), 3)
     self.assertEqual(pda.get_number_transitions(), 1)
     pda.add_transition(
         State("from"), Epsilon(), StackSymbol("stack symbol"), State("to"),
         [StackSymbol("A"), StackSymbol("B")])
     self.assertEqual(len(pda.states), 2)
     self.assertEqual(len(pda.input_symbols), 1)
     self.assertEqual(len(pda.stack_symbols), 3)
     self.assertEqual(pda.get_number_transitions(), 2)