Exemplo n.º 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)
 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_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.º 4
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_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)