def test_functions_follow_empty(self): expected = follow(self.follow_empty, 'B') self.assertTrue(len(expected) == 1) self.assertTrue('c' in expected) expected = follow(self.follow_empty, 'C') self.assertTrue(expected == follow(self.follow_empty, 'B'))
def test_functions_no_empty_grammar(self): expected = follow(self.no_empty, 'B') self.assertTrue(len(expected) == 1) self.assertTrue('a' in expected) expected = follow(self.no_empty, 'C') self.assertTrue(expected == follow(self.no_empty, 'B'))
def test_right_recursive_epsilon(self): expected = follow(self.right_recursive_epsilon, 'T') self.assertTrue(len(expected) == 4) self.assertTrue('+' in expected) self.assertTrue('-' in expected) self.assertTrue(')' in expected) self.assertTrue(self.right_recursive_epsilon.EOF in expected) expected = follow(self.right_recursive_epsilon, 'E') self.assertTrue(len(expected) == 2) self.assertTrue(')' in expected) self.assertTrue(self.right_recursive_epsilon.EOF in expected)
def test_left_recursive(self): expected = follow(self.left_recursive, 'T') self.assertTrue(len(expected) == 4) self.assertTrue('+' in expected) self.assertTrue('-' in expected) self.assertTrue(')' in expected) self.assertTrue(self.left_recursive.EOF in expected) expected = follow(self.left_recursive, 'E') self.assertTrue(len(expected) == 4) self.assertTrue('+' in expected) self.assertTrue('-' in expected) self.assertTrue(')' in expected) self.assertTrue(self.left_recursive.EOF in expected)
def test_functions_twolevels_grammar(self): expected = first(self.twolevels, ['A']) self.assertTrue(len(expected) == 1) self.assertTrue('c' in expected) expected = first(self.twolevels, ['B']) self.assertTrue(len(expected) == 1) self.assertTrue(self.twolevels.EMPTY in expected) expected = follow(self.twolevels, 'B') self.assertTrue(len(expected) == 1) self.assertTrue('c' in expected) expected = follow(self.twolevels, 'M') self.assertTrue(expected == follow(self.twolevels, 'B'))
def test_simple(self): expected = follow(self.simple, 'A') self.assertTrue(len(expected) == 3) self.assertTrue('a' in expected) self.assertTrue('b' in expected) self.assertTrue('s' in expected) expected = follow(self.simple, 'B') self.assertTrue(len(expected) == 2) self.assertTrue('b' in expected) self.assertTrue('s' in expected) expected = follow(self.simple, 'S') self.assertTrue(len(expected) == 1) self.assertTrue(self.simple.EOF in expected)
def test_lr_value_with_actions(self): expected = follow(self.lrvalue_with_actions, 'S') self.assertTrue(len(expected) == 2) self.assertTrue(')' in expected) self.assertTrue(self.lrvalue_with_actions.EOF in expected) expected = follow(self.lrvalue_with_actions, 'L') self.assertTrue(len(expected) == 3) self.assertTrue(')' in expected) self.assertTrue('=' in expected) self.assertTrue(self.lrvalue_with_actions.EOF in expected) expected = follow(self.lrvalue_with_actions, 'R') self.assertTrue(len(expected) == 3) self.assertTrue(')' in expected) self.assertTrue('=' in expected) self.assertTrue(self.lrvalue_with_actions.EOF in expected)
def test_functions_simple_grammar(self): expected = first(self.simple, ['A']) self.assertTrue(len(expected) == 1) self.assertTrue('a' in expected) expected = follow(self.simple, 'M') self.assertTrue(len(expected) == 1) self.assertTrue('a' in expected)
def test_bug_missing_terminals_found_by_follow_algorithm(self): '''The problem is due the 'follow' algorithm assumes that if a rule has the nonterminal X, the rules has only one X. Obviously, this is not true in general. ''' found = follow(self.grammar, 'expr') self.assertTrue({'NL', 'NUM'} & found) #found by the 'first' algorithm self.assertTrue({'+', '-'} & found) #found by the 'follow' algorithm self.assertTrue(frozenset(['NUM', 'NL', '+', '-']) == found)
def test_more_complex(self): expected = follow(self.more_complex, 'A') self.assertTrue(len(expected) == 1) self.assertTrue('q' in expected) expected = follow(self.more_complex, 'B') self.assertTrue(len(expected) == 1) self.assertTrue('r' in expected) expected = follow(self.more_complex, 'S') self.assertTrue(len(expected) == 1) self.assertTrue(self.more_complex.EOF in expected) expected = follow(self.more_complex, 'C') self.assertTrue(len(expected) == 1) self.assertTrue('a' in expected) expected = follow(self.more_complex, 'D') self.assertTrue(len(expected) == 1) self.assertTrue('b' in expected)
def followers(self, grammar): return follow(grammar, self.sym_production)