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()), 8) 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.assertFalse(g.have_rule(RuleNewSto0)) self.assertFalse(g.have_rule(RuleNewSto1)) self.assertFalse(g.have_rule(RuleNewStoC)) self.assertFalse(g.have_rule(RuleNewStoEPS)) class RuleOldBtoEps(Rule): rule = ([B], [EPS]) self.assertTrue(g.have_rule(RuleOldBtoEps))
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)
def test_transformShouldNotChange(self): g = Grammar(nonterminals=[S, A,B,C], rules=[Rules]) ContextFree.transform_to_chomsky_normal_form(g) self.assertEqual(g.rules_count(), 1) self.assertEqual(len(g.rules()), 1) self.assertEqual(g.rules()[0], Rules)
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)
def test_simpleTestShouldChange(self): g = Grammar(terminals=[1], nonterminals=[S, A, B, C], rules=[Rules], start_symbol=S) ContextFree.remove_rules_with_epsilon(g, transform_grammar=True) self.assertEqual(g.rules_count(), 6) class RuleNewS(Rule): rule=([S], [1]) class RuleNewA(Rule): rule=([A], [1]) self.assertTrue(g.have_rule(RuleNewS)) self.assertTrue(g.have_rule(RuleNewA)) fromS = g.get_rule(RuleNewS) 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 = g.get_rule(RuleNewA) 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.assertFalse(g.have_rule(OldA)) self.assertFalse(g.have_rule(OldB))
def test_simpleTestShouldNotChange(self): g = Grammar(terminals=[0, 1], nonterminals=[S, A, B], rules=[RuleSto0, RuleStoA, RuleAtoAB, RuleBto1], start_symbol=S) ContextFree.remove_useless_symbols(g) self.assertTrue(g.have_term([0, 1])) self.assertTrue(g.have_nonterm([S, A, B]))
def test_simpleTestShouldNotChange(self): g = Grammar(terminals=[0,1,2,3], nonterminals=[S, A, B, C, D], rules=[Rules], start_symbol=S) ContextFree.remove_unit_rules(g) # Removed class RuleStoA(Rule): rule = ([S], [A]) self.assertTrue(g.have_rule(RuleStoA)) class RuleStoB(Rule): rule = ([S], [B]) self.assertTrue(g.have_rule(RuleStoB)) class RuleAtoC(Rule): rule = ([A], [C]) self.assertTrue(g.have_rule(RuleAtoC)) class RuleBtoD(Rule): rule = ([B], [D]) self.assertTrue(g.have_rule(RuleBtoD)) # Old rules class RuleNewAto0A(Rule): rule = ([A], [0, A]) self.assertTrue(g.have_rule(RuleNewAto0A)) class RuleNewAto1S(Rule): rule = ([A], [1, S]) self.assertTrue(g.have_rule(RuleNewAto1S)) class RuleNewBto2B(Rule): rule = ([B], [2, B]) self.assertTrue(g.have_rule(RuleNewBto2B)) class RuleNewBto3S(Rule): rule = ([B], [3, S]) self.assertTrue(g.have_rule(RuleNewBto3S)) class RuleNewCto1C(Rule): rule = ([C], [1, C]) self.assertTrue(g.have_rule(RuleNewCto1C)) class RuleNewCto0(Rule): rule = ([C], [0]) self.assertTrue(g.have_rule(RuleNewCto0)) class RuleNewDto3D(Rule): rule = ([D], [3, D]) self.assertTrue(g.have_rule(RuleNewDto3D)) class RuleNewDto2(Rule): rule = ([D], [2]) self.assertTrue(g.have_rule(RuleNewDto2)) # New rules class RuleNewSto0A(Rule): rule = ([S], [0, A]) self.assertFalse(g.have_rule(RuleNewSto0A)) class RuleNewSto1S(Rule): rule = ([S], [1, S]) self.assertFalse(g.have_rule(RuleNewSto1S)) class RuleNewSto1C(Rule): rule = ([S], [1, C]) self.assertFalse(g.have_rule(RuleNewSto1C)) class RuleNewSto0(Rule): rule = ([S], [0]) self.assertFalse(g.have_rule(RuleNewSto0)) class RuleNewSto2B(Rule): rule = ([S], [2, B]) self.assertFalse(g.have_rule(RuleNewSto2B)) class RuleNewSto3S(Rule): rule = ([S], [3, S]) self.assertFalse(g.have_rule(RuleNewSto3S)) class RuleNewSto3D(Rule): rule = ([S], [3, D]) self.assertFalse(g.have_rule(RuleNewSto3D)) class RuleNewSto2(Rule): rule = ([S], [2]) self.assertFalse(g.have_rule(RuleNewSto2)) class RuleNewAto1C(Rule): rule = ([A], [1, C]) self.assertFalse(g.have_rule(RuleNewAto1C)) class RuleNewAto0(Rule): rule = ([A], [0]) self.assertFalse(g.have_rule(RuleNewAto0)) class RuleNewBto3D(Rule): rule = ([B], [3, D]) self.assertFalse(g.have_rule(RuleNewBto3D)) class RuleNewBto2(Rule): rule = ([B], [2]) self.assertFalse(g.have_rule(RuleNewBto2))
def setUp(self): self.g = Grammar(terminals=[My], nonterminals=[S], rules=[R], start_symbol=S) ContextFree.remove_useless_symbols(self.g, transform_grammar=True) ContextFree.remove_rules_with_epsilon(self.g, transform_grammar=True) ContextFree.remove_unit_rules(self.g, transform_grammar=True) ContextFree.remove_useless_symbols(self.g, transform_grammar=True) ContextFree.transform_to_chomsky_normal_form(self.g, transform_grammar=True)
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_count(), 7) self.assertEqual(len(g.rules()), 7) self.assertEqual(g.nonterms_count(), 3) self.assertEqual(len(g.nonterms()), 3) self.assertTrue(g.have_rule(Rules))
def test_simpleTestShouldNotChange(self): g = Grammar(terminals=[0, 1], nonterminals=[S, A, B, C], rules=[Rules], start_symbol=S) ContextFree.remove_unit_rules(g) # Removed class RuleStoA(Rule): rule=([S], [A]) self.assertTrue(g.have_rule(RuleStoA)) class RuleAtoB(Rule): rule=([A], [B]) self.assertTrue(g.have_rule(RuleAtoB)) class RuleBtoC(Rule): rule=([B], [C]) self.assertTrue(g.have_rule(RuleBtoC)) # Old rules class RuleNewSto0B0(Rule): rule = ([S], [0, B, 0]) self.assertTrue(g.have_rule(RuleNewSto0B0)) class RuleNewAto0A(Rule): rule = ([A], [0, A]) self.assertTrue(g.have_rule(RuleNewAto0A)) class RuleNewBto1B(Rule): rule = ([B], [1, B]) self.assertTrue(g.have_rule(RuleNewBto1B)) class RuleNewBtoAB(Rule): rule = ([B], [A, B]) self.assertTrue(g.have_rule(RuleNewBtoAB)) class RuleNewBtoEPS(Rule): rule = ([B], [EPS]) self.assertTrue(g.have_rule(RuleNewBtoEPS)) class RuleNewCto1A(Rule): rule = ([C], [1, A]) self.assertTrue(g.have_rule(RuleNewCto1A)) class RuleNewCto1(Rule): rule = ([C], [1]) self.assertTrue(g.have_rule(RuleNewCto1)) # New rules class RuleNewSto0A(Rule): rule = ([S], [0, A]) self.assertFalse(g.have_rule(RuleNewSto0A)) class RuleNewSto1B(Rule): rule = ([S], [1, B]) self.assertFalse(g.have_rule(RuleNewSto1B)) class RuleNewStoAB(Rule): rule = ([S], [A, B]) self.assertFalse(g.have_rule(RuleNewStoAB)) class RuleNewStoEPS(Rule): rule = ([S], [EPS]) self.assertFalse(g.have_rule(RuleNewStoEPS)) class RuleNewSto1A(Rule): rule = ([S], [1, A]) self.assertFalse(g.have_rule(RuleNewSto1A)) class RuleNewSto1(Rule): rule = ([S], [1]) self.assertFalse(g.have_rule(RuleNewSto1)) class RuleNewAto1B(Rule): rule = ([A], [1, B]) self.assertFalse(g.have_rule(RuleNewAto1B)) class RuleNewAtoAB(Rule): rule = ([A], [A, B]) self.assertFalse(g.have_rule(RuleNewAtoAB)) class RuleNewAtoEPS(Rule): rule = ([A], [EPS]) self.assertFalse(g.have_rule(RuleNewAtoEPS)) class RuleNewAto1A(Rule): rule = ([A], [1, A]) self.assertFalse(g.have_rule(RuleNewAto1A)) class RuleNewAto1(Rule): rule = ([A], [1]) self.assertFalse(g.have_rule(RuleNewAto1)) class RuleNewBto1A(Rule): rule = ([B], [1, A]) self.assertFalse(g.have_rule(RuleNewBto1A)) class RuleNewBto1(Rule): rule = ([B], [1]) self.assertFalse(g.have_rule(RuleNewBto1))
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, transform_grammar=True) self.assertTrue(g.have_term(0)) self.assertFalse(g.have_term(1)) self.assertTrue(g.have_nonterm(S)) self.assertFalse(g.have_nonterm(A)) self.assertFalse(g.have_nonterm(B))
def test_transformShouldChange(self): g = Grammar(nonterminals=[S, A, B, C], rules=[Rules]) ContextFree.transform_to_chomsky_normal_form(g, transform_grammar=True) self.assertEqual(g.rules_count(), 2) self.assertEqual(len(g.rules()), 2) fromS = list(filter(lambda r: r.fromSymbol == S, g.rules()))[0] self.assertEqual(fromS.right[0], A) temp = fromS.right[1] fromTemp = list(filter(lambda r: r.right == [B, C], g.rules()))[0] self.assertEqual(temp, fromTemp.fromSymbol) self.assertEqual(g.nonterms_count(), 5) self.assertEqual(len(g.nonterms()), 5)
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_count(), 6) class RuleNewS(Rule): rule=([S], [1]) class RuleNewA(Rule): rule=([A], [1]) self.assertFalse(g.have_rule(RuleNewS)) self.assertFalse(g.have_rule(RuleNewA)) class OldA(Rule): rule=([A], [EPS]) class OldB(Rule): rule=([B], [EPS]) self.assertTrue(g.have_rule(OldA)) self.assertTrue(g.have_rule(OldB))
def test_transformShouldChange(self): g = Grammar(terminals=['a', 'b'], nonterminals=[S, A, B], rules=[Rules]) ContextFree.transform_to_chomsky_normal_form(g, transform_grammar=True) self.assertEqual(g.rules_count(), 13) self.assertEqual(len(g.rules()), 13) self.assertEqual(g.nonterms_count(), 9) self.assertEqual(len(g.nonterms()), 9) # Old class AaRule(Rule): rule=([A], ['a']) self.assertTrue(g.have_rule(AaRule)) class BAARule(Rule): rule=([B], [A, A]) self.assertTrue(g.have_rule(BAARule)) # New StoaB = list(filter(lambda r: r.fromSymbol == S and r.right[1] == B, g.rules()))[0] special_a = StoaB.right[0] StobA = list(filter(lambda r: r.fromSymbol == S and r.right[1] == A, g.rules()))[0] special_b = StobA.right[0] class StoaBRule(Rule): rule=([S], [special_a, B]) self.assertTrue(g.have_rule(StoaBRule)) class StobARule(Rule): rule=([S], [special_b, A]) self.assertTrue(g.have_rule(StobARule)) class AtoaSRule(Rule): rule=([A], [special_a, S]) self.assertTrue(g.have_rule(AtoaSRule)) AtobAA = list(filter(lambda r: r.fromSymbol == A and r.right[0] == special_b, g.rules()))[0] new_AA = AtobAA.right[1] class AtoabAARule(Rule): rule=([A], [special_b, new_AA]) self.assertTrue(g.have_rule(AtoabAARule)) BtoaBBaA = list(filter(lambda r: r.fromSymbol == B and r.right[0] == special_a, g.rules()))[0] new_BBaA = BtoaBBaA.right[1] class BtoaBBaA(Rule): rule=([B], [special_a, new_BBaA]) self.assertTrue(g.have_rule(BtoaBBaA)) class AAtoAARule(Rule): rule=([new_AA], [A, A]) self.assertTrue(g.have_rule(AAtoAARule)) BBaAtoBBaA = list(filter(lambda r: r.fromSymbol == new_BBaA, g.rules()))[0] self.assertEqual(BBaAtoBBaA.right[0], B) new_BaA = BBaAtoBBaA.right[1] BaAtoBaA = list(filter(lambda r: r.fromSymbol == new_BaA, g.rules()))[0] self.assertEqual(BaAtoBaA.right[0], B) new_aA = BaAtoBaA.right[1] class aAtoaARule(Rule): rule = ([new_aA], [special_a, A]) self.assertTrue(g.have_rule(aAtoaARule)) # Terminals class atoaRule(Rule): rule=([special_a], ['a']) self.assertTrue(g.have_rule(atoaRule)) class btobRule(Rule): rule=([special_b], ['b']) self.assertTrue(g.have_rule(btobRule))
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, transform_grammar=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.assertTrue( g.have_rule([RuleNewSto0, RuleNewSto1, RuleNewStoC, RuleNewStoEPS])) fromSto0 = g.get_rule(RuleNewSto0) 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 = g.get_rule(RuleNewSto1) 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 = g.get_rule(RuleNewStoC) 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 = g.get_rule(RuleNewStoEPS) 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.assertFalse(g.have_rule(RuleOldBtoEps))
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)
def test_simpleTest(self): g = Grammar(terminals=[1], nonterminals=[S, A, B, C], rules=[Rules]) com = ContextFree.remove_rules_with_epsilon(g) self.assertEqual(com.rules_count(), 6) class RuleNewS(Rule): rule=([S], [1]) class RuleNewA(Rule): rule=([A], [1]) self.assertTrue(com.have_rule(RuleNewS)) self.assertTrue(com.have_rule(RuleNewA)) fromS = com.get_rule(RuleNewS) 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 = com.get_rule(RuleNewA) 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.assertFalse(com.have_rule(OldA)) self.assertFalse(com.have_rule(OldB))
def test_transformShouldChange(self): g = Grammar(terminals=[0, 1], nonterminals=[S], rules=[Rules]) ContextFree.transform_to_chomsky_normal_form(g, transform_grammar=True) self.assertEqual(g.rules_count(), 3) self.assertEqual(len(g.rules()), 3) fromS = list(filter(lambda r: r.fromSymbol == S, g.rules()))[0] to0 = fromS.right[0] to1 = fromS.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.nonterms_count(), 3) self.assertEqual(len(g.nonterms()), 3)
def test_partlyIncompleteTestWithChange(self): changed = ContextFree.remove_nongenerating_nonterminals( self.g, transform_grammar=True) self.assertEqual(id(changed), id(self.g)) self.assertTrue(self.g.have_term([0, 1])) self.assertTrue(self.g.have_nonterm([A, B])) self.assertFalse(self.g.have_nonterm(C))
def test_simpleTest(self): g = Grammar(terminals=[1], nonterminals=[S, A, B, C], rules=[Rules]) n = ContextFree.find_nonterminals_rewritable_to_epsilon(g) self.assertEqual(len(n), 2) for i in [A, B]: self.assertIn(i, n)
def test_multipleUsage(self): g = Grammar(terminals=[0, 1, 2, 3], nonterminals=[S, A, B, C, D], rules=[Rules]) n = ContextFree.find_nonterminals_rewritable_to_epsilon(g) self.assertEqual(len(n), 4) for i in [A, B, C, D]: self.assertIn(i, n)
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), [])
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))
def test_pathRulesInvalid(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.assertIsNone(res.path_rules(A, D)) self.assertIsNone(res.path_rules(D, S)) self.assertIsNone(res.path_rules(D, S)) self.assertIsNone(res.path_rules(E, D))
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_count(), 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.assertFalse(g.have_rule(RuleNewAto0A)) self.assertFalse(g.have_rule(RuleNewAtoA0)) self.assertFalse(g.have_rule(RuleNewAtoB)) self.assertFalse(g.have_rule(RuleNewAtoC)) self.assertFalse(g.have_rule(RuleNewAtoCC)) self.assertFalse(g.have_rule(RuleNewAto1)) self.assertFalse(g.have_rule(RuleNewBto3)) self.assertFalse(g.have_rule(RuleNewCtoA3)) self.assertFalse(g.have_rule(RuleNewDtoAB)) self.assertFalse(g.have_rule(RuleNewDtoAA)) self.assertFalse(g.have_rule(RuleNewDtoA3)) self.assertFalse(g.have_rule(RuleNewCto3)) self.assertFalse(g.have_rule(RuleNewDtoA)) self.assertFalse(g.have_rule(RuleNewDtoB)) self.assertFalse(g.have_rule(RuleNewDto3)) class BtoEps(Rule): rule=([B], [EPS]) class CtoEps(Rule): rule=([B], [EPS]) self.assertTrue(g.have_rule(BtoEps)) self.assertTrue(g.have_rule(CtoEps))
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.assertTrue(com.have_term(0)) self.assertFalse(com.have_term(1)) self.assertTrue(com.have_nonterm(S)) self.assertFalse(com.have_nonterm(A)) self.assertFalse(com.have_nonterm(B))
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_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)
def test_transformShouldChange(self): g = Grammar(nonterminals=[S, A, B, C], rules=[Rules]) ContextFree.transform_to_chomsky_normal_form(g, transform_grammar=True) self.assertEqual(g.rules_count(), 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.nonterms_count(), 7) self.assertEqual(len(g.nonterms()), 7)
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)