| @@ -27,12 +27,12 @@ class Parser(object): | |||||
| def parse(self, seq): | def parse(self, seq): | ||||
| states_idx = self.ga.states_idx | states_idx = self.ga.states_idx | ||||
| stack = [(None, self.ga.init_state_idx)] | |||||
| state_stack = [self.ga.init_state_idx] | |||||
| value_stack = [] | |||||
| i = 0 | i = 0 | ||||
| res = None | |||||
| def get_action(key): | def get_action(key): | ||||
| state = stack[-1][1] | |||||
| state = state_stack[-1] | |||||
| try: | try: | ||||
| return states_idx[state][key] | return states_idx[state][key] | ||||
| except KeyError: | except KeyError: | ||||
| @@ -47,38 +47,40 @@ class Parser(object): | |||||
| def reduce(rule): | def reduce(rule): | ||||
| if rule.expansion: | if rule.expansion: | ||||
| s = stack[-len(rule.expansion):] | |||||
| del stack[-len(rule.expansion):] | |||||
| s = value_stack[-len(rule.expansion):] | |||||
| del state_stack[-len(rule.expansion):] | |||||
| del value_stack[-len(rule.expansion):] | |||||
| else: | else: | ||||
| s = [] | s = [] | ||||
| res = self.callbacks[rule]([x[0] for x in s]) | |||||
| res = self.callbacks[rule](s) | |||||
| if rule.origin == self.ga.start_symbol and len(stack) == 1: | |||||
| if len(state_stack) == 1 and rule.origin == self.ga.start_symbol: | |||||
| return res | return res | ||||
| _action, new_state = get_action(rule.origin) | _action, new_state = get_action(rule.origin) | ||||
| assert _action == ACTION_SHIFT | assert _action == ACTION_SHIFT | ||||
| stack.append((res, new_state)) | |||||
| state_stack.append(new_state) | |||||
| value_stack.append(res) | |||||
| # Main LALR-parser loop | # Main LALR-parser loop | ||||
| while i < len(seq): | while i < len(seq): | ||||
| action, arg = get_action(seq[i].type) | action, arg = get_action(seq[i].type) | ||||
| if action == ACTION_SHIFT: | if action == ACTION_SHIFT: | ||||
| stack.append((seq[i], arg)) | |||||
| state_stack.append(arg) | |||||
| value_stack.append(seq[i]) | |||||
| i+= 1 | i+= 1 | ||||
| else: | else: | ||||
| reduce(arg) | reduce(arg) | ||||
| while stack: | |||||
| while True: | |||||
| _action, rule = get_action('$end') | _action, rule = get_action('$end') | ||||
| assert _action == 'reduce' | assert _action == 'reduce' | ||||
| res = reduce(rule) | res = reduce(rule) | ||||
| if res: | if res: | ||||
| break | |||||
| assert state_stack == [self.ga.init_state_idx] and not value_stack, len(state_stack) | |||||
| return res | |||||
| assert stack == [(None, self.ga.init_state_idx)], len(stack) | |||||
| return res | |||||