Beispiel #1
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()
        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')
Beispiel #2
0
 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([]))
     )
Beispiel #3
0
 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([]))})
Beispiel #4
0
 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(['#']))})
Beispiel #5
0
 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'])))
Beispiel #6
0
    def read_input_stepwise(self, input_str):
        """
        Check if the given string is accepted by this DPDA.

        Yield the DPDA's current state and current stack at each step.
        """
        current_state = self.initial_state
        stack = PDAStack([self.initial_stack_symbol])

        yield current_state, stack
        for input_symbol in input_str:
            current_state, new_stack_top = self._get_transition(
                current_state, input_symbol, stack.top())
            self._replace_stack_top(stack, new_stack_top)
            # Follow any lambda transitions from the current configuration
            while self._has_lambda_transition(current_state, stack.top()):
                current_state, new_stack_top = self._get_transition(
                    current_state, 'ε', stack.top())
                self._replace_stack_top(stack, new_stack_top)
            yield current_state, stack

        self._check_for_input_rejection(current_state, stack)
Beispiel #7
0
    def _validate_input_yield(self, input_str):
        """
        Check if the given string is accepted by this DPDA.

        Yield the DPDA's current state and current stack at each step.
        """
        current_state = self.initial_state
        stack = PDAStack([self.initial_stack_symbol])

        yield current_state, stack
        for input_symbol in input_str:
            current_state, new_stack_top = self._get_transition(
                current_state, input_symbol, stack.top())
            self._replace_stack_top(stack, new_stack_top)
            # Follow any lambda transitions from the current configuration
            while self._has_lambda_transition(current_state, stack.top()):
                current_state, new_stack_top = self._get_transition(
                    current_state, '', stack.top())
                self._replace_stack_top(stack, new_stack_top)
            yield current_state, stack

        self._check_for_input_rejection(current_state, stack)
Beispiel #8
0
    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)
Beispiel #9
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')
Beispiel #10
0
 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']))
     )
Beispiel #11
0
 def test_stack_iter(self):
     """Should loop through the PDA stack in some manner."""
     nose.assert_equal(list(PDAStack(['a', 'b'])), ['a', 'b'])
Beispiel #12
0
 def test_stack_copy(self):
     """Should create an exact of the PDA stack."""
     stack = PDAStack(['a', 'b'])
     stack_copy = stack.copy()
     nose.assert_is_not(stack, stack_copy)
     nose.assert_equal(stack, stack_copy)
Beispiel #13
0
 def test_stack_repr(self):
     """Should create proper string representation of PDA stack."""
     stack = PDAStack(['a', 'b'])
     nose.assert_equal(repr(stack), 'PDAStack([\'a\', \'b\'])')
Beispiel #14
0
 def test_stack_copy(self):
     """Should create an exact of the PDA stack."""
     stack = PDAStack(['a', 'b'])
     stack_copy = stack.copy()
     nose.assert_is_not(stack, stack_copy)
     nose.assert_equal(stack, stack_copy)
Beispiel #15
0
 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_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\'))')
Beispiel #17
0
 def test_create_with_multiple_parameters(self):
     """Should create a new PDA stack with elements passed as parameters."""
     stack = PDAStack('a', 'b')
     nose.assert_equal(stack, PDAStack(('a', 'b')))