Typo's.
[minix3.git] / commands / byacc / main.c
blobe6d0d7eadd0b74aa0a60def8ad912ad2ca23fb91
1 #include <sys/types.h>
2 #include <signal.h>
3 #include "defs.h"
5 char dflag;
6 char lflag;
7 char rflag;
8 char tflag;
9 char vflag;
11 char *symbol_prefix;
12 char *file_prefix = "y";
13 char *myname = "yacc";
14 char *temp_form = "yacc.XXXXXXX";
16 int lineno;
17 int outline;
19 char *action_file_name;
20 char *code_file_name;
21 char *defines_file_name;
22 char *input_file_name = "";
23 char *output_file_name;
24 char *text_file_name;
25 char *union_file_name;
26 char *verbose_file_name;
28 FILE *action_file; /* a temp file, used to save actions associated */
29 /* with rules until the parser is written */
30 FILE *code_file; /* y.code.c (used when the -r option is specified) */
31 FILE *defines_file; /* y.tab.h */
32 FILE *input_file; /* the input file */
33 FILE *output_file; /* y.tab.c */
34 FILE *text_file; /* a temp file, used to save text until all */
35 /* symbols have been defined */
36 FILE *union_file; /* a temp file, used to save the union */
37 /* definition until all symbol have been */
38 /* defined */
39 FILE *verbose_file; /* y.output */
41 int nitems;
42 int nrules;
43 int nsyms;
44 int ntokens;
45 int nvars;
47 int start_symbol;
48 char **symbol_name;
49 short *symbol_value;
50 short *symbol_prec;
51 char *symbol_assoc;
53 short *ritem;
54 short *rlhs;
55 short *rrhs;
56 short *rprec;
57 char *rassoc;
58 short **derives;
59 char *nullable;
61 extern char *mktemp();
62 extern char *getenv();
65 done(k)
66 int k;
68 if (action_file) { fclose(action_file); unlink(action_file_name); }
69 if (text_file) { fclose(text_file); unlink(text_file_name); }
70 if (union_file) { fclose(union_file); unlink(union_file_name); }
71 exit(k);
75 void
76 onintr(signo)
77 int signo;
79 done(1);
83 set_signals()
85 #ifdef SIGINT
86 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
87 signal(SIGINT, onintr);
88 #endif
89 #ifdef SIGTERM
90 if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
91 signal(SIGTERM, onintr);
92 #endif
93 #ifdef SIGHUP
94 if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
95 signal(SIGHUP, onintr);
96 #endif
100 usage()
102 fprintf(stderr, "usage: %s [-dlrtv] [-b file_prefix] [-p symbol_prefix] filename\n", myname);
103 exit(1);
107 getargs(argc, argv)
108 int argc;
109 char *argv[];
111 register int i;
112 register char *s;
114 if (argc > 0) myname = argv[0];
115 for (i = 1; i < argc; ++i)
117 s = argv[i];
118 if (*s != '-') break;
119 switch (*++s)
121 case '\0':
122 input_file = stdin;
123 if (i + 1 < argc) usage();
124 return;
126 case '-':
127 ++i;
128 goto no_more_options;
130 case 'b':
131 if (*++s)
132 file_prefix = s;
133 else if (++i < argc)
134 file_prefix = argv[i];
135 else
136 usage();
137 continue;
139 case 'd':
140 dflag = 1;
141 break;
143 case 'l':
144 lflag = 1;
145 break;
147 case 'p':
148 if (*++s)
149 symbol_prefix = s;
150 else if (++i < argc)
151 symbol_prefix = argv[i];
152 else
153 usage();
154 continue;
156 case 'r':
157 rflag = 1;
158 break;
160 case 't':
161 tflag = 1;
162 break;
164 case 'v':
165 vflag = 1;
166 break;
168 default:
169 usage();
172 for (;;)
174 switch (*++s)
176 case '\0':
177 goto end_of_option;
179 case 'd':
180 dflag = 1;
181 break;
183 case 'l':
184 lflag = 1;
185 break;
187 case 'r':
188 rflag = 1;
189 break;
191 case 't':
192 tflag = 1;
193 break;
195 case 'v':
196 vflag = 1;
197 break;
199 default:
200 usage();
203 end_of_option:;
206 no_more_options:;
207 if (i + 1 != argc) usage();
208 input_file_name = argv[i];
212 char *
213 allocate(n)
214 unsigned n;
216 register char *p;
218 p = NULL;
219 if (n)
221 p = CALLOC(1, n);
222 if (!p) no_space();
224 return (p);
228 create_file_names()
230 int i, len;
231 char *tmpdir;
233 tmpdir = getenv("TMPDIR");
234 if (tmpdir == 0) tmpdir = "/tmp";
236 len = strlen(tmpdir);
237 i = len + 13;
238 if (len && tmpdir[len-1] != '/')
239 ++i;
241 action_file_name = MALLOC(i);
242 if (action_file_name == 0) no_space();
243 text_file_name = MALLOC(i);
244 if (text_file_name == 0) no_space();
245 union_file_name = MALLOC(i);
246 if (union_file_name == 0) no_space();
248 strcpy(action_file_name, tmpdir);
249 strcpy(text_file_name, tmpdir);
250 strcpy(union_file_name, tmpdir);
252 if (len && tmpdir[len - 1] != '/')
254 action_file_name[len] = '/';
255 text_file_name[len] = '/';
256 union_file_name[len] = '/';
257 ++len;
260 strcpy(action_file_name + len, temp_form);
261 strcpy(text_file_name + len, temp_form);
262 strcpy(union_file_name + len, temp_form);
264 action_file_name[len + 5] = 'a';
265 text_file_name[len + 5] = 't';
266 union_file_name[len + 5] = 'u';
268 mktemp(action_file_name);
269 mktemp(text_file_name);
270 mktemp(union_file_name);
272 len = strlen(file_prefix);
274 output_file_name = MALLOC(len + 7);
275 if (output_file_name == 0)
276 no_space();
277 strcpy(output_file_name, file_prefix);
278 strcpy(output_file_name + len, OUTPUT_SUFFIX);
280 if (rflag)
282 code_file_name = MALLOC(len + 8);
283 if (code_file_name == 0)
284 no_space();
285 strcpy(code_file_name, file_prefix);
286 strcpy(code_file_name + len, CODE_SUFFIX);
288 else
289 code_file_name = output_file_name;
291 if (dflag)
293 defines_file_name = MALLOC(len + 7);
294 if (defines_file_name == 0)
295 no_space();
296 strcpy(defines_file_name, file_prefix);
297 strcpy(defines_file_name + len, DEFINES_SUFFIX);
300 if (vflag)
302 verbose_file_name = MALLOC(len + 8);
303 if (verbose_file_name == 0)
304 no_space();
305 strcpy(verbose_file_name, file_prefix);
306 strcpy(verbose_file_name + len, VERBOSE_SUFFIX);
311 open_files()
313 create_file_names();
315 if (input_file == 0)
317 input_file = fopen(input_file_name, "r");
318 if (input_file == 0)
319 open_error(input_file_name);
322 action_file = fopen(action_file_name, "w");
323 if (action_file == 0)
324 open_error(action_file_name);
326 text_file = fopen(text_file_name, "w");
327 if (text_file == 0)
328 open_error(text_file_name);
330 if (vflag)
332 verbose_file = fopen(verbose_file_name, "w");
333 if (verbose_file == 0)
334 open_error(verbose_file_name);
337 if (dflag)
339 defines_file = fopen(defines_file_name, "w");
340 if (defines_file == 0)
341 open_error(defines_file_name);
342 union_file = fopen(union_file_name, "w");
343 if (union_file == 0)
344 open_error(union_file_name);
347 output_file = fopen(output_file_name, "w");
348 if (output_file == 0)
349 open_error(output_file_name);
351 if (rflag)
353 code_file = fopen(code_file_name, "w");
354 if (code_file == 0)
355 open_error(code_file_name);
357 else
358 code_file = output_file;
363 main(argc, argv)
364 int argc;
365 char *argv[];
367 set_signals();
368 getargs(argc, argv);
369 open_files();
370 reader();
371 lr0();
372 lalr();
373 make_parser();
374 verbose();
375 output();
376 done(0);
377 /*NOTREACHED*/