def test_indirect_left_recursion(self): rec = lazy() C = seqn("At", rec, lit('t')) B = seqn("Cd", C, lit('d')) A = alt("Br|eps", seqn("Br", B, lit('r')), eps()) rec.set_rule(A) g = Grammar() g.add(A) g.set_nullables() g.set_left_recursives() # A.remove_lazy_rules() Rule.DEBUG = True pn = A.apply("tdr", 0, 0) self.check(pn, True, 0, 3)
def test_left_recursion(self): rec = lazy() r = alt("R", seqn("Ra", rec, lit('a')), lit('b')) rec.set_rule(r) parser = Parser('b') pn = parser.apply_rule(rec, 0) print(pn)
def test_add(self): g = Grammar() r = alt("MyAlt", lit('a'), lit('b')) g.add(r) self.assertEqual(3, len(g.rules)) rec = lazy() C = seqn("At", rec, lit('t')) B = seqn("Cd", C, lit('d')) A = alt("A", seqn("Br", B, lit('r')), eps()) rec.set_rule(A) g.add(A) self.assertEqual(11, len(g.rules)) g.set_nullables() for r in g.rules.values(): print(r, r.is_nullable) print("---") g.set_left_recursives() for r in g.rules.values(): print(r, r.is_left_recursive)
def test_non_left_recursion(self): rec = lazy() r = alt("R", seqn("aRb", lit('a'), rec, lit('b')), eps()) rec.set_rule(r) r.remove_lazy_rules() pn = r.apply("ab", 0, 0) self.check(pn, True, 0, 2) r.init_memos() pn = r.apply("aabb", 0, 0) self.check(pn, True, 0, 4) r.init_memos() pn = r.apply("aacbb", 0, 0) self.check(pn, True, 0, 0)
def test_left_recursion(self): rec = lazy() r = alt("R", seqn("Ra", rec, lit('a')), lit('b')) rec.set_rule(r) r.remove_lazy_rules() pn = r.apply("b", 0, 0) self.check(pn, True, 0, 1) r.init_memo() pn = r.apply("bac", 0, 0) self.check(pn, True, 0, 2) r.init_memo() pn = r.apply("baa", 0, 0) self.check(pn, True, 0, 3) r.init_memo() pn = r.apply("baaac", 0, 0) self.check(pn, True, 0, 4)
def test_seqn(self): pn = seqn("MySeq", lit('a'), lit('b')).apply("abcd", 0) self.check(pn, True, 0, 2) pn = seqn("MySeq", lit('a'), lit('b')).apply("abcd", 2) self.check(pn, False, 2, 0)