Exemplo n.º 1
0
 def cyk(self, q):
     g = ContextFree.prepare_for_cyk(self.grammar)
     if not q:
         q = [EPS]
     try:
         cyk(g, q)
     except BaseException:
         return False
     return True
Exemplo n.º 2
0
 def test_shouldParseCorrectTypesForThreeLoops(self):
     parsed = cyk(self.g, [2, 0, 0, 0, 1])
     self.assertIsInstance(parsed, S)
     self.assertIsInstance(parsed.to_rule, SAB)
     self.assertIsInstance(parsed.to_rule.to_symbols[0], A)
     self.assertIsInstance(parsed.to_rule.to_symbols[1], B)
     a1 = parsed.to_rule.to_symbols[0]
     b = parsed.to_rule.to_symbols[1]
     self.assertIsInstance(b.to_rule, B1)
     self.assertIsInstance(b.to_rule.to_symbols[0], Terminal)
     self.assertIsInstance(a1.to_rule, AAC)
     self.assertIsInstance(a1.to_rule.to_symbols[0], A)
     self.assertIsInstance(a1.to_rule.to_symbols[1], C)
     a2 = a1.to_rule.to_symbols[0]
     c1 = a1.to_rule.to_symbols[1]
     self.assertIsInstance(c1.to_rule, C0)
     self.assertIsInstance(c1.to_rule.to_symbols[0], Terminal)
     self.assertIsInstance(a2.to_rule, AAC)
     self.assertIsInstance(a2.to_rule.to_symbols[0], A)
     self.assertIsInstance(a2.to_rule.to_symbols[1], C)
     a3 = a2.to_rule.to_symbols[0]
     c2 = a2.to_rule.to_symbols[1]
     self.assertIsInstance(c2.to_rule, C0)
     self.assertIsInstance(c2.to_rule.to_symbols[0], Terminal)
     self.assertIsInstance(a3.to_rule, AAC)
     self.assertIsInstance(a3.to_rule.to_symbols[0], A)
     self.assertIsInstance(a3.to_rule.to_symbols[1], C)
     a4 = a3.to_rule.to_symbols[0]
     c3 = a3.to_rule.to_symbols[1]
     self.assertIsInstance(c3.to_rule, C0)
     self.assertIsInstance(c3.to_rule.to_symbols[0], Terminal)
     self.assertIsInstance(a4.to_rule, A2)
     self.assertIsInstance(a4.to_rule.to_symbols[0], Terminal)
Exemplo n.º 3
0
 def test_simpleTestOverUnitRule(self):
     g = Grammar(terminals=[0, 1],
                 nonterminals=[S, A, B, C, D],
                 rules=[RuleS1AB, RuleB0, RuleACD, RuleCD, RuleDEps],
                 start_symbol=S)
     gr = ContextFree.transform_to_chomsky_normal_form(
         ContextFree.remove_unit_rules(
             ContextFree.remove_rules_with_epsilon(g)))
     pars = cyk(gr, [1, 0])
     s = InverseContextFree.epsilon_rules_restore(
         InverseContextFree.unit_rules_restore(
             InverseContextFree.transform_from_chomsky_normal_form(pars)))
     self.assertIsInstance(s, S)
     self.assertIsInstance(s.to_rule, RuleS1AB)
     self.assertIsInstance(s.to_rule.to_symbols[0], Terminal)
     self.assertEqual(s.to_rule.to_symbols[0].s, 1)
     b = s.to_rule.to_symbols[2]
     self.assertIsInstance(b, B)
     self.assertIsInstance(b.to_rule, RuleB0)
     self.assertIsInstance(b.to_rule.to_symbols[0], Terminal)
     self.assertEqual(b.to_rule.to_symbols[0].s, 0)
     a = s.to_rule.to_symbols[1]
     self.assertIsInstance(a, A)
     self.assertIsInstance(a.to_rule, RuleACD)
     d = a.to_rule.to_symbols[1]
     self.assertIsInstance(d, D)
     self.assertIsInstance(d.to_rule, RuleDEps)
     self.assertIs(d.to_rule.to_symbols[0].s, EPS)
     c = a.to_rule.to_symbols[0]
     self.assertIsInstance(c, C)
     self.assertIsInstance(c.to_rule, RuleCD)
     d = c.to_rule.to_symbols[0]
     self.assertIsInstance(d, D)
     self.assertIsInstance(d.to_rule, RuleDEps)
     self.assertIs(d.to_rule.to_symbols[0].s, EPS)
Exemplo n.º 4
0
def parse_from_tokens(input):
    parsed = cyk(_g, input)
    parsed = InverseContextFree.transform_from_chomsky_normal_form(parsed)
    parsed = InverseContextFree.unit_rules_restore(parsed)
    parsed = InverseContextFree.epsilon_rules_restore(parsed)
    parsed = InverseCommon.splitted_rules(parsed)
    return parsed.get_representation()
Exemplo n.º 5
0
    def testRealTraversingReturnValues(self):
        g = Grammar(terminals=[0],
                    nonterminals=[A],
                    rules=[Rules],
                    start_symbol=A)
        res = cyk(g, [0])

        def travRule(item, callback):
            self.assertIsInstance(item, Rules)
            yield item
            for ch in item.to_symbols:
                yield callback(ch)

        def travNonterminals(item, callback):
            self.assertIsInstance(item, A)
            yield item
            yield callback(item.to_rule)

        def travTerms(item, callback):
            self.assertIsInstance(item, Terminal)
            yield item

        resp = list(
            Traversing.traverse_separated(res, travRule, travNonterminals,
                                          travTerms))
        self.assertIsInstance(resp[0], A)
        self.assertIsInstance(resp[1], Rules)
        self.assertIsInstance(resp[2], Terminal)
        self.assertEqual(resp[2].s, 0)
Exemplo n.º 6
0
 def test_transform(self):
     g = Grammar(terminals=[0, 1, 2],
                 nonterminals=[S, A, B, C],
                 rules=[RuleSABC, RuleA0, RuleB1, RuleC2],
                 start_symbol=S)
     com = ContextFree.transform_to_chomsky_normal_form(g)
     pars = cyk(com, [0, 1, 2])
     trans = InverseContextFree.transform_from_chomsky_normal_form(pars)
     self.assertIsInstance(trans, S)
     self.assertIsInstance(trans.to_rule, RuleSABC)
     a = trans.to_rule.to_symbols[0]
     b = trans.to_rule.to_symbols[1]
     c = trans.to_rule.to_symbols[2]
     self.assertIsInstance(a, A)
     self.assertIsInstance(a.to_rule, RuleA0)
     self.assertIsInstance(a.to_rule.to_symbols[0], Terminal)
     self.assertEqual(a.to_rule.to_symbols[0].s, 0)
     self.assertIsInstance(b, B)
     self.assertIsInstance(b.to_rule, RuleB1)
     self.assertIsInstance(b.to_rule.to_symbols[0], Terminal)
     self.assertEqual(b.to_rule.to_symbols[0].s, 1)
     self.assertIsInstance(c, C)
     self.assertIsInstance(c.to_rule, RuleC2)
     self.assertIsInstance(c.to_rule.to_symbols[0], Terminal)
     self.assertEqual(c.to_rule.to_symbols[0].s, 2)
Exemplo n.º 7
0
 def test_repeatOfC(self):
     g = Grammar(terminals=[0, 1, 2, 3],
                 nonterminals=[S, A, B, C, D],
                 rules=[
                     RuleSA, RuleSB, RuleAC, RuleA0A, RuleBD, RuleB2B,
                     RuleB3S, RuleC1C, RuleC0, RuleD3D, RuleD2
                 ],
                 start_symbol=S)
     gr = ContextFree.transform_to_chomsky_normal_form(
         ContextFree.remove_unit_rules(g))
     pars = cyk(gr, [1, 1, 0])
     s = InverseContextFree.unit_rules_restore(
         InverseContextFree.transform_from_chomsky_normal_form(pars))
     self.assertIsInstance(s, S)
     self.assertIsInstance(s.to_rule, RuleSA)
     a = s.to_rule.to_symbols[0]
     self.assertIsInstance(a, A)
     self.assertIsInstance(a.to_rule, RuleAC)
     c = a.to_rule.to_symbols[0]
     self.assertIsInstance(c, C)
     self.assertIsInstance(c.to_rule, RuleC1C)
     self.assertIsInstance(c.to_rule.to_symbols[0], Terminal)
     self.assertEqual(c.to_rule.to_symbols[0].s, 1)
     c = c.to_rule.to_symbols[1]
     self.assertIsInstance(c, C)
     self.assertIsInstance(c.to_rule, RuleC1C)
     self.assertIsInstance(c.to_rule.to_symbols[0], Terminal)
     self.assertEqual(c.to_rule.to_symbols[0].s, 1)
     c = c.to_rule.to_symbols[1]
     self.assertIsInstance(c, C)
     self.assertIsInstance(c.to_rule, RuleC0)
     self.assertIsInstance(c.to_rule.to_symbols[0], Terminal)
     self.assertEqual(c.to_rule.to_symbols[0].s, 0)
Exemplo n.º 8
0
    def test_recursiveTest(self):
        class Rule0(Rule): rule=([A], [A, 2])
        class Rule1(Rule): rule=([A], [B, C])
        class Rule2(Rule): rule=([B], [0])
        class Rule3(Rule): rule=([C], [1])
        g = Grammar(terminals=[0, 1, 2],
                    nonterminals=[A, B, C],
                    rules=[Rule0, Rule1, Rule2, Rule3],
                    start_symbol=A)
        ContextFree.prepare_for_cyk(g, True)
        root = cyk(g, [0, 1, 2, 2])
        root = InverseContextFree.reverse_cyk_transforms(root)
        repre = Traversing.print(root)
        expect = \
"""(N)A
`--(R)Rule0
   |--(N)A
   |  `--(R)Rule0
   |     |--(N)A
   |     |  `--(R)Rule1
   |     |     |--(N)B
   |     |     |  `--(R)Rule2
   |     |     |     `--(T)0
   |     |     `--(N)C
   |     |        `--(R)Rule3
   |     |           `--(T)1
   |     `--(T)2
   `--(T)2
"""
        self.assertEqual(repre, expect)
Exemplo n.º 9
0
    def test_shouldTraverse(self):
        g = Grammar(terminals=[0, 1],
                    nonterminals=[A, B, C],
                    rules=[Rule1, Rule2, Rule3],
                    start_symbol=A)
        root = cyk(g, [0, 1])

        def traverse_func(item, callback):
            if isinstance(item, Rule):
                yield item
                for el in item.to_symbols:
                    yield callback(el)
            elif isinstance(item, Nonterminal):
                yield item
                yield callback(item.to_rule)
            else:
                yield item

        gen = Traversing.traverse(root, traverse_func)
        res = list(gen)
        self.assertTrue(isinstance(res[0], A))
        self.assertTrue(isinstance(res[1], Rule1))
        self.assertTrue(isinstance(res[2], B))
        self.assertTrue(isinstance(res[3], Rule2))
        self.assertTrue(isinstance(res[4], Terminal))
        self.assertEqual(res[4].s, 0)
        self.assertTrue(isinstance(res[5], C))
        self.assertTrue(isinstance(res[6], Rule3))
        self.assertTrue(isinstance(res[7], Terminal))
        self.assertEqual(res[7].s, 1)
Exemplo n.º 10
0
 def test_parseWithThreeIterations(self):
     g = Grammar(terminals=[0, 1],
                 nonterminals=[S, A, B],
                 rules=[Rules, AtoAB],
                 start_symbol=S)
     res = cyk(g, [0, 1, 1, 1, 1])
     res = InverseCommon.splitted_rules(res)
     self.assertIsInstance(res, S)
     self.assertIsInstance(res.to_rule, Rules)
     a = res.to_rule.to_symbols[0]
     b = res.to_rule.to_symbols[1]
     self.assertIsInstance(a, A)
     self.assertIsInstance(b, B)
     self.assertIsInstance(a.to_rule, AtoAB)
     self.assertIsInstance(b.to_rule, Rules)
     b = a.to_rule.to_symbols[1]
     a = a.to_rule.to_symbols[0]
     self.assertIsInstance(a, A)
     self.assertIsInstance(b, B)
     self.assertIsInstance(a.to_rule, AtoAB)
     self.assertIsInstance(b.to_rule, Rules)
     b = a.to_rule.to_symbols[1]
     a = a.to_rule.to_symbols[0]
     self.assertIsInstance(a, A)
     self.assertIsInstance(b, B)
     self.assertIsInstance(a.to_rule, AtoAB)
     self.assertIsInstance(b.to_rule, Rules)
     b = a.to_rule.to_symbols[1]
     a = a.to_rule.to_symbols[0]
     self.assertIsInstance(a, A)
     self.assertIsInstance(b, B)
     self.assertIsInstance(a.to_rule, Rules)
     self.assertIsInstance(b.to_rule, Rules)
Exemplo n.º 11
0
 def test_shouldParseCorrectSymbolsOneLoop(self):
     parsed = cyk(self.g, [2, 0, 1])
     a = parsed.to_rule.to_symbols[0]
     b = parsed.to_rule.to_symbols[1]
     c = a.to_rule.to_symbols[1]
     a = a.to_rule.to_symbols[0]
     self.assertEqual(a.to_rule.to_symbols[0].s, 2)
     self.assertEqual(b.to_rule.to_symbols[0].s, 1)
     self.assertEqual(c.to_rule.to_symbols[0].s, 0)
Exemplo n.º 12
0
 def test_rewriteSimple1(self):
     g = Grammar(terminals=[0, 1],
                 nonterminals=[S],
                 rules=[Rules],
                 start_symbol=S)
     res = cyk(g, [1])
     res = InverseCommon.splitted_rules(res)
     self.assertIsInstance(res, S)
     self.assertIsInstance(res.to_rule, Rules)
Exemplo n.º 13
0
 def test_shouldParseCorrectSymbols(self):
     parsed = cyk(self.g, [0, 1, 2])
     a = parsed.to_rule.to_symbols[0]
     b = parsed.to_rule.to_symbols[1]
     c = b.to_rule.to_symbols[0]
     d = b.to_rule.to_symbols[1]
     self.assertEqual(a.to_rule.to_symbols[0].s, 0)
     self.assertEqual(c.to_rule.to_symbols[0].s, 1)
     self.assertEqual(d.to_rule.to_symbols[0].s, 2)
Exemplo n.º 14
0
 def test_fiveTerminals(self):
     result = cyk(self.g, [My(1), My(2), My(3), My(4), My(5)])
     terms = filter(lambda x: isinstance(x, Terminal), Traversing.post_order(result))
     terms = list(terms)
     self.assertEqual(terms[0].s.prop, 1)
     self.assertEqual(terms[1].s.prop, 2)
     self.assertEqual(terms[2].s.prop, 3)
     self.assertEqual(terms[3].s.prop, 4)
     self.assertEqual(terms[4].s.prop, 5)
Exemplo n.º 15
0
    def testAIn(self):
        g = Grammar(terminals=[0],
                    nonterminals=[A],
                    rules=[Rules],
                    start_symbol=A)
        res = cyk(g, [0])

        def trav(item, callback):
            self.assertTrue(isinstance(item, A))

        Traversing.traverse(res, trav)
Exemplo n.º 16
0
 def testTraversePostOrder(self):
     g = Grammar(terminals=[0],
                 nonterminals=[A],
                 rules=[Rules],
                 start_symbol=A)
     res = cyk(g, [0])
     resp = list(Traversing.post_order(res))
     self.assertIsInstance(resp[2], A)
     self.assertIsInstance(resp[1], Rules)
     self.assertIsInstance(resp[0], Terminal)
     self.assertEqual(resp[0].s, 0)
Exemplo n.º 17
0
 def test_shouldParseCorrectTypesForNoLoop(self):
     parsed = cyk(self.g, [2, 1])
     self.assertIsInstance(parsed, S)
     self.assertIsInstance(parsed.to_rule, SAB)
     self.assertIsInstance(parsed.to_rule.to_symbols[0], A)
     self.assertIsInstance(parsed.to_rule.to_symbols[1], B)
     a = parsed.to_rule.to_symbols[0]
     b = parsed.to_rule.to_symbols[1]
     self.assertIsInstance(a.to_rule, A2)
     self.assertIsInstance(a.to_rule.to_symbols[0], Terminal)
     self.assertIsInstance(b.to_rule, B1)
     self.assertIsInstance(b.to_rule.to_symbols[0], Terminal)
Exemplo n.º 18
0
    def test_nonterminalToTerm(self):
        class Rule1(Rule): rule=([A], [0])
        g = Grammar(terminals=[0],
                    nonterminals=[A],
                    rules=[Rule1],
                    start_symbol=A)
        root = cyk(g, [0])
        repre = Traversing.print(root)
        expect = \
"""(N)A
`--(R)Rule1
   `--(T)0
"""
        self.assertEqual(repre, expect)
Exemplo n.º 19
0
 def test_transform(self):
     g = Grammar(terminals=[0, 1],
                 nonterminals=[S],
                 rules=[Rules],
                 start_symbol=S)
     tr = ContextFree.transform_to_chomsky_normal_form(g)
     pars = cyk(tr, [0, 1])
     rest = InverseContextFree.transform_from_chomsky_normal_form(pars)
     self.assertIsInstance(rest, S)
     self.assertIsInstance(rest.to_rule, Rules)
     self.assertIsInstance(rest.to_rule.to_symbols[0], Terminal)
     self.assertIsInstance(rest.to_rule.to_symbols[1], Terminal)
     self.assertEqual(rest.to_rule.to_symbols[0].s, 0)
     self.assertEqual(rest.to_rule.to_symbols[1].s, 1)
Exemplo n.º 20
0
 def test_shouldParseCorrectSymbolsThreeLoops(self):
     parsed = cyk(self.g, [2, 0, 0, 0, 1])
     a1 = parsed.to_rule.to_symbols[0]
     b = parsed.to_rule.to_symbols[1]
     a2 = a1.to_rule.to_symbols[0]
     c1 = a1.to_rule.to_symbols[1]
     a3 = a2.to_rule.to_symbols[0]
     c2 = a2.to_rule.to_symbols[1]
     a4 = a3.to_rule.to_symbols[0]
     c3 = a3.to_rule.to_symbols[1]
     self.assertEqual(a4.to_rule.to_symbols[0].s, 2)
     self.assertEqual(c3.to_rule.to_symbols[0].s, 0)
     self.assertEqual(c2.to_rule.to_symbols[0].s, 0)
     self.assertEqual(c1.to_rule.to_symbols[0].s, 0)
     self.assertEqual(b.to_rule.to_symbols[0].s, 1)
Exemplo n.º 21
0
    def test_shouldParseAndUseValues(self):
        parsed = cyk(self.g, [My(1), My(2)])
        parsed = InverseContextFree.transform_from_chomsky_normal_form(parsed)
        parsed = InverseContextFree.unit_rules_restore(parsed)
        parsed = InverseContextFree.epsilon_rules_restore(parsed)

        self.assertIsInstance(parsed, S)
        self.assertIsInstance(parsed.to_rule, R)
        left = parsed.to_rule.to_symbols[0]
        right = parsed.to_rule.to_symbols[1]
        self.assertIsInstance(left, Terminal)
        self.assertIsInstance(left.s, My)
        self.assertEqual(left.s.prop, 1)
        self.assertIsInstance(right, Terminal)
        self.assertIsInstance(right.s, My)
        self.assertEqual(right.s.prop, 2)
Exemplo n.º 22
0
 def test_transform(self):
     g = Grammar(terminals=[0, 1, 2, 3],
                 nonterminals=[S, A, B, C],
                 rules=[RuleS, RuleA, RuleB, RuleS0, RuleA1, RuleB2, RuleC3],
                 start_symbol=S)
     gr = ContextFree.transform_to_chomsky_normal_form(g)
     pars = cyk(gr, [1, 3, 1, 2, 3, 1, 3])
     trans = InverseContextFree.transform_from_chomsky_normal_form(pars)
     self.assertIsInstance(trans, S)
     self.assertIsInstance(trans.to_rule, RuleS)
     a = trans.to_rule.to_symbols[0]
     self.assertIsInstance(a, A)
     self.assertIsInstance(a.to_rule, RuleA1)
     self.assertIsInstance(a.to_rule.to_symbols[0], Terminal)
     self.assertEqual(a.to_rule.to_symbols[0].s, 1)
     b = trans.to_rule.to_symbols[1]
     self.assertIsInstance(b, B)
     self.assertIsInstance(b.to_rule, RuleB)
     c = b.to_rule.to_symbols[0]
     self.assertIsInstance(c, C)
     self.assertIsInstance(c.to_rule, RuleC3)
     self.assertIsInstance(c.to_rule.to_symbols[0], Terminal)
     self.assertEqual(c.to_rule.to_symbols[0].s, 3)
     s = b.to_rule.to_symbols[1]
     self.assertIsInstance(s, S)
     self.assertIsInstance(s.to_rule, RuleS)
     self.assertIsInstance(s.to_rule.to_symbols[0], A)
     self.assertIsInstance(s.to_rule.to_symbols[0].to_rule, RuleA1)
     self.assertIsInstance(s.to_rule.to_symbols[0].to_rule.to_symbols[0], Terminal)
     self.assertEqual(s.to_rule.to_symbols[0].to_rule.to_symbols[0].s, 1)
     self.assertIsInstance(s.to_rule.to_symbols[1], B)
     self.assertIsInstance(s.to_rule.to_symbols[1].to_rule, RuleB2)
     self.assertIsInstance(s.to_rule.to_symbols[1].to_rule.to_symbols[0], Terminal)
     self.assertEqual(s.to_rule.to_symbols[1].to_rule.to_symbols[0].s, 2)
     self.assertIsInstance(s.to_rule.to_symbols[2], C)
     self.assertIsInstance(s.to_rule.to_symbols[2].to_rule, RuleC3)
     self.assertIsInstance(s.to_rule.to_symbols[2].to_rule.to_symbols[0], Terminal)
     self.assertEqual(s.to_rule.to_symbols[2].to_rule.to_symbols[0].s, 3)
     self.assertIsInstance(b.to_rule.to_symbols[2], A)
     self.assertIsInstance(b.to_rule.to_symbols[2].to_rule, RuleA1)
     self.assertIsInstance(b.to_rule.to_symbols[2].to_rule.to_symbols[0], Terminal)
     self.assertEqual(b.to_rule.to_symbols[2].to_rule.to_symbols[0].s, 1)
     self.assertIsInstance(pars.to_rule.to_symbols[2], C)
     self.assertIsInstance(pars.to_rule.to_symbols[2].to_rule, RuleC3)
     self.assertIsInstance(pars.to_rule.to_symbols[2].to_rule.to_symbols[0], Terminal)
     self.assertEqual(pars.to_rule.to_symbols[2].to_rule.to_symbols[0].s, 3)
Exemplo n.º 23
0
def test_cnf_grammar_is_equiv():
    lines = ['S a S b S', 'S eps']
    g, symbols = split_cfg(lines)

    tmp_g = ContextFree.transform_to_chomsky_normal_form(g)
    new_g = ContextFree.prepare_for_cyk(tmp_g)
    assert cyk(new_g, 'abab')
    assert cyk(new_g, 'aabb')
    with raises(Exception):
        cyk(new_g, 'aba')
    with raises(Exception):
        cyk(new_g, 'abba')
Exemplo n.º 24
0
    def testTraversingOwnTypes(self):
        g = Grammar(terminals=[0],
                    nonterminals=[A],
                    rules=[Rules],
                    start_symbol=A)
        res = cyk(g, [0])

        def travRule(item, callback):
            self.assertIsInstance(item, Rules)

        def travNonterminals(item, callback):
            self.assertIsInstance(item, A)

        def travTerms(item, callback):
            self.assertIsInstance(item, Terminal)

        Traversing.traverse_separated(res, travRule, travNonterminals,
                                      travTerms)
Exemplo n.º 25
0
 def test_shouldParseCorrectTypes(self):
     parsed = cyk(self.g, [0, 1, 2])
     self.assertIsInstance(parsed, S)
     self.assertIsInstance(parsed.to_rule, SAB)
     self.assertIsInstance(parsed.to_rule.to_symbols[0], A)
     self.assertIsInstance(parsed.to_rule.to_symbols[1], B)
     a = parsed.to_rule.to_symbols[0]
     b = parsed.to_rule.to_symbols[1]
     self.assertIsInstance(a.to_rule, A0)
     self.assertIsInstance(a.to_rule.to_symbols[0], Terminal)
     self.assertIsInstance(b.to_rule, BCD)
     self.assertIsInstance(b.to_rule.to_symbols[0], C)
     self.assertIsInstance(b.to_rule.to_symbols[1], D)
     c = b.to_rule.to_symbols[0]
     d = b.to_rule.to_symbols[1]
     self.assertIsInstance(c.to_rule, C1)
     self.assertIsInstance(c.to_rule.to_symbols[0], Terminal)
     self.assertIsInstance(d.to_rule, D2)
     self.assertIsInstance(d.to_rule.to_symbols[0], Terminal)
Exemplo n.º 26
0
def test_split_cfg_grammar():
    lines = ['S a S b S', 'S eps']
    g, symbols = split_cfg(lines)
    assert len(g.terminals) == 3
    assert len(g.nonterminals) == 1
    assert len(g.rules) == 2

    # Checks if grammar accepts right words using library methods
    new_g = ContextFree.prepare_for_cyk(g)
    assert cyk(new_g, 'abab')
    assert cyk(new_g, 'aabb')
    with raises(Exception):
        cyk(new_g, 'aba')
    with raises(Exception):
        cyk(new_g, 'abba')
Exemplo n.º 27
0
 def test_simpleTestOverUnitRule(self):
     g = Grammar(
         terminals=[0, 1, 2, 3],
         nonterminals=[S, A, B, C],
         rules=[RuleS0A, RuleAB, RuleAEps, RuleBEps, RuleB1C, RuleC11],
         start_symbol=S)
     gr = ContextFree.transform_to_chomsky_normal_form(
         ContextFree.remove_unit_rules(
             ContextFree.remove_rules_with_epsilon(g)))
     pars = cyk(gr, [0])
     s = InverseContextFree.epsilon_rules_restore(
         InverseContextFree.unit_rules_restore(
             InverseContextFree.transform_from_chomsky_normal_form(pars)))
     self.assertIsInstance(s, S)
     self.assertIsInstance(s.to_rule, RuleS0A)
     self.assertIsInstance(s.to_rule.to_symbols[0], Terminal)
     self.assertEqual(s.to_rule.to_symbols[0].s, 0)
     a = s.to_rule.to_symbols[1]
     self.assertIsInstance(a, A)
     self.assertIsInstance(a.to_rule, RuleAEps)
     self.assertIs(a.to_rule.to_symbols[0].s, EPS)
Exemplo n.º 28
0
    def test_splitTest(self):
        class Rule1(Rule): rule=([A], [B, C])
        class Rule2(Rule): rule=([B], [0])
        class Rule3(Rule): rule=([C], [1])
        g = Grammar(terminals=[0, 1],
                    nonterminals=[A, B, C],
                    rules=[Rule1, Rule2, Rule3],
                    start_symbol=A)
        root = cyk(g, [0, 1])
        repre = Traversing.print(root)
        expect = \
"""(N)A
`--(R)Rule1
   |--(N)B
   |  `--(R)Rule2
   |     `--(T)0
   `--(N)C
      `--(R)Rule3
         `--(T)1
"""
        self.assertEqual(repre, expect)
Exemplo n.º 29
0
 def test_directTo2(self):
     g = Grammar(terminals=[0, 1, 2, 3],
                 nonterminals=[S, A, B, C, D],
                 rules=[
                     RuleSA, RuleSB, RuleAC, RuleA0A, RuleA1S, RuleBD,
                     RuleB2B, RuleB3S, RuleC1C, RuleC0, RuleD3D, RuleD2
                 ],
                 start_symbol=S)
     gr = ContextFree.transform_to_chomsky_normal_form(
         ContextFree.remove_unit_rules(g))
     pars = cyk(gr, [2])
     s = InverseContextFree.unit_rules_restore(
         InverseContextFree.transform_from_chomsky_normal_form(pars))
     self.assertIsInstance(s, S)
     self.assertIsInstance(s.to_rule, RuleSB)
     b = s.to_rule.to_symbols[0]
     self.assertIsInstance(b, B)
     self.assertIsInstance(b.to_rule, RuleBD)
     d = b.to_rule.to_symbols[0]
     self.assertIsInstance(d, D)
     self.assertIsInstance(d.to_rule, RuleD2)
     self.assertIsInstance(d.to_rule.to_symbols[0], Terminal)
     self.assertEqual(d.to_rule.to_symbols[0].s, 2)
Exemplo n.º 30
0
    def testRealTraversing(self):
        g = Grammar(terminals=[0],
                    nonterminals=[A],
                    rules=[Rules],
                    start_symbol=A)
        res = cyk(g, [0])

        def travRule(item, callback):
            self.assertIsInstance(item, Rules)
            yield item
            for ch in item.to_symbols:
                yield callback(ch)

        def travNonterminals(item, callback):
            self.assertIsInstance(item, A)
            yield item
            yield callback(item.to_rule)

        def travTerms(item, callback):
            self.assertIsInstance(item, Terminal)
            yield item

        Traversing.traverse_separated(res, travRule, travNonterminals,
                                      travTerms)