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)
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)
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)
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.')])
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)
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)
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)
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') ])
class FluxSemanticAnalyzer: 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) def analyze(self): return self.__analyzer.analyze() @property def input_stream(self): return self.__input_stream @staticmethod def __convert_internal_form_to_symbols( internal_form: InternalForm) -> List[Symbol]: return [Symbol(key) for key in internal_form.atom_keys]