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())
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)
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))
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))
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)