Exemplo n.º 1
0
 def test_when_generating_closure_expect_valid_items(self):
     # declarations:
     extended = self.grammar.extend()
     item = ParserItem.item_for(extended.rules_of(NonTerminal("E"))[0])
     # when:
     items = Closure(item, extended).closure()
     # then:
     self.assertEqual(items, [
         ParserItem.from_string('E -> . S'),
         ParserItem.from_string('S -> . a A')
     ])
Exemplo n.º 2
0
 def closure(self) -> List[ParserItem]:
     result = [self._item]
     accumulator = [self._item]
     while accumulator:
         for item in accumulator:
             if item.has_next:
                 for rule in self._grammar.rules_of(item.next):
                     if ParserItem.item_for(rule) not in result:
                         accumulator.append(ParserItem.item_for(rule))
                         result.append(ParserItem.item_for(rule))
             accumulator.remove(item)
     return result
Exemplo n.º 3
0
 def test_when_generating_state_finite_automaton_expect_valid_transitions(
         self):
     # when:
     transitions = StateFiniteAutomaton(self.grammar).transitions
     # then:
     self.assertEqual(len(transitions), 8)
     self.assertEqual(
         (transitions[0].source.items, transitions[0].destination.items,
          transitions[0].symbol), ([
              ParserItem.from_string('E -> . S'),
              ParserItem.from_string('S -> .aA')
          ], [ParserItem.from_string('E -> S.')], NonTerminal("S")))
Exemplo n.º 4
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')
     ])
Exemplo n.º 5
0
    def test_when_shifting_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().parser_step

        # then:
        self.assertEqual(next_parser_step.current_state.items, [
            ParserItem.from_string('S -> a.A'),
            ParserItem.from_string('A -> .bA'),
            ParserItem.from_string('A -> .c')
        ])
Exemplo n.º 6
0
 def test_when_is_going_to_next_state_expect_valid_next_state(self):
     # declarations:
     extended = self.grammar.extend()
     item = ParserItem.item_for(extended.rules_of(NonTerminal("E"))[0])
     items = Closure(item, extended).closure()
     state = State(items, self.grammar)
     # when
     actual = state.go_to(Symbol("a"))
     # then:
     self.assertEqual(actual.items, [
         ParserItem.from_string('S -> a . A'),
         ParserItem.from_string('A -> . b A'),
         ParserItem.from_string('A -> . c')
     ])
Exemplo n.º 7
0
 def __init__(self, grammar: ContextFreeGrammar):
     self.__grammar = grammar
     extended = self.__grammar.extend()
     item = ParserItem.item_for(extended.rules_of(NonTerminal("E"))[0])
     items = Closure(item, extended).closure()
     self.__start = State(items, self.__grammar)
     self.__transitions = []
     self.__states = [self.__start]
     self.__build(self.__start, self.__transitions, self.__states)
Exemplo n.º 8
0
 def test_when_item_parser_not_represents_rule_expect_item_parser_not_equal_to_rule(
         self):
     # give:
     item = ParserItem.from_string("E -> S.")
     # when/then:
     self.assertFalse(item.equals_rule(Rule.from_string("S -> a A")))