-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctree.c
232 lines (181 loc) · 5.42 KB
/
ctree.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#define DEFINE_GLOBALS
#include "globals.h"
#undef DEFINE_GLOBALS
#include "ctree.h"
#include "lexer.h"
#include "token.h"
#include "gram_parser.cpp.h"
#include "tree.h"
#include "prnttree.h"
#include "CodeGenerator.h"
/* ############################################################### */
#define VERSION "C-Tree Version 0.14"
/* extern FILE *yyin; */
/* extern int yyparse(); */
char *cur_file;
int show_pcode = 0;
int show_SymbolTable = 0;
int show_AST = 0;
#ifdef USE_CPP
# ifdef USE_GCC_4_CPP
# define LIB_CPP "gcc -E "
# endif
# ifdef USE_CAT_4_CPP
# define LIB_CPP "cat "
# endif
# ifdef USE_CPP_4_CPP
# define LIB_CPP "/lib/cpp "
# endif
# ifndef LIB_CPP
# define LIB_CPP "/lib/cpp "
# endif
#endif /* USE_CPP */
/* ############################################################### */
void Usage(char *prog)
{
fprintf(stderr,"Usage: %s [options] [filename(s)]\n\n",prog);
fprintf(stderr,"This program parses C code, builds a tree and generate Pcode.\n\n");
fprintf(stderr,"Options:\n");
fprintf(stderr,"\t-Pcode:\t\t Show the generated Pcode (code machine).\n");
fprintf(stderr,"\t-AST:\t\t Show Abstract-syntax-tree (AST).\n");
fprintf(stderr,"\t-SymTab:\t Show Symbol Table.\n");
fprintf(stderr,"\t-version:\t Show ctree version (-V).\n");
fprintf(stderr,"\t-help:\t\t Show usage (-h).\n");
exit(0);
}
/* ############################################################### */
int processArgs( int argc, char **argv )
{
int num_files = 0;
char *arg;
char *prog = argv[0];
argc--;
argv++;
while(argc-- > 0) {
arg = *argv++;
if (strcmp(arg, "-Pcode") == 0)
show_pcode = 1;
else if (strcmp(arg, "-AST") == 0)
show_AST = 1;
else if (strcmp(arg, "-SymTab") == 0)
show_SymbolTable = 1;
else if ((strcmp(arg,"-version") == 0)
|| (strcmp(arg,"-V") == 0))
fprintf(stdout, VERSION "\n");
else if ((strcmp(arg,"-help") == 0)
|| (strcmp(arg,"-h") == 0))
Usage(prog);
else
file_list[num_files++] = arg;
}
file_list[num_files] = NULL;
return(num_files);
}
/* ############################################################### */
int main( int argc, char **argv )
{
int i, nf;
FILE *fp;
#ifdef USE_CPP
char cpp_cmmd[500];
char cpp_file[200];
#endif
treenode *parse_tree;
context_t *contxt;
if ((nf = processArgs(argc,argv)) < 0)
return -1;
if (nf == 0)
Usage(argv[0]);
parse_tree = (treenode *) NULL;
init_nmetab();
ParseStack = new_treestk();
DoneStack = new_treestk();
contxt = new_context();
if (!ParseStack || !DoneStack || !contxt) {
delete_treestk(ParseStack);
delete_treestk(DoneStack);
free_context(contxt);
fputs("Out of memory.\n", stderr);
exit(1);
}
ParseStack->contxt = contxt;
for (i=0; i < nf; i++) {
#ifdef USE_CPP
char *end;
#endif
cur_file = file_list[i];
#ifdef USE_CPP
strcpy(cpp_file, file_list[i]);
end = strrchr(cpp_file,'.');
if (!end) {
fprintf(stderr, "No '.' found in filename '%s'\n", cur_file);
continue;
}
sprintf(end, "_pp");
/*
willey
need to redirect output to file if using GCC, not give target file as final
argument, as in cpp
*/
#ifdef USE_GCC_4_CPP
sprintf(cpp_cmmd,"%s -DCTREE %s > %s",LIB_CPP, file_list[i], cpp_file);
#elif defined(USE_CAT_4_CPP)
sprintf(cpp_cmmd,"%s %s > %s", LIB_CPP, file_list[i], cpp_file);
#else
sprintf(cpp_cmmd, "%s -DCTREE %s %s", LIB_CPP, file_list[i], cpp_file);
#endif
/*
willey
debugging line to see what preprocessor is being called
*/
#ifdef DEBUG
fprintf(stderr,"%s\n",cpp_cmmd);
#endif
system(cpp_cmmd);
if ((fp = fopen(cpp_file,"r")) == NULL)
return(1);
#else
if ((fp = fopen(cur_file,"r")) == NULL)
return(1);
#endif
handle_new_file(ParseStack, fp, file_list[i]);
enter_scope(contxt);
tree_parse(ParseStack, 0);
parse_tree = (top_of_stack(DoneStack))->parse_tree;
#ifdef USE_CPP
if (remove(cpp_file)) {
fprintf(stderr, "Remove of file '%s' failed.\n", cpp_file);
}
#endif
if (parse_tree)
{
/* Show data */
if(show_pcode)
print_pcode(parse_tree,stdout);
else if(show_AST)
show_tree(parse_tree,stdout);
else if (show_SymbolTable)
print_symbol_table(parse_tree);
parse_tree = (treenode *) NULL;
}
else
{
fputs(" parse_tree is NULL\n", stdout);
}
exit_scope(contxt);
while (! is_empty(ParseStack))
delete_stk_item(pop(ParseStack));
while (! is_empty(DoneStack))
delete_stk_item(Parse_TOS = pop(DoneStack));
}
delete_treestk(ParseStack);
delete_treestk(DoneStack);
free_context(contxt);
free_nmetab();
return(0);
}
/* ############################################################### */