Ejemplo n.º 1
0
    def test_exp(self):

        t = SymbolSet('t', [
            ('bra_open', '('),
            ('bra_close', ')'),
            ('add', '+'),
            ('mul', '*'),
            'id',
        ])

        nt = SymbolSet('nt', [
            ('value', 'V'),
            ('exp', 'E'),
            ('product', 'P'),
            ('start', 'S'),
        ])

        rules = [
            CFGRule(nt.value, (t.id,)),
            CFGRule(nt.value, (t.bra_open, nt.exp, t.bra_close)),
            CFGRule(nt.product, (nt.value,)),
            CFGRule(nt.product, (nt.product, t.mul, nt.value)),
            CFGRule(nt.exp, (nt.product,)),
            CFGRule(nt.exp, (nt.exp, t.add, nt.product)),
            CFGRule(nt.start, (nt.exp,)),
        ]

        identity = lambda x: x

        reductions = [
            Reduction(rules[0], lambda x1: float(x1)),
            Reduction(rules[1], lambda x1, x2, x3: x2),
            Reduction(rules[2], identity),
            Reduction(rules[3], lambda x1, x2, x3: x1 * x3),
            Reduction(rules[4], identity),
            Reduction(rules[5], lambda x1, x2, x3: x1 + x3),
            Reduction(rules[6], identity),
        ]

        cfgex = CFGExtended(nt, t, reductions, nt.start)
        slr_gen = SLRStateGenerator(cfgex)
        lr_gen = LRStateGenerator(cfgex)
        slr_action_table, slr_transition_table, slr_debug_info = slr_gen.generate_tables()
        lr_action_table, lr_transition_table, lr_debug_info = lr_gen.generate_tables()
        slr_parser = LRParser(slr_action_table, slr_transition_table,
                            initial_state=0,
                            debug_info=slr_debug_info)
        lr_parser = LRParser(lr_action_table, lr_transition_table,
                            initial_state=0,
                            debug_info=lr_debug_info)

        #self.dump_action_table(action_table, debug_info)

        for parser in [slr_parser, lr_parser]:
            tokens = [
                Token(t.bra_open),
                Token(t.id, 1),
                Token(t.bra_close),
            ]
            result = parser.parse(tokens)

            self.assertEqual(result, 1)

            tokens = [
                Token(t.id, 2),
                Token(t.add),
                Token(t.id, 2),
            ]
            result = parser.parse(tokens)

            self.assertEqual(result, 4)

            tokens = [
                Token(t.id, 2),
                Token(t.add),
                Token(t.id, 2),
                Token(t.mul),
                Token(t.id, 2),
            ]
            result = parser.parse(tokens)

            self.assertEqual(result, 6)

            tokens = [
                Token(t.id, 2),
                Token(t.add),
                Token(t.bra_open),
                Token(t.id, 2),
                Token(t.mul),
                Token(t.id, 2),
                Token(t.bra_close),
            ]
            result = parser.parse(tokens)

            self.assertEqual(result, 6)

            tokens = [
                Token(t.bra_open),
                Token(t.id, 2),
                Token(t.add),
                Token(t.id, 2),
                Token(t.bra_close),
                Token(t.mul),
                Token(t.id, 2),
            ]
            result = parser.parse(tokens)

            self.assertEqual(result, 8)

            tokens = [
                Token(t.bra_open),
                Token(t.bra_open),
                Token(t.id, 2),
                Token(t.add),
                Token(t.id, 3),
                Token(t.bra_close),
                Token(t.mul),
                Token(t.bra_open),
                Token(t.id, 7),
                Token(t.add),
                Token(t.id, 11),
                Token(t.bra_close),
                Token(t.bra_close),
            ]
            result = parser.parse(tokens)

            self.assertEqual(result, (2 + 3) * (7 + 11))
Ejemplo n.º 2
0
    def test_exp_slr(self):

        t = SymbolSet('t', [
            ('bra_open', '('),
            ('bra_close', ')'),
            ('add', '+'),
            ('mul', '*'),
            'id',
        ])

        nt = SymbolSet('nt', [
            ('value', 'V'),
            ('exp', 'E'),
            ('product', 'P'),
            ('start', 'S'),
        ])

        rules = [
            CFGRule(nt.value, (t.id,)),
            CFGRule(nt.value, (t.bra_open, nt.exp, t.bra_close)),
            CFGRule(nt.product, (nt.value,)),
            CFGRule(nt.product, (nt.product, t.mul, nt.value)),
            CFGRule(nt.exp, (nt.product,)),
            CFGRule(nt.exp, (nt.exp, t.add, nt.product)),
            CFGRule(nt.start, (nt.exp,)),
        ]

        cfg = CFG(nt, t, rules, nt.start)

        start_rule = rules[6]

        start_situation = SLRSituationState(start_rule.head_symbol,
                                            (), start_rule.body_symbols)

        s0 = SLRState(cfg, [start_situation])

        cmp1_s0 = SLRState(cfg, [
            SLRSituationState(nt.start, (), (nt.exp,)),
            SLRSituationState(nt.exp, (), (nt.exp, t.add, nt.product)),
            SLRSituationState(nt.exp, (), (nt.product,)),
            SLRSituationState(nt.product, (), (nt.product, t.mul, nt.value)),
            SLRSituationState(nt.product, (), (nt.value,)),
            SLRSituationState(nt.value, (), (t.bra_open, nt.exp, t.bra_close)),
            SLRSituationState(nt.value, (), (t.id,)),
        ])
        cmp2_s0 = SLRState(cfg, [
            SLRSituationState(nt.start, (), (nt.exp,)),
            SLRSituationState(nt.exp, (nt.exp,), (t.add, nt.product)),
        ])

        self.assertEqual(s0, cmp1_s0)
        self.assertNotEqual(s0, cmp2_s0)

        self.assertEqual(repr(s0)[:len('SLRState([')], 'SLRState([')

        self.assertEqual(unicode(str(s0)), unicode(s0))

        self.assertSetEqual(s0.actions, frozenset([
            nt.exp, nt.product, nt.value,
            t.id, t.bra_open
        ]))

        for a in s0.actions:
            next_states = s0.next_states(a)
            self.assertEqual(len(next_states), 1)

        gen = SLRStateGenerator(cfg)
        action_table, transition_table, debug_info = gen.generate_tables()
        self.assertEqual(len(action_table), 12)
        self.assertEqual(len(transition_table), 12)
        self.assertEqual(len(debug_info), 12)