-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
69 lines (55 loc) · 1.2 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "cmm-strtab.h"
#include "cmm-symtab.h"
#include "node.h"
#include "asm.h"
#include <string.h>
// from syntax.tab.c
int yyrestart(FILE *);
int yyparse();
void ast();
void semantic_analysis();
void free_ast();
void translate();
extern int is_syn_error;
extern bool semantic_error;
int main(int argc, char *argv[])
{
/* Need arguments. */
if (argc <= 2) {
printf("Usage: %s infile outfile\n", argv[0]);
return 1;
}
// ./cc src.cmm out.s
FILE *file;
file = fopen(argv[1], "r");
asm_file = fopen(argv[2], "w");
if (!file) {
perror(argv[1]);
return 1;
}
else if (!asm_file) {
perror(argv[2]);
return 1;
}
/* Initialize strtab: a tab storaging all the strings */
init_strtab();
/* Lexical analysis */
yyrestart(file);
yyparse();
/* Syntax analysis */
if (!is_syn_error) {
/* Semantic analysis */
semantic_analysis();
if (!semantic_error) {
/* Generate asm codes */
translate();
}
} else {
/* Syntax error */
free_ast();
return 2;
}
/* Release the parsing tree. */
free_ast();
return 0;
}