示例#1
0
    def test_follow2(self):
        expected = {'S': set('$'), 'A': set('$'), 'B': set('b'), 'C': set('d')}
        g = grammar.make_dummy_grammar("test_input/test2.cfg")
        first = first_follow.get_first(g)
        got = first_follow.get_follow(g, first)

        self.assertEqual(expected, got)
示例#2
0
def construct_parse_table(g):
    first = first_follow.get_first(g)
    follow = first_follow.get_follow(g, first)
    # len(term) columns, since we also need a column for $ but not one for ""
    parse_table = [[None for j in range(len(g.term))]
                   for i in range(len(g.nonterm))]

    # for rule A -> a (a could be string of terms/nonterms)
    for rule in g.rules:
        row = g.nonterm.index(rule.lhs)
        first_set = []
        for idx, sym in enumerate(rule.rhs):
            if len(first_set) == 0:
                first_set.extend(first[sym])
            else:
                if "" in first[rule.rhs[idx - 1]]:
                    first_set.extend(first[sym])
        # For each term t in First(a) add A->a to M[A,t] (only if t != "")
        for term in first_set:
            col = g.term.index(term)
            if term != "":
                parse_table[row][col] = rule
            # If "" in First(a), for each terminal b in Follow(A),
            #  add A->a to M[A,b]
            else:
                follow_set = follow[rule.lhs]
                for sym in follow_set:
                    if sym == "$":
                        col = -1
                    else:
                        col = g.term.index(sym)
                    parse_table[row][col] = rule

    return parse_table
示例#3
0
    def test_follow1(self):
        expected = {'A': set('$'), 'D': set(['$', 'e']), 'E': set("$")}
        g = grammar.make_dummy_grammar("test_input/test1.cfg")
        first = first_follow.get_first(g)
        got = first_follow.get_follow(g, first)

        self.assertEqual(expected, got)
示例#4
0
def main(fname):
    with open(fname, "r") as infile:
        tokens = scanner.scan(infile, rules)
    tokens.append(scanner.Symbol("$", "EOF", -1, -1, -1))
    #print(tokens)
    g = grammar.Grammar(grammar_dict)

    lr_parse_common.augment_grammar(g)

    for rule in g.rules:
        if rule.to_node is None:
            rule.to_node = lambda rule, children: ast.ASTNode(
                rule.lhs, children)

    kernel = slr1.LR0Item(g.rules[-1], 0)
    first_set = first_follow.get_first(g)
    follow = first_follow.get_follow(g, first_set)
    dfa = lr_parse_common.make_dfa(g, slr1.closure, kernel, first_set)
    action, goto_table = slr1.make_parse_table(dfa, follow, g)
    ast_root = lr_parse_common.parse(dfa, action, goto_table, tokens, g)

    print(ast.gen_ast_digraph(ast_root))

    gen_code = gen_ir.CodeGenVisitor(ast_root)
    gen_code.accept()
    with open(fname + ".ll", "w") as outfile:
        outfile.write(gen_code.get_code())
示例#5
0
    def test_not_slr1_grammar(self):
        g = grammar.make_dummy_grammar("test_input/test4.cfg")
        lr_parse_common.augment_grammar(g)
        first = first_follow.get_first(g)
        follow = first_follow.get_follow(g, first)

        kernel = slr1.LR0Item(g.rules[-1], 0)
        dfa = lr_parse_common.make_dfa(g, slr1.closure, kernel, first)

        with self.assertRaises(lr_parse_common.ShiftReduceError):
            action, goto_table = slr1.make_parse_table(dfa, follow, g)
示例#6
0
def main():
    import scanner
    g = grammar.make_dummy_grammar("test_input/test4.cfg")
    lr_parse_common.augment_grammar(g)
    kernel = LR1Item(g.rules[-1], 0, "$")
    first_set = first_follow.get_first(g)
    follow = first_follow.get_follow(g, first_set)
    dfa = lr_parse_common.make_dfa(g, closure, kernel, first_set)
    print(dfa.generate_dot_file())
    action, goto = make_parse_table(dfa, follow, g)

    tokens = scanner.dummy_tokenize("i=*i$")
    ast_root = lr_parse_common.parse(dfa, action, goto, tokens, g)
    print(ast.gen_ast_digraph(ast_root))
示例#7
0
def main():
    import scanner
    g = grammar.make_dummy_grammar("test_input/test3.cfg")
    lr_parse_common.augment_grammar(g)
    first = first_follow.get_first(g)
    follow = first_follow.get_follow(g, first)

    # the kernel is the new rule just added (by augmenting) with the dist marker at the beginning
    kernel = LR0Item(g.rules[-1], 0)
    dfa = lr_parse_common.make_dfa(g, closure, kernel, first)
    print(dfa.generate_dot_file())
    action, goto_table = make_parse_table(dfa, follow, g)
    print(action)
    print(goto_table)

    tokens = scanner.dummy_tokenize("(a(a((a))))$")
    ast_root = lr_parse_common.parse(dfa, action, goto_table, tokens, g)
    print(ast.gen_ast_digraph(ast_root))
示例#8
0
    def test_make_table(self):
        g = grammar.make_dummy_grammar("test_input/test3.cfg")
        lr_parse_common.augment_grammar(g)
        first = first_follow.get_first(g)
        follow = first_follow.get_follow(g, first)

        kernel = slr1.LR0Item(g.rules[-1], 0)
        dfa = lr_parse_common.make_dfa(g, slr1.closure, kernel, first)

        action, goto_table = slr1.make_parse_table(dfa, follow, g)
        
        expected_action = [
            ['s3', 's4', None, None],
            [None, None, None, 'accept'],
            [None, None, None, 'r0'],
            ['r2', 'r2', 'r2', 'r2'],
            ['s3', 's4', None, None],
            ['s3', 's4', 'r4', None],
            [None, None, 's8', None],
            [None, None, 'r3', None],
            ['r1', 'r1', 'r1', 'r1']
        ]

        self.assertEqual(expected_action, action)

        expected_goto = [
            [1, 2, None, None],
            [None, None, None, None],
            [None, None, None, None],
            [None, None, None, None],
            [None, 5, 6, None],
            [None, 5, 7, None],
            [None, None, None, None],
            [None, None, None, None],
            [None, None, None, None]
        ]

        self.assertEqual(expected_goto, goto_table)