Exemple #1
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)
Exemple #2
0
 def test_simpleTestWithChange(self):
     ContextFree.remove_nongenerating_nonterminals(self.g, inplace=True)
     self.assertIn(0, self.g.terminals)
     self.assertIn(1, self.g.terminals)
     self.assertIn(A, self.g.nonterminals)
     self.assertIn(B, self.g.nonterminals)
     self.assertNotIn(C, self.g.nonterminals)
Exemple #3
0
 def test_simpleTestWithoutChange(self):
     ContextFree.remove_nongenerating_nonterminals(self.g)
     self.assertIn(0, self.g.terminals)
     self.assertIn(1, self.g.terminals)
     self.assertIn(A, self.g.nonterminals)
     self.assertIn(B, self.g.nonterminals)
     self.assertIn(C, self.g.nonterminals)
Exemple #4
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)
Exemple #5
0
 def test_transformShouldNotChange(self):
     g = Grammar(terminals=['a', 'b'],
                 nonterminals=[S, A, B],
                 rules=[Rules])
     ContextFree.transform_to_chomsky_normal_form(g)
     self.assertEqual(g.rules.size(), 7)
     self.assertEqual(len(g.rules), 7)
     self.assertEqual(g.nonterminals.size(), 3)
     self.assertEqual(len(g.nonterminals), 3)
     self.assertIn(Rules, g.rules)
Exemple #6
0
 def setUp(self):
     self.g = Grammar(terminals=[My],
                      nonterminals=[S],
                      rules=[R],
                      start_symbol=S)
     ContextFree.remove_useless_symbols(self.g, inplace=True)
     ContextFree.remove_rules_with_epsilon(self.g, True)
     ContextFree.remove_unit_rules(self.g, True)
     ContextFree.remove_useless_symbols(self.g, inplace=True)
     ContextFree.transform_to_chomsky_normal_form(self.g, True)
Exemple #7
0
 def test_simpleTestShouldChange(self):
     g = Grammar(terminals=[0, 1],
                 nonterminals=[S, A, B],
                 rules=[RuleSto0, RuleStoA, RuleAtoAB, RuleBto1],
                 start_symbol=S)
     ContextFree.remove_useless_symbols(g, inplace=True)
     self.assertIn(0, g.terminals)
     self.assertNotIn(1, g.terminals)
     self.assertIn(S, g.nonterminals)
     self.assertNotIn(A, g.nonterminals)
     self.assertNotIn(B, g.nonterminals)
Exemple #8
0
 def test_transformShouldChange(self):
     g = Grammar(nonterminals=[S, A, B, C], rules=[Rules])
     ContextFree.transform_to_chomsky_normal_form(g, True)
     self.assertEqual(g.rules.size(), 2)
     self.assertEqual(len(g.rules), 2)
     from_s = list(filter(lambda r: r.fromSymbol == S, g.rules))[0]
     self.assertEqual(from_s.right[0], A)
     temp = from_s.right[1]
     from_temp = list(filter(lambda r: r.right == [B, C], g.rules))[0]
     self.assertEqual(temp, from_temp.fromSymbol)
     self.assertEqual(g.nonterminals.size(), 5)
     self.assertEqual(len(g.nonterminals), 5)
Exemple #9
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')
Exemple #10
0
    def test_simpleChainingTestShouldNotChange(self):
        g = Grammar(terminals=[0, 1],
                    nonterminals=[S, A, B, C],
                    rules=[Rules],
                    start_symbol=S)
        ContextFree.remove_rules_with_epsilon(g)
        self.assertEqual(len(g.rules), 7)

        class RuleNewStoBC(Rule):
            rule = ([S], [B, C])

        class RuleNewStoAC(Rule):
            rule = ([S], [A, C])

        class RuleNewStoAB(Rule):
            rule = ([S], [A, B])

        class RuleNewAto0(Rule):
            rule = ([A], [0])

        class RuleNewStoB(Rule):
            rule = ([S], [B])

        class RuleNewStoC(Rule):
            rule = ([S], [C])

        class RuleNewStoA(Rule):
            rule = ([S], [A])

        class RuleNewStoEPS(Rule):
            rule = ([S], [EPS])

        self.assertNotIn(RuleNewStoBC, g.rules)
        self.assertNotIn(RuleNewStoAC, g.rules)
        self.assertNotIn(RuleNewStoAB, g.rules)
        self.assertNotIn(RuleNewAto0, g.rules)
        self.assertNotIn(RuleNewStoB, g.rules)
        self.assertNotIn(RuleNewStoC, g.rules)
        self.assertNotIn(RuleNewStoA, g.rules)
        self.assertNotIn(RuleNewStoEPS, g.rules)

        class RuleOldAtoEps(Rule):
            rule = ([A], [EPS])

        class RuleOldBtoEps(Rule):
            rule = ([B], [EPS])

        class RuleOldCtoEps(Rule):
            rule = ([C], [EPS])

        self.assertIn(RuleOldAtoEps, g.rules)
        self.assertIn(RuleOldBtoEps, g.rules)
        self.assertIn(RuleOldCtoEps, g.rules)
 def test_transformShouldChange(self):
     g = Grammar(terminals=[0, 1], nonterminals=[S], rules=[Rules])
     ContextFree.transform_to_chomsky_normal_form(g, True)
     self.assertEqual(g.rules.size(), 3)
     self.assertEqual(len(g.rules), 3)
     from_s = list(filter(lambda r: r.fromSymbol == S, g.rules))[0]
     to0 = from_s.right[0]
     to1 = from_s.right[1]
     self.assertTrue(issubclass(to0, ContextFree.ChomskyTermNonterminal))
     self.assertTrue(issubclass(to1, ContextFree.ChomskyTermNonterminal))
     to0R = list(filter(lambda r: r.right == [0], g.rules))[0]
     to1R = list(filter(lambda r: r.right == [1], g.rules))[0]
     self.assertEqual(to0R.fromSymbol, to0)
     self.assertEqual(to1R.fromSymbol, to1)
     self.assertEqual(g.nonterminals.size(), 3)
     self.assertEqual(len(g.nonterminals), 3)
Exemple #12
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)
Exemple #13
0
 def test_simpleTest(self):
     changed = ContextFree.remove_nongenerating_nonterminals(self.g)
     self.assertIn(0, changed.terminals)
     self.assertIn(1, changed.terminals)
     self.assertIn(A, changed.nonterminals)
     self.assertIn(B, changed.nonterminals)
     self.assertNotIn(C, changed.nonterminals)
 def test_reachablesInvalid(self):
     g = Grammar(terminals=[0, 1, 2],
                 nonterminals=[S, A, B, C],
                 rules=[Rules],
                 start_symbol=S)
     res = ContextFree.find_nonterminals_reachable_by_unit_rules(g)
     self.assertEqual(res.reachables(D), [])
     self.assertEqual(res.reachables(E), [])
Exemple #15
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
 def test_reachInvalid(self):
     g = Grammar(terminals=[0, 1, 2],
                 nonterminals=[S, A, B, C],
                 rules=[Rules],
                 start_symbol=S)
     res = ContextFree.find_nonterminals_reachable_by_unit_rules(g)
     self.assertFalse(res.reach(D, S))
     self.assertFalse(res.reach(S, D))
     self.assertFalse(res.reach(D, D))
     self.assertFalse(res.reach(D, E))
Exemple #17
0
 def test_multipleUsageShouldNotChange(self):
     g = Grammar(terminals=[0, 1, 2, 3],
                 nonterminals=[S, A, B, C, D],
                 rules=[Rules],
                 start_symbol=S)
     ContextFree.remove_rules_with_epsilon(g)
     self.assertEqual(len(g.rules), 12)
     self.assertEqual(g.rules.size(), 12)
     class RuleNewAto0A(Rule): rule = ([A], [0, A])
     class RuleNewAtoA0(Rule): rule = ([A], [A, 0])
     class RuleNewAtoB(Rule): rule = ([A], [B])
     class RuleNewAtoC(Rule): rule = ([A], [C])
     class RuleNewAtoCC(Rule): rule = ([A], [C, C])
     class RuleNewAto1(Rule): rule = ([A], [1])
     class RuleNewBto3(Rule): rule = ([B], [3])
     class RuleNewCtoA3(Rule): rule = ([C], [A, 3])
     class RuleNewDtoAB(Rule): rule = ([D], [A, B])
     class RuleNewDtoAA(Rule): rule = ([D], [A, A])
     class RuleNewDtoA3(Rule): rule = ([D], [A, 3])
     class RuleNewCto3(Rule): rule = ([C], [3])
     class RuleNewDtoA(Rule): rule = ([D], [A])
     class RuleNewDtoB(Rule): rule = ([D], [B])
     class RuleNewDto3(Rule): rule = ([D], [3])
     self.assertNotIn(RuleNewAto0A, g.rules)
     self.assertNotIn(RuleNewAtoA0, g.rules)
     self.assertNotIn(RuleNewAtoB, g.rules)
     self.assertNotIn(RuleNewAtoC, g.rules)
     self.assertNotIn(RuleNewAtoCC, g.rules)
     self.assertNotIn(RuleNewAto1, g.rules)
     self.assertNotIn(RuleNewBto3, g.rules)
     self.assertNotIn(RuleNewCtoA3, g.rules)
     self.assertNotIn(RuleNewDtoAB, g.rules)
     self.assertNotIn(RuleNewDtoAA, g.rules)
     self.assertNotIn(RuleNewDtoA3, g.rules)
     self.assertNotIn(RuleNewCto3, g.rules)
     self.assertNotIn(RuleNewDtoA, g.rules)
     self.assertNotIn(RuleNewDtoB, g.rules)
     self.assertNotIn(RuleNewDto3, g.rules)
     class BtoEps(Rule): rule=([B], [EPS])
     class CtoEps(Rule): rule=([B], [EPS])
     self.assertIn(BtoEps, g.rules)
     self.assertIn(CtoEps, g.rules)
Exemple #18
0
 def test_simpleTest(self):
     g = Grammar(terminals=[0, 1],
                 nonterminals=[S, A, B],
                 rules=[RuleSto0, RuleStoA, RuleAtoAB, RuleBto1],
                 start_symbol=S)
     com = ContextFree.remove_useless_symbols(g)
     self.assertIn(0, com.terminals)
     self.assertNotIn(1, com.terminals)
     self.assertIn(S, com.nonterminals)
     self.assertNotIn(A, com.nonterminals)
     self.assertNotIn(B, com.nonterminals)
Exemple #19
0
 def test_simpleTestShouldNotChange(self):
     g = Grammar(terminals=[0, 1, 2],
                 nonterminals=[S, A, B, C],
                 rules=[Rules],
                 start_symbol=S)
     ContextFree.remove_unit_rules(g)
     # Removed
     class RuleStoA(Rule): rule = ([S], [A])
     self.assertIn(RuleStoA, g.rules)
     class RuleAtoB(Rule): rule = ([A], [B])
     self.assertIn(RuleAtoB, g.rules)
     class RuleBtoC(Rule): rule = ([B], [C])
     self.assertIn(RuleBtoC, g.rules)
     class RuleCtoA(Rule): rule = ([C], [A])
     self.assertIn(RuleCtoA, g.rules)
     # Old rules
     class RuleNewAto0(Rule): rule = ([A], [0])
     self.assertIn(RuleNewAto0, g.rules)
     class RuleNewBto1(Rule): rule = ([B], [1])
     self.assertIn(RuleNewBto1, g.rules)
     class RuleNewCto2(Rule): rule = ([C], [2])
     self.assertIn(RuleNewCto2, g.rules)
     # New rules
     class RuleNewSto0(Rule): rule = ([S], [0])
     self.assertNotIn(RuleNewSto0, g.rules)
     class RuleNewSto1(Rule): rule = ([S], [1])
     self.assertNotIn(RuleNewSto1, g.rules)
     class RuleNewSto2(Rule): rule = ([S], [2])
     self.assertNotIn(RuleNewSto2, g.rules)
     class RuleNewAto1(Rule): rule = ([A], [1])
     self.assertNotIn(RuleNewAto1, g.rules)
     class RuleNewAto2(Rule): rule = ([A], [2])
     self.assertNotIn(RuleNewAto2, g.rules)
     class RuleNewBto2(Rule): rule = ([B], [2])
     self.assertNotIn(RuleNewBto2, g.rules)
     class RuleNewBto0(Rule): rule = ([B], [0])
     self.assertNotIn(RuleNewBto0, g.rules)
     class RuleNewCto0(Rule): rule = ([C], [0])
     self.assertNotIn(RuleNewCto0, g.rules)
     class RuleNewCto1(Rule): rule = ([C], [1])
     self.assertNotIn(RuleNewCto1, g.rules)
Exemple #20
0
 def test_transform(self):
     g = Grammar(nonterminals=[S, A, B, C], rules=[Rules])
     com = ContextFree.transform_to_chomsky_normal_form(g)
     self.assertEqual(com.rules.size(), 2)
     self.assertEqual(len(com.rules), 2)
     from_s = list(filter(lambda r: r.fromSymbol == S, com.rules))[0]
     self.assertEqual(from_s.right[0], A)
     temp = from_s.right[1]
     temp_rule = list(filter(lambda r: r.right == [B, C], com.rules))[0]
     self.assertEqual(temp, temp_rule.fromSymbol)
     self.assertEqual(com.nonterminals.size(), 5)
     self.assertEqual(len(com.nonterminals), 5)
Exemple #21
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)
 def test_transformShouldChange(self):
     g = Grammar(nonterminals=[S, A, B, C], rules=[Rules])
     ContextFree.transform_to_chomsky_normal_form(g, True)
     self.assertEqual(g.rules.size(), 6)
     self.assertEqual(len(g.rules), 6)
     fromS = list(filter(lambda r: r.fromSymbol == S, g.rules))[0]
     self.assertEqual(fromS.right[0], A)
     tempS = fromS.right[1]
     fromSTemp = list(filter(lambda r: r.right == [B, C], g.rules))[0]
     self.assertEqual(tempS, fromSTemp.fromSymbol)
     fromA = list(filter(lambda r: r.fromSymbol == A, g.rules))[0]
     self.assertEqual(fromA.right[0], B)
     tempA = fromA.right[1]
     fromATemp = list(filter(lambda r: r.right == [C, S], g.rules))[0]
     self.assertEqual(tempA, fromATemp.fromSymbol)
     fromB = list(filter(lambda r: r.fromSymbol == B, g.rules))[0]
     self.assertEqual(fromB.right[0], C)
     tempB = fromB.right[1]
     fromBTemp = list(filter(lambda r: r.right == [S, A], g.rules))[0]
     self.assertEqual(tempB, fromBTemp.fromSymbol)
     self.assertEqual(g.nonterminals.size(), 7)
     self.assertEqual(len(g.nonterminals), 7)
Exemple #23
0
    def test_simpleTestShouldChange(self):
        g = Grammar(terminals=[1],
                    nonterminals=[S, A, B, C],
                    rules=[Rules],
                    start_symbol=S)
        ContextFree.remove_rules_with_epsilon(g, True)
        self.assertEqual(g.rules.size(), 6)

        class RuleNewS(Rule):
            rule = ([S], [1])

        class RuleNewA(Rule):
            rule = ([A], [1])

        self.assertIn(RuleNewS, g.rules)
        self.assertIn(RuleNewA, g.rules)
        fromS = list(filter(lambda x: hash(x) == hash(RuleNewS), g.rules))[0]
        self.assertEqual(fromS.fromSymbol, S)
        self.assertEqual(fromS.toSymbol, 1)
        self.assertTrue(isclass(fromS))
        self.assertTrue(issubclass(fromS, ContextFree.EpsilonRemovedRule))
        self.assertEqual(fromS.from_rule.rule, ([S], [1, B]))
        self.assertEqual(fromS.replace_index, 1)
        fromA = list(filter(lambda x: hash(x) == hash(RuleNewA), g.rules))[0]
        self.assertEqual(fromA.fromSymbol, A)
        self.assertEqual(fromA.toSymbol, 1)
        self.assertTrue(isclass(fromA))
        self.assertTrue(issubclass(fromA, ContextFree.EpsilonRemovedRule))
        self.assertEqual(fromA.from_rule.rule, ([A], [1, B]))
        self.assertEqual(fromA.replace_index, 1)

        class OldA(Rule):
            rule = ([A], [EPS])

        class OldB(Rule):
            rule = ([B], [EPS])

        self.assertNotIn(OldA, g.rules)
        self.assertNotIn(OldB, g.rules)
Exemple #24
0
    def test_simpleTestShouldNotChange(self):
        g = Grammar(terminals=[1], nonterminals=[S, A, B, C], rules=[Rules])
        ContextFree.remove_rules_with_epsilon(g)
        self.assertEqual(g.rules.size(), 6)

        class RuleNewS(Rule):
            rule = ([S], [1])

        class RuleNewA(Rule):
            rule = ([A], [1])

        self.assertNotIn(RuleNewS, g.rules)
        self.assertNotIn(RuleNewA, g.rules)

        class OldA(Rule):
            rule = ([A], [EPS])

        class OldB(Rule):
            rule = ([B], [EPS])

        self.assertIn(OldA, g.rules)
        self.assertIn(OldB, g.rules)
Exemple #25
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)
 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)
Exemple #27
0
 def test_simpleChainingTestShouldChange(self):
     g = Grammar(terminals=[0, 1],
                 nonterminals=[S, A, B, C],
                 rules=[Rules],
                 start_symbol=S)
     ContextFree.remove_rules_with_epsilon(g, True)
     self.assertEqual(len(g.rules), 11)
     class RuleNewSto0(Rule): rule = ([S], [0])
     class RuleNewSto1(Rule): rule = ([S], [1])
     class RuleNewStoC(Rule): rule = ([S], [C])
     class RuleNewStoEPS(Rule): rule = ([S], [EPS])
     self.assertIn(RuleNewSto0, g.rules)
     self.assertIn(RuleNewSto1, g.rules)
     self.assertIn(RuleNewStoC, g.rules)
     self.assertIn(RuleNewStoEPS, g.rules)
     fromSto0 = list(filter(lambda x: hash(x) == hash(RuleNewSto0), g.rules))[0]
     self.assertTrue(isclass(fromSto0))
     self.assertTrue(issubclass(fromSto0, ContextFree.EpsilonRemovedRule))
     self.assertEqual(fromSto0.from_rule.rule, ([S], [0, A]))
     self.assertEqual(fromSto0.replace_index, 1)
     fromSto1 = list(filter(lambda x: hash(x) == hash(RuleNewSto1), g.rules))[0]
     self.assertTrue(isclass(fromSto1))
     self.assertTrue(issubclass(fromSto1, ContextFree.EpsilonRemovedRule))
     self.assertEqual(fromSto1.from_rule.rule, ([S], [1, C]))
     self.assertEqual(fromSto1.replace_index, 1)
     fromStoC = list(filter(lambda x: hash(x) == hash(RuleNewStoC), g.rules))[0]
     self.assertTrue(isclass(fromStoC))
     self.assertTrue(issubclass(fromStoC, ContextFree.EpsilonRemovedRule))
     self.assertEqual(fromStoC.from_rule.rule, ([S], [C, C]))
     self.assertEqual(fromStoC.replace_index, 0)
     fromStoEPS = list(filter(lambda x: hash(x) == hash(RuleNewStoEPS), g.rules))[0]
     self.assertTrue(isclass(fromStoEPS))
     self.assertTrue(issubclass(fromStoEPS, ContextFree.EpsilonRemovedRule))
     self.assertEqual(fromStoEPS.from_rule.rule, ([S], [C]))
     self.assertEqual(fromStoEPS.replace_index, 0)
     class RuleOldBtoEps(Rule): rule = ([B], [EPS])
     self.assertNotIn(RuleOldBtoEps, g.rules)
Exemple #28
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')
Exemple #29
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)
Exemple #30
0
    def test_transform(self):
        g = Grammar(terminals=['a', 'b'],
                    nonterminals=[S, A, B],
                    rules=[Rules])
        com = ContextFree.transform_to_chomsky_normal_form(g)
        self.assertEqual(com.rules.size(), 13)
        self.assertEqual(len(com.rules), 13)
        self.assertEqual(com.nonterminals.size(), 9)
        self.assertEqual(len(com.nonterminals), 9)

        # Old
        class AaRule(Rule):
            rule = ([A], ['a'])

        self.assertIn(AaRule, com.rules)

        class BAARule(Rule):
            rule = ([B], [A, A])

        self.assertIn(BAARule, com.rules)
        # New
        StoaB = list(
            filter(lambda r: r.fromSymbol == S and r.right[1] == B,
                   com.rules))[0]
        special_a = StoaB.right[0]
        StobA = list(
            filter(lambda r: r.fromSymbol == S and r.right[1] == A,
                   com.rules))[0]
        special_b = StobA.right[0]

        class StoaBRule(Rule):
            rule = ([S], [special_a, B])

        self.assertIn(StoaBRule, com.rules)

        class StobARule(Rule):
            rule = ([S], [special_b, A])

        self.assertIn(StobARule, com.rules)

        class AtoaSRule(Rule):
            rule = ([A], [special_a, S])

        self.assertIn(AtoaSRule, com.rules)
        AtobAA = list(
            filter(lambda r: r.fromSymbol == A and r.right[0] == special_b,
                   com.rules))[0]
        new_AA = AtobAA.right[1]

        class AtoabAARule(Rule):
            rule = ([A], [special_b, new_AA])

        self.assertIn(AtoabAARule, com.rules)
        BtoaBBaA = list(
            filter(lambda r: r.fromSymbol == B and r.right[0] == special_a,
                   com.rules))[0]
        new_BBaA = BtoaBBaA.right[1]

        class BtoaBBaA(Rule):
            rule = ([B], [special_a, new_BBaA])

        self.assertIn(BtoaBBaA, com.rules)

        class AAtoAARule(Rule):
            rule = ([new_AA], [A, A])

        self.assertIn(AAtoAARule, com.rules)
        BBaAtoBBaA = list(filter(lambda r: r.fromSymbol == new_BBaA,
                                 com.rules))[0]
        self.assertEqual(BBaAtoBBaA.right[0], B)
        new_BaA = BBaAtoBBaA.right[1]
        BaAtoBaA = list(filter(lambda r: r.fromSymbol == new_BaA,
                               com.rules))[0]
        self.assertEqual(BaAtoBaA.right[0], B)
        new_aA = BaAtoBaA.right[1]

        class aAtoaARule(Rule):
            rule = ([new_aA], [special_a, A])

        self.assertIn(aAtoaARule, com.rules)

        # Terminals
        class atoaRule(Rule):
            rule = ([special_a], ['a'])

        self.assertIn(atoaRule, com.rules)

        class btobRule(Rule):
            rule = ([special_b], ['b'])

        self.assertIn(btobRule, com.rules)