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)
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()
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 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))
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()
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()
def perform_stack_action(self, stack: AutomatonStack, configuration: Configuration): stack.add_level()
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()
def check_stack(stack: AutomatonStack): if stack.get_current_content() == '': ParsingError('Empty member variable detected')
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))
def perform_stack_action(self, stack: AutomatonStack, configuration: Configuration): variable = stack.pop_data() stack.add_data(FunctionDeclaration(variable.name))
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()
def add_constraint_to_stack(stack: AutomatonStack): value = stack.pop_data() variable = stack.pop_data() stack.add_data(Constraint(variable, value))
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)
def perform_stack_action(self, stack: AutomatonStack, configuration: Configuration): stack.add_data(VariableExpression(stack.get_current_content())) stack.remove_level() AcceptState.check_stack(stack)
def perform_stack_action(self, stack: AutomatonStack, configuration: Configuration): stack.add_data(FunctionDeclaration(stack.get_current_content())) stack.remove_level() stack.add_level()