Пример #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)
Пример #2
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)
Пример #3
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()
Пример #4
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)
Пример #5
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)
Пример #6
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)
Пример #7
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)
Пример #8
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)
Пример #9
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)
Пример #10
0
    def get(self):
        yield from self.to_symbols[0].get
        yield from self.to_symbols[2].get


g = Grammar(terminals=list(ascii_lowercase + '()*+'),
            nonterminals=[Symb, Concat, Or, Iterate],
            rules=[
                SymbRule, Bracket, ConcatRewrite, ConcatRule, OrRewrite,
                OrRule, IterateRewrite, IterateRule
            ],
            start_symbol=Or)

if __name__ == '__main__':
    gr = ContextFree.prepare_for_cyk(g)

    while True:
        read = input("Type regex or exit to quit: ").strip()
        if read == "exit":
            break
        if len(read) == 0:
            continue

        root = cyk(gr, read)
        root = InverseContextFree.reverse_cyk_transforms(root)
        root = InverseCommon.splitted_rules(root)
        for form in root.get:
            print(form)

    print("Quiting the application")