示例#1
0
    def test_lexer(self):

        M = MetaGrammar.from_declaration(
            ('LETTER', list('ABCDEF')),
            ('NUMBER', range(4)),
            ('LBR', '['),
            ('RBR', ']'),
            ('string', 'symbol', 'string'),
            ('string', 'empty'),
            ('symbol', 'LETTER', 'number'),
            ('number', 'NUMBER', 'number'),
            ('number', 'empty'),
            )

        lexer = lex.lex(module=Lexer(M))
        lexer.input('A21[]')

        self.assertEqual(list(get_tokens(lexer)), [
            ('LETTER', 'A'),
            ('NUMBER', '2'),
            ('NUMBER', '1'),
            ('LBR', '['),
            ('RBR', ']')
            ]
                             )
        lexer.input('A21X[]')

        with self.assertWarns(UserWarning):
            list(get_tokens(lexer))
示例#2
0
    def test_parser(self):

        M = MetaGrammar.from_declaration(
            ('LETTER', list('ABCDEF')),
            ('NUMBER', range(4)),
            ('LBR', '['),
            ('RBR', ']'),
            ('string', 'string', 'symbol'),
            ('string', 'string', 'level'),
            ('string', 'empty'),
            ('symbol', 'LETTER', 'jump'),
            ('jump', 'jump', 'NUMBER'),
            ('jump', 'empty'),
            ('level', 'LBR', 'substring', 'RBR', 'jump'),
            ('substring', 'substring', 'symbol'),
            ('substring', 'empty'),
            )

        # d = to_agraph(M)
        # d.layout('dot')
        # d.draw('parser.png')

        # parser = Parser(M)
        # print(parser.__dict__)
        # # yacc.yacc(module=parser, start=M.axiom)


        print(SimpleGenerator.generate(M, ['string'], 10))
示例#3
0
文件: test_asg.py 项目: csantran/pg2l
    def setUp(self):
        M = MetaGrammar.from_declaration(
            ('START', 'S'),
            ('LETTER', list('ABCDEF')),
            ('NUMBER', range(4)),
            ('LBR', '['),
            ('RBR', ']'),
            ('REWRITE', ':'),
            ('expression', 'string'),
            ('expression', 'production'),
            ('expression', 'grammar'),
            ('string', 'symbol', 'string'),
            ('string', 'level', 'string'),
            ('string', 'empty'),
            ('symbol', 'LETTER', 'jump'),
            ('jump', 'jump', 'NUMBER'),
            ('jump', 'empty'),
            ('level', 'LBR', 'substring', 'RBR', 'jump'),
            ('substring', 'substring', 'symbol'),
            ('substring', 'substring', 'level'),
            ('substring', 'empty'),
            ('grammar', 'axiom', 'NEWLINE', 'productions'),
            ('productions', 'production', 'NEWLINE', 'productions'),
            ('productions', 'empty'),
            ('axiom', 'START', 'REWRITE', 'string'),
            ('production', 'symbol', 'REWRITE', 'string'),
        )

        self.parser = Parser(M)
示例#4
0
    def test_meta_grammar(self):

        M = MetaGrammar.from_declaration(
            ('S', 'F'),
            ('S', '(', 'S', '+', 'F', ')'),
            ('F', '1'),
            )

        self.assertEqual(M.axiom, 'S')
        self.assertEqual(M.terminals, {'(', ')', '1', '+'})
        self.assertEqual(M.nonterminals, {'S', 'F'})

        self.assertEqual(sorted(list(M.productions)), sorted([
            ('S', ('(', 'S', '+', 'F', ')')),
            ('S', ('F',)),
            ('F', ('1',)),
            ]))

        self.assertEqual(M.alphabet, {'(', '+', 'S', 'F', '1', ')'})
示例#5
0
    def test_lsys_grammar(self):

        M = MetaGrammar.from_declaration(
            ('START', 'S'),
            ('LETTER', list('ABCDEF')),
            ('NUMBER', range(4)),
            ('LBR', '['),
            ('RBR', ']'),
            ('REWRITE', ':'),
            ('expression', 'string'),
            ('expression', 'production'),
            ('expression', 'grammar'),
            ('string', 'symbol', 'string'),
            ('string', 'level', 'string'),
            ('string', 'empty'),
            ('symbol', 'LETTER', 'jump'),
            ('jump', 'jump', 'NUMBER'),
            ('jump', 'empty'),
            ('level', 'LBR', 'string', 'RBR', 'jump'),
            ('grammar', 'axiom', 'NEWLINE', 'productions'),
            ('productions', 'production', 'NEWLINE', 'productions'),
            ('productions', 'empty'),
            ('axiom', 'START', 'REWRITE', 'string'),
            ('production', 'symbol', 'REWRITE', 'string'),
        )

        # d = to_agraph(M)
        # d.layout('dot')
        # d.draw('parser.png')

        L = Grammar.from_string(M, """S:A
A:A[C]B
B:A
""")

        gen = Generator(L)
示例#6
0
    def test_parser(self):

        M = MetaGrammar.from_declaration(
            ('LETTER', list('ABCDEF')),
            ('NUMBER', range(4)),
            ('LBR', '['),
            ('RBR', ']'),
            ('string', 'string', 'symbol'),
            ('string', 'string', 'level'),
            ('string', 'empty'),
            ('symbol', 'LETTER', 'jump'),
            ('jump', 'jump', 'NUMBER'),
            ('jump', 'empty'),
            ('level', 'LBR', 'substring', 'RBR', 'jump'),
            ('substring', 'substring', 'symbol'),
            ('substring', 'empty'),
        )

        # d = to_agraph(M)
        # d.layout('dot')
        # d.draw('parser.png')

        # parser = Parser(M)
        # print(parser.__dict__)
        # # yacc.yacc(module=parser, start=M.axiom)
        parser = Parser(M)
        self.assertEqual(str(parser.parse('AB12[CDA]')), 'AB12[CDA]')

        with self.assertWarns(UserWarning):
            parser.parse('AB12[CDA')

        with self.assertWarns(UserWarning):
            parser.parse('AB12XCDA')

        with self.assertWarns(UserWarning):
            parser.parse('AB12]CDA')