Esempio n. 1
0
 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)
Esempio n. 2
0
    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)
Esempio n. 3
0
 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)
Esempio n. 4
0
 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)
Esempio n. 5
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)
Esempio n. 6
0
 def test_alt(self):
     pn = alt("MyAlt", lit('a'), lit('b')).apply("abcd", 0)
     self.check(pn, True, 0, 1)
     pn = alt("MyAlt", lit('a'), lit('b')).apply("abcd", 2)
     self.check(pn, False, 2, 0)