示例#1
0
 def test_init_npda_missing_formal_params(self):
     """Should raise an error if formal NPDA parameters are missing."""
     with self.assertRaises(TypeError):
         NPDA(states={'q0', 'q1', 'q2'},
              input_symbols={'a', 'b'},
              initial_state='q0',
              final_states={'q0'})
示例#2
0
def do_npda(alphabet, stack_symb, states_dict):
    npda = NPDA(states={'s0'},
                input_symbols=alphabet,
                stack_symbols=stack_symb,
                transitions={
                    's0': states_dict,
                },
                initial_state='s0',
                initial_stack_symbol=(MARKER_STACK, 'E'),
                final_states={'s0'},
                acceptance_mode='both')
    return npda
示例#3
0
 def test_init_npda_no_acceptance_mode(self):
     """Should create a new NPDA."""
     new_npda = NPDA(states={'q0'},
                     input_symbols={'a', 'b'},
                     stack_symbols={'#'},
                     transitions={'q0': {
                         'a': {
                             '#': {('q0', '')}
                         },
                     }},
                     initial_state='q0',
                     initial_stack_symbol='#',
                     final_states={'q0'})
     self.assertEqual(new_npda.acceptance_mode, 'both')
示例#4
0
 def test_init_npda(self):
     """Should copy NPDA if passed into NPDA constructor."""
     new_npda = NPDA.copy(self.npda)
     self.assert_is_copy(new_npda, self.npda)
示例#5
0
    def setup(self):
        """Reset test automata before every test function."""
        # DPDA which which matches zero or more 'a's, followed by the same
        # number of 'b's (accepting by final state)
        self.dpda = DPDA(states={'q0', 'q1', 'q2', 'q3'},
                         input_symbols={'a', 'b'},
                         stack_symbols={'0', '1'},
                         transitions={
                             'q0': {
                                 'a': {
                                     '0': ('q1', ('1', '0'))
                                 }
                             },
                             'q1': {
                                 'a': {
                                     '1': ('q1', ('1', '1'))
                                 },
                                 'b': {
                                     '1': ('q2', '')
                                 }
                             },
                             'q2': {
                                 'b': {
                                     '1': ('q2', '')
                                 },
                                 '': {
                                     '0': ('q3', ('0', ))
                                 }
                             }
                         },
                         initial_state='q0',
                         initial_stack_symbol='0',
                         final_states={'q3'})

        # NPDA which matches palindromes consisting of 'a's and 'b's
        # (accepting by final state)
        # q0 reads the first half of the word, q1 the other half, q2 accepts.
        # But we have to guess when to switch.
        self.npda = NPDA(states={'q0', 'q1', 'q2'},
                         input_symbols={'a', 'b'},
                         stack_symbols={'A', 'B', '#'},
                         transitions={
                             'q0': {
                                 '': {
                                     '#': {('q2', '#')},
                                 },
                                 'a': {
                                     '#': {('q0', ('A', '#'))},
                                     'A': {
                                         ('q0', ('A', 'A')),
                                         ('q1', ''),
                                     },
                                     'B': {('q0', ('A', 'B'))},
                                 },
                                 'b': {
                                     '#': {('q0', ('B', '#'))},
                                     'A': {('q0', ('B', 'A'))},
                                     'B': {
                                         ('q0', ('B', 'B')),
                                         ('q1', ''),
                                     },
                                 },
                             },
                             'q1': {
                                 '': {
                                     '#': {('q2', '#')}
                                 },
                                 'a': {
                                     'A': {('q1', '')}
                                 },
                                 'b': {
                                     'B': {('q1', '')}
                                 },
                             },
                         },
                         initial_state='q0',
                         initial_stack_symbol='#',
                         final_states={'q2'})