Repository navigation
Expand file tree
/
Copy pathmain.c
More file actions
97 lines (75 loc) · 1.75 KB
/
Copy pathmain.c
File metadata and controls
97 lines (75 loc) · 1.75 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <stdio.h>
#include "stable.h"
#include "gtree.h"
#include "semantic.h"
#include "interm.h"
#include "gencode.h"
extern int yydebug;
char usage[] = "Usage:\n"
"\tcmmc <Source File Name> [[Dest File Name] [Whether to simplify]]\n"
"\tOutput via stdout if destination file not specified.\n"
"\t'no' if simplification not expected, 0-9 for levels.\n"
"\t'whether to simplify' is available iff. Dest File Name is specified, and accessable.\n";
/* Grammer Tree Root */
struct gnode* gtroot;
static int err_status = 0;
inline void set_error_status()
{
err_status = 1;
}
void print_err(int errtype, int lineno, char* msg)
{
printf("Error type %d at line %d: %s\n", errtype, lineno, msg);
set_error_status(); /* error has been found! */
}
int main(int argc, char** argv)
{
int print_reduced;
FILE *f;
FILE *fout; /* stdout default */
if (argc < 2)
{
printf("%s", usage);
return -1;
}
if (argc < 4)
print_reduced = 100;
else if (strcmp(argv[3], "no") == 0)
print_reduced = 0;
else
print_reduced = atoi(argv[3]);
if (!(f = fopen(argv[1], "r")))
return -1;
if (argc < 3 || !(fout = fopen(argv[2], "w")))
fout = NULL;
/* initialization */
init_stable();
/* grammer */
yyrestart(f);
yyparse();
if (gtroot == NULL)
{
printf("Syntax error, program stopped.\n");
return -1;
}
/* semantic analysis */
semantic_parse();
if (err_status) /* Well, semantic error! */
return -1;
/* generate intermediate code! */
im_init(NULL);
interm_parse();
if (!print_reduced)
im_display();
/* reduce the intermediate code! */
im_reduce(print_reduced);
/* display the code! */
/*if (print_reduced)
im_display();*/
gc_init(fout);
gc_generate();
/* close file descriptor */
if (fout)
fclose(fout);
return 0;
}