Beispiel #1
0
 def perform_stack_action(self, stack: AutomatonStack,
                          configuration: Configuration):
     MemberState.check_stack(stack)
     stack.add_data(stack.get_current_content())
     MemberState.add_member_to_stack(stack)
     stack.remove_level()
     ExpressionEndState.add_function_to_stack(stack)
Beispiel #2
0
 def perform_stack_action(self, stack: AutomatonStack,
                          configuration: Configuration):
     current_content = stack.get_current_content()
     if not current_content:
         raise ParsingError('A variable name was expected!')
     stack.add_data(Argument(current_content))
     stack.remove_level()
Beispiel #3
0
    def __init__(self, start_state: State, accept_states: List[State],
                 state_descriptions: List[StateDescription]):
        self.start_state = start_state
        self.accept_states = accept_states
        self.state_descriptions = state_descriptions

        self.state_transition_mapping = self._create_state_transition_mapping()
        self.state_default_transition_mapping = self._create_state_default_transition_mapping(
        )

        self.stack = AutomatonStack()
 def perform_stack_action(self, stack: AutomatonStack,
                          configuration: Configuration):
     current_string = stack.get_current_content()
     stack.add_data(
         MessageComponent(MessageComponentType.PYTHON_CODE, current_string))
     stack.remove_level()
     stack.add_level()
Beispiel #5
0
 def perform_stack_action(self, stack: AutomatonStack,
                          configuration: Configuration):
     current_content = stack.get_current_content()
     if not current_content:
         raise ParsingError('Empty function name is not allowed!')
     stack.add_data(FunctionDeclaration(current_content))
     stack.remove_level()
     stack.add_level()
Beispiel #6
0
    def add_constraint_to_stack(stack: AutomatonStack):
        value = stack.pop_data()
        variable = stack.pop_data()

        stack.add_data(Constraint(variable, value))
 def perform_stack_action(self, stack: AutomatonStack,
                          configuration: Configuration):
     stack.add_level()
Beispiel #8
0
 def perform_stack_action(self, stack: AutomatonStack, configuration: Configuration):
     current_content = stack.get_current_content()
     stack.add_data(VariableExpression(current_content))
     stack.remove_level()
     stack.add_level()
Beispiel #9
0
 def perform_stack_action(self, stack: AutomatonStack,
                          configuration: Configuration):
     stack.add_data(VariableExpression(stack.get_current_content()))
     stack.remove_level()
     AcceptState.check_stack(stack)
Beispiel #10
0
 def perform_stack_action(self, stack: AutomatonStack,
                          configuration: Configuration):
     stack.add_data(FunctionDeclaration(stack.get_current_content()))
     stack.remove_level()
     stack.add_level()
Beispiel #11
0
 def perform_stack_action(self, stack: AutomatonStack,
                          configuration: Configuration):
     current_string = stack.get_current_content()
     stack.add_data(ConstantExpression(current_string))
     stack.remove_level()
     stack.add_level()
Beispiel #12
0
class ModifiedPushdownAutomaton:
    def __init__(self, start_state: State, accept_states: List[State],
                 state_descriptions: List[StateDescription]):
        self.start_state = start_state
        self.accept_states = accept_states
        self.state_descriptions = state_descriptions

        self.state_transition_mapping = self._create_state_transition_mapping()
        self.state_default_transition_mapping = self._create_state_default_transition_mapping(
        )

        self.stack = AutomatonStack()

    def _create_state_transition_mapping(
            self) -> Dict[State, Dict[str, Transition]]:
        state_transition_mapping = {}
        for state_description in self.state_descriptions:
            input_state = state_description.default_state
            if input_state not in state_transition_mapping:
                state_transition_mapping[input_state] = {}
            for transition in state_description.transitions:
                input_char = transition.input_configuration.character
                state_transition_mapping[input_state][input_char] = transition
        return state_transition_mapping

    def _create_state_default_transition_mapping(
            self) -> Dict[State, DefaultTransition]:
        state_default_transition_mapping = {}
        for state_description in self.state_descriptions:
            state_default_transition_mapping[state_description.default_state] = \
                state_description.default_transition
        return state_default_transition_mapping

    def parse(self, input_tape: str) -> List[object]:
        self.stack.clear()
        current_state = self.start_state
        input_tape_index = 0

        for input_char in input_tape:
            try:
                configuration = Configuration(current_state, input_char)
                transition = self._find_transition(configuration)
                current_state = self._apply_transition(transition,
                                                       configuration)
                input_tape_index += 1
            except ParsingError as error:
                print('State:', current_state.name)
                print('Index:', input_tape_index)
                print('Original Input:', input_tape)
                raise error

        if current_state not in self.accept_states:
            print('State:', current_state.name)
            raise ParsingError(
                f'Parser was not in a final state after the input tape was read.'
            )

        return self.stack.data_stack[:]

    def _apply_transition(self, transition: Transition,
                          input_configuration: Configuration) -> State:
        transition.perform_stack_action(self.stack, input_configuration)
        output_configuration = transition.get_output_configuration(
            input_configuration)
        self.stack.add_char(output_configuration.character)
        return output_configuration.state

    def _find_transition(self, configuration: Configuration):
        if configuration.state not in self.state_transition_mapping or \
            configuration.character not in self.state_transition_mapping[configuration.state]:
            return self._find_default_transition(configuration.state)
        return self.state_transition_mapping[configuration.state][
            configuration.character]

    def _find_default_transition(self, current_state: State):
        if current_state not in self.state_default_transition_mapping:
            raise ParsingError(
                f'No default transition found for state {current_state.name}.')
        return self.state_default_transition_mapping.get(current_state, None)
Beispiel #13
0
 def perform_stack_action(self, stack: AutomatonStack,
                          configuration: Configuration):
     variable = stack.pop_data()
     stack.add_data(FunctionDeclaration(variable.name))