예제 #1
0
 def __replace_stack(self, state: DestinationState,
                     action_table: ActionTable) -> List[DestinationState]:
     return self.__remove_from_stack(state) + [
         action_table.next_state(
             self.__remove_from_stack(state)[-1],
             state.to_rule().left)
     ]
예제 #2
0
 def test_when_given_wrong_expression_expect_analyzer_raises_exception(
         self):
     # given:
     action_table = ActionTable(StateFiniteAutomaton(self.grammar))
     analyzer = SemanticAnalyzer(action_table, Symbol.from_string("aabc"))
     # when/then:
     self.assertRaises(AnalyzerConflict, analyzer.analyze)
예제 #3
0
 def test_when_getting_action_for_next_state_expect_right_action(self):
     # given:
     action_table = ActionTable(StateFiniteAutomaton(self.grammar))
     analyzer = SemanticAnalyzer(action_table, Symbol.from_string("abbbc"))
     # when:
     action = analyzer.next_action
     # then:
     self.assertEqual(action.source, analyzer.next_state)
예제 #4
0
 def __init__(self, internal_form: InternalForm,
              grammar: ContextFreeGrammar):
     self.__grammar = grammar
     self.__action_table = ActionTable(StateFiniteAutomaton(self.__grammar))
     self.__input_stream = self.__convert_internal_form_to_symbols(
         internal_form)
     self.__analyzer = SemanticAnalyzer(self.__action_table,
                                        self.__input_stream)
예제 #5
0
 def test_when_given_correct_expression_expect_analyzer_returns_true(self):
     # given:
     action_table = ActionTable(StateFiniteAutomaton(self.grammar))
     analyzer = SemanticAnalyzer(action_table, Symbol.from_string("abbbc"))
     # when:
     actual = analyzer.analyze()
     # print(actual)
     # then:
     self.assertTrue(actual)
예제 #6
0
 def test_when_analyzer_accepts_input_expect_is_accepted_equals_true(self):
     # given:
     action_table = ActionTable(StateFiniteAutomaton(self.grammar))
     analyzer = SemanticAnalyzer(action_table, Symbol.from_string("abbbc"))
     # when:
     actual = analyzer.shift().shift().shift().shift().shift().reduce(
     ).reduce().reduce().reduce().reduce().is_accepted
     # then:
     self.assertTrue(actual)
예제 #7
0
 def test_when_building_action_table_from_grammar_expect_valid_action_table(
         self):
     # given:
     state_finite_automaton = StateFiniteAutomaton(self.grammar)
     # when:
     action_table = ActionTable(state_finite_automaton)
     # then:
     self.assertTrue(len(action_table.actions),
                     len(state_finite_automaton.states))
예제 #8
0
 def test_when_reducing_analyzer_expect_valid_next_parser_step(self):
     # given:
     action_table = ActionTable(StateFiniteAutomaton(self.grammar))
     analyzer = SemanticAnalyzer(action_table, Symbol.from_string("abbbc"))
     # when:
     next_parser_step = analyzer.shift().shift().shift().shift().shift(
     ).reduce().reduce().parser_step
     # then:
     self.assertEqual(next_parser_step.current_state.items,
                      [ParserItem.from_string('A -> bA.')])
예제 #9
0
 def test_when_getting_next_state_from_parser_step_expect_next_state(self):
     # given:
     action_table = ActionTable(StateFiniteAutomaton(self.grammar))
     analyzer = SemanticAnalyzer(action_table, Symbol.from_string("abbbc"))
     # when:
     next_state = analyzer.next_state
     # then:
     self.assertEqual(next_state.items, [
         ParserItem.from_string('S -> a.A'),
         ParserItem.from_string('A -> .bA'),
         ParserItem.from_string('A -> .c')
     ])
예제 #10
0
 def test_when_grammar_has_empty_rule_expect_to_accept_input_stream(self):
     # given:
     data = {
         'terminals': ['1', '2'],
         'non-terminals': ['S'],
         'rules': ['S -> ', 'S -> 1 S', 'S -> 2 S'],
         'start': 'S'
     }
     grammar = ContextFreeGrammar.from_complex_dictionary(data)
     # when/then:
     action_table = ActionTable(StateFiniteAutomaton(grammar))
     analyzer = SemanticAnalyzer(action_table,
                                 Symbol.from_complex_string("1 1 1 2"))
     # when:
     actual = analyzer.analyze()
     # then:
     self.assertTrue(actual)