1 | #include <sys/types.h>
|
---|
2 | #include <signal.h>
|
---|
3 | #include "defs.h"
|
---|
4 |
|
---|
5 | char dflag;
|
---|
6 | char lflag;
|
---|
7 | char rflag;
|
---|
8 | char tflag;
|
---|
9 | char vflag;
|
---|
10 |
|
---|
11 | char *symbol_prefix;
|
---|
12 | char *file_prefix = "y";
|
---|
13 | char *myname = "yacc";
|
---|
14 | char *temp_form = "yacc.XXXXXXX";
|
---|
15 |
|
---|
16 | int lineno;
|
---|
17 | int outline;
|
---|
18 |
|
---|
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;
|
---|
27 |
|
---|
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 */
|
---|
40 |
|
---|
41 | int nitems;
|
---|
42 | int nrules;
|
---|
43 | int nsyms;
|
---|
44 | int ntokens;
|
---|
45 | int nvars;
|
---|
46 |
|
---|
47 | int start_symbol;
|
---|
48 | char **symbol_name;
|
---|
49 | short *symbol_value;
|
---|
50 | short *symbol_prec;
|
---|
51 | char *symbol_assoc;
|
---|
52 |
|
---|
53 | short *ritem;
|
---|
54 | short *rlhs;
|
---|
55 | short *rrhs;
|
---|
56 | short *rprec;
|
---|
57 | char *rassoc;
|
---|
58 | short **derives;
|
---|
59 | char *nullable;
|
---|
60 |
|
---|
61 | extern char *mktemp();
|
---|
62 | extern char *getenv();
|
---|
63 |
|
---|
64 |
|
---|
65 | done(k)
|
---|
66 | int k;
|
---|
67 | {
|
---|
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);
|
---|
72 | }
|
---|
73 |
|
---|
74 |
|
---|
75 | void
|
---|
76 | onintr(signo)
|
---|
77 | int signo;
|
---|
78 | {
|
---|
79 | done(1);
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | set_signals()
|
---|
84 | {
|
---|
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
|
---|
97 | }
|
---|
98 |
|
---|
99 |
|
---|
100 | usage()
|
---|
101 | {
|
---|
102 | fprintf(stderr, "usage: %s [-dlrtv] [-b file_prefix] [-p symbol_prefix] filename\n", myname);
|
---|
103 | exit(1);
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | getargs(argc, argv)
|
---|
108 | int argc;
|
---|
109 | char *argv[];
|
---|
110 | {
|
---|
111 | register int i;
|
---|
112 | register char *s;
|
---|
113 |
|
---|
114 | if (argc > 0) myname = argv[0];
|
---|
115 | for (i = 1; i < argc; ++i)
|
---|
116 | {
|
---|
117 | s = argv[i];
|
---|
118 | if (*s != '-') break;
|
---|
119 | switch (*++s)
|
---|
120 | {
|
---|
121 | case '\0':
|
---|
122 | input_file = stdin;
|
---|
123 | if (i + 1 < argc) usage();
|
---|
124 | return;
|
---|
125 |
|
---|
126 | case '-':
|
---|
127 | ++i;
|
---|
128 | goto no_more_options;
|
---|
129 |
|
---|
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;
|
---|
138 |
|
---|
139 | case 'd':
|
---|
140 | dflag = 1;
|
---|
141 | break;
|
---|
142 |
|
---|
143 | case 'l':
|
---|
144 | lflag = 1;
|
---|
145 | break;
|
---|
146 |
|
---|
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;
|
---|
155 |
|
---|
156 | case 'r':
|
---|
157 | rflag = 1;
|
---|
158 | break;
|
---|
159 |
|
---|
160 | case 't':
|
---|
161 | tflag = 1;
|
---|
162 | break;
|
---|
163 |
|
---|
164 | case 'v':
|
---|
165 | vflag = 1;
|
---|
166 | break;
|
---|
167 |
|
---|
168 | default:
|
---|
169 | usage();
|
---|
170 | }
|
---|
171 |
|
---|
172 | for (;;)
|
---|
173 | {
|
---|
174 | switch (*++s)
|
---|
175 | {
|
---|
176 | case '\0':
|
---|
177 | goto end_of_option;
|
---|
178 |
|
---|
179 | case 'd':
|
---|
180 | dflag = 1;
|
---|
181 | break;
|
---|
182 |
|
---|
183 | case 'l':
|
---|
184 | lflag = 1;
|
---|
185 | break;
|
---|
186 |
|
---|
187 | case 'r':
|
---|
188 | rflag = 1;
|
---|
189 | break;
|
---|
190 |
|
---|
191 | case 't':
|
---|
192 | tflag = 1;
|
---|
193 | break;
|
---|
194 |
|
---|
195 | case 'v':
|
---|
196 | vflag = 1;
|
---|
197 | break;
|
---|
198 |
|
---|
199 | default:
|
---|
200 | usage();
|
---|
201 | }
|
---|
202 | }
|
---|
203 | end_of_option:;
|
---|
204 | }
|
---|
205 |
|
---|
206 | no_more_options:;
|
---|
207 | if (i + 1 != argc) usage();
|
---|
208 | input_file_name = argv[i];
|
---|
209 | }
|
---|
210 |
|
---|
211 |
|
---|
212 | char *
|
---|
213 | allocate(n)
|
---|
214 | unsigned n;
|
---|
215 | {
|
---|
216 | register char *p;
|
---|
217 |
|
---|
218 | p = NULL;
|
---|
219 | if (n)
|
---|
220 | {
|
---|
221 | p = CALLOC(1, n);
|
---|
222 | if (!p) no_space();
|
---|
223 | }
|
---|
224 | return (p);
|
---|
225 | }
|
---|
226 |
|
---|
227 |
|
---|
228 | create_file_names()
|
---|
229 | {
|
---|
230 | int i, len;
|
---|
231 | char *tmpdir;
|
---|
232 |
|
---|
233 | tmpdir = getenv("TMPDIR");
|
---|
234 | if (tmpdir == 0) tmpdir = "/tmp";
|
---|
235 |
|
---|
236 | len = strlen(tmpdir);
|
---|
237 | i = len + 13;
|
---|
238 | if (len && tmpdir[len-1] != '/')
|
---|
239 | ++i;
|
---|
240 |
|
---|
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();
|
---|
247 |
|
---|
248 | strcpy(action_file_name, tmpdir);
|
---|
249 | strcpy(text_file_name, tmpdir);
|
---|
250 | strcpy(union_file_name, tmpdir);
|
---|
251 |
|
---|
252 | if (len && tmpdir[len - 1] != '/')
|
---|
253 | {
|
---|
254 | action_file_name[len] = '/';
|
---|
255 | text_file_name[len] = '/';
|
---|
256 | union_file_name[len] = '/';
|
---|
257 | ++len;
|
---|
258 | }
|
---|
259 |
|
---|
260 | strcpy(action_file_name + len, temp_form);
|
---|
261 | strcpy(text_file_name + len, temp_form);
|
---|
262 | strcpy(union_file_name + len, temp_form);
|
---|
263 |
|
---|
264 | action_file_name[len + 5] = 'a';
|
---|
265 | text_file_name[len + 5] = 't';
|
---|
266 | union_file_name[len + 5] = 'u';
|
---|
267 |
|
---|
268 | mktemp(action_file_name);
|
---|
269 | mktemp(text_file_name);
|
---|
270 | mktemp(union_file_name);
|
---|
271 |
|
---|
272 | len = strlen(file_prefix);
|
---|
273 |
|
---|
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);
|
---|
279 |
|
---|
280 | if (rflag)
|
---|
281 | {
|
---|
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);
|
---|
287 | }
|
---|
288 | else
|
---|
289 | code_file_name = output_file_name;
|
---|
290 |
|
---|
291 | if (dflag)
|
---|
292 | {
|
---|
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);
|
---|
298 | }
|
---|
299 |
|
---|
300 | if (vflag)
|
---|
301 | {
|
---|
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);
|
---|
307 | }
|
---|
308 | }
|
---|
309 |
|
---|
310 |
|
---|
311 | open_files()
|
---|
312 | {
|
---|
313 | create_file_names();
|
---|
314 |
|
---|
315 | if (input_file == 0)
|
---|
316 | {
|
---|
317 | input_file = fopen(input_file_name, "r");
|
---|
318 | if (input_file == 0)
|
---|
319 | open_error(input_file_name);
|
---|
320 | }
|
---|
321 |
|
---|
322 | action_file = fopen(action_file_name, "w");
|
---|
323 | if (action_file == 0)
|
---|
324 | open_error(action_file_name);
|
---|
325 |
|
---|
326 | text_file = fopen(text_file_name, "w");
|
---|
327 | if (text_file == 0)
|
---|
328 | open_error(text_file_name);
|
---|
329 |
|
---|
330 | if (vflag)
|
---|
331 | {
|
---|
332 | verbose_file = fopen(verbose_file_name, "w");
|
---|
333 | if (verbose_file == 0)
|
---|
334 | open_error(verbose_file_name);
|
---|
335 | }
|
---|
336 |
|
---|
337 | if (dflag)
|
---|
338 | {
|
---|
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);
|
---|
345 | }
|
---|
346 |
|
---|
347 | output_file = fopen(output_file_name, "w");
|
---|
348 | if (output_file == 0)
|
---|
349 | open_error(output_file_name);
|
---|
350 |
|
---|
351 | if (rflag)
|
---|
352 | {
|
---|
353 | code_file = fopen(code_file_name, "w");
|
---|
354 | if (code_file == 0)
|
---|
355 | open_error(code_file_name);
|
---|
356 | }
|
---|
357 | else
|
---|
358 | code_file = output_file;
|
---|
359 | }
|
---|
360 |
|
---|
361 |
|
---|
362 | int
|
---|
363 | main(argc, argv)
|
---|
364 | int argc;
|
---|
365 | char *argv[];
|
---|
366 | {
|
---|
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*/
|
---|
378 | }
|
---|