Пример #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)
Пример #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()
Пример #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()
Пример #4
0
 def add_function_to_stack(stack: AutomatonStack):
     arguments = []
     if not stack.data_stack:
         raise ParsingError('Detected a closing parantheses without an opening one')
     stack_content = stack.pop_data()
     while not isinstance(stack_content, FunctionDeclaration):
         arguments.insert(0, stack_content)
         if not stack.data_stack:
             raise ParsingError('Detected a closing parantheses without an opening one')
         stack_content = stack.pop_data()
     stack.add_data(stack_content.create_function(arguments))
Пример #5
0
 def perform_stack_action(self, stack: AutomatonStack,
                          configuration: Configuration):
     current_string = stack.get_current_content()
     stack.add_data(
         MessageComponent(MessageComponentType.STRING, current_string))
     stack.remove_level()
     stack.add_level()
Пример #6
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()
Пример #7
0
 def perform_stack_action(self, stack: AutomatonStack,
                          configuration: Configuration):
     stack.add_level()
Пример #8
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()
Пример #9
0
 def check_stack(stack: AutomatonStack):
     if stack.get_current_content() == '':
         ParsingError('Empty member variable detected')
Пример #10
0
    def add_member_to_stack(stack: AutomatonStack):
        assert len(stack.data_stack) >= 2

        member = stack.pop_data()
        variable = stack.pop_data()
        stack.add_data(MemberExpression(variable.name, member))
Пример #11
0
 def perform_stack_action(self, stack: AutomatonStack,
                          configuration: Configuration):
     variable = stack.pop_data()
     stack.add_data(FunctionDeclaration(variable.name))
Пример #12
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()
Пример #13
0
    def add_constraint_to_stack(stack: AutomatonStack):
        value = stack.pop_data()
        variable = stack.pop_data()

        stack.add_data(Constraint(variable, value))
Пример #14
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)
Пример #15
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)
Пример #16
0
 def perform_stack_action(self, stack: AutomatonStack,
                          configuration: Configuration):
     stack.add_data(FunctionDeclaration(stack.get_current_content()))
     stack.remove_level()
     stack.add_level()