def test_read_input_valid_accept_by_empty_stack(self): """Should return correct config if NPDA accepts by empty stack.""" self.npda.transitions['q2'] = {'': {'#': {('q2', '')}}} self.npda.final_states = set() self.npda.acceptance_mode = 'empty_stack' self.assertEqual(self.npda.read_input('abaaba'), {PDAConfiguration('q2', '', PDAStack([]))})
def _get_next_configuration(self, old_config): """Advance to the next configuration.""" transitions = set() if old_config.remaining_input: transitions.add( self._get_transition(old_config.state, old_config.remaining_input[0], old_config.stack.top())) transitions.add( self._get_transition(old_config.state, '', old_config.stack.top())) if None in transitions: transitions.remove(None) if len(transitions) == 0: raise exceptions.RejectionException( 'The automaton entered a configuration for which no ' 'transition is defined ({}, {}, {})'.format( old_config.state, old_config.remaining_input[0], old_config.stack.top())) if len(transitions) > 1: raise pda_exceptions.NondeterminismError( 'The automaton entered a configuration for which more' 'than one transition is defined ({}, {}'.format( old_config.state, old_config.stack.top())) input_symbol, new_state, new_stack_top = transitions.pop() remaining_input = old_config.remaining_input if input_symbol: remaining_input = remaining_input[1:] new_config = PDAConfiguration( new_state, remaining_input, self._replace_stack_top(old_config.stack, new_stack_top)) return new_config
def test_read_input_valid_accept_by_empty_stack(self): """Should return correct config if DPDA accepts by empty stack.""" self.dpda.transitions['q2']['']['0'] = ('q2', '') nose.assert_equal( self.dpda.read_input('aabb'), PDAConfiguration('q2', '', PDAStack([])) )
def _get_next_configurations(self, old_config): """Advance to the next configurations.""" transitions = set() if old_config.remaining_input: transitions.update(self._get_transitions( old_config.state, old_config.remaining_input[0], old_config.stack.top() )) transitions.update(self._get_transitions( old_config.state, '', old_config.stack.top() )) new_configs = set() for input_symbol, new_state, new_stack_top in transitions: remaining_input = old_config.remaining_input if input_symbol: remaining_input = remaining_input[1:] new_config = PDAConfiguration( new_state, remaining_input, self._replace_stack_top(old_config.stack, new_stack_top) ) new_configs.add(new_config) return new_configs
def read_input_stepwise(self, input_str): """ Check if the given string is accepted by this NPDA. Yield the NPDA's current configurations at each step. """ current_configurations = set() current_configurations.add( PDAConfiguration(self.initial_state, input_str, PDAStack([self.initial_stack_symbol]))) yield current_configurations while current_configurations: new_configurations = set() for config in current_configurations: if self._has_accepted(config): # One accepting configuration is enough. return if config.remaining_input: new_configurations.update( self._get_next_configurations(config)) elif self._has_lambda_transition(config.state, config.stack.top()): new_configurations.update( self._get_next_configurations(config)) current_configurations = new_configurations yield current_configurations raise exceptions.RejectionException( 'the NPDA did not reach an accepting configuration')
def test_read_input_valid_consecutive_lambda_transitions(self): """Should follow consecutive lambda transitions when validating.""" self.npda.states.update({'q3', 'q4'}) self.npda.final_states = {'q4'} self.npda.transitions['q2'] = {'': {'#': {('q3', '#')}}} self.npda.transitions['q3'] = {'': {'#': {('q4', '#')}}} self.assertEqual(self.npda.read_input('abaaba'), {PDAConfiguration('q4', '', PDAStack(['#']))})
def test_read_input_valid_consecutive_lambda_transitions(self): """Should follow consecutive lambda transitions when validating.""" self.dpda.states = {'q4'} self.dpda.final_states = {'q4'} self.dpda.transitions['q2']['']['0'] = ('q3', ('0', )) self.dpda.transitions['q3'] = {'': {'0': ('q4', ('0', ))}} self.assertEqual(self.dpda.read_input('aabb'), PDAConfiguration('q4', '', PDAStack(['0'])))
def read_input_stepwise(self, input_str): """ Check if the given string is accepted by this NPDA. Yield the NPDA's current configurations at each step. """ current_configurations = set() initial_configuration = PDAConfiguration( self.initial_state, input_str, PDAStack([self.initial_stack_symbol])) initial_configuration.cid = 0 initial_configuration.pcid = 0 current_configurations.add(initial_configuration) yield current_configurations cid = 1 while current_configurations: new_configurations = set() for config in current_configurations: if self._has_accepted(config): # One accepting configuration is enough. return next_configurations = set() if config.remaining_input or self._has_lambda_transition( config.state, config.stack.top()): next_configurations = self._get_next_configurations(config) for next_conf in next_configurations: next_conf.cid = cid cid += 1 next_conf.pcid = config.cid new_configurations.update(next_configurations) current_configurations = new_configurations yield current_configurations raise RejectionException( 'the NPDA did not reach an accepting configuration')
def read_input_stepwise(self, input_str): """ Check if the given string is accepted by this DPDA. Yield the DPDA's current configuration at each step. """ current_configuration = PDAConfiguration( self.initial_state, input_str, PDAStack([self.initial_stack_symbol])) yield current_configuration while (current_configuration.remaining_input or self._has_lambda_transition( current_configuration.state, current_configuration.stack.top())): current_configuration = self._get_next_configuration( current_configuration) yield current_configuration if self._has_accepted(current_configuration): return self._check_for_input_rejection(current_configuration)
def test_read_input_valid_accept_by_final_state(self): """Should return correct config if NPDA accepts by final state.""" self.assertEqual(self.npda.read_input('abaaba'), {PDAConfiguration('q2', '', PDAStack(['#']))})
def test_read_input_valid_accept_by_final_state(self): """Should return correct config if DPDA accepts by final state.""" nose.assert_equal( self.dpda.read_input('aabb'), PDAConfiguration('q3', '', PDAStack(['0'])) )
def test_config_repr(self): """Should create proper string representation of PDA stack.""" config = PDAConfiguration('q0', 'ab', PDAStack(['a', 'b'])) nose.assert_equal( repr(config), 'PDAConfiguration(\'q0\', \'ab\', PDAStack(\'a\', \'b\'))')