Berkeley YACC  1993-03-03
Berkeley's version of Yet Another Compiler Compiler
 All Data Structures Files Functions Variables Typedefs Macros Groups
main.c
Go to the documentation of this file.
1 #include <signal.h>
2 #include "defs.h"
3 
4 char dflag;
5 char lflag;
6 char rflag;
7 char tflag;
8 char vflag;
9 
11 char *file_prefix = "y";
12 char *myname = "yacc";
13 char *temp_form = "yacc.XXXXXXX";
14 
15 int lineno;
16 int outline;
17 
21 char *input_file_name = "";
26 
27 FILE *action_file; /* a temp file, used to save actions associated */
28  /* with rules until the parser is written */
29 FILE *code_file; /* y.code.c (used when the -r option is specified) */
30 FILE *defines_file; /* y.tab.h */
31 FILE *input_file; /* the input file */
32 FILE *output_file; /* y.tab.c */
33 FILE *text_file; /* a temp file, used to save text until all */
34  /* symbols have been defined */
35 FILE *union_file; /* a temp file, used to save the union */
36  /* definition until all symbol have been */
37  /* defined */
38 FILE *verbose_file; /* y.output */
39 
40 int nitems;
41 
45 int nrules;
46 
55 int nsyms;
56 
64 int ntokens;
65 
74 int nvars;
75 
83 
90 char **symbol_name;
91 short *symbol_value;
92 short *symbol_prec;
94 
112 short *ritem;
113 
119 short *rlhs;
120 
126 short *rrhs;
127 short *rprec;
128 char *rassoc;
129 
140 short **derives;
141 char *nullable;
142 
143 extern char *mktemp();
144 extern char *getenv();
145 
146 
157 int k;
158 {
159  if (action_file) { fclose(action_file); unlink(action_file_name); }
160  if (text_file) { fclose(text_file); unlink(text_file_name); }
161  if (union_file) { fclose(union_file); unlink(union_file_name); }
162  exit(k);
163 }
164 
173 {
174  done(1);
175 }
176 
177 
186 {
187 #ifdef SIGINT
188  if (signal(SIGINT, SIG_IGN) != SIG_IGN)
189  signal(SIGINT, onintr);
190 #endif
191 #ifdef SIGTERM
192  if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
193  signal(SIGTERM, onintr);
194 #endif
195 #ifdef SIGHUP
196  if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
197  signal(SIGHUP, onintr);
198 #endif
199 }
200 
201 
203 {
204  fprintf(stderr, "usage: %s [-dlrtv] [-b file_prefix] [-p symbol_prefix] filename\n", myname);
205  exit(1);
206 }
207 
208 
209 getargs(argc, argv)
210 int argc;
211 char *argv[];
212 {
213  register int i;
214  register char *s;
215 
216  if (argc > 0) myname = argv[0];
217  for (i = 1; i < argc; ++i)
218  {
219  s = argv[i];
220  if (*s != '-') break;
221  switch (*++s)
222  {
223  case '\0':
224  input_file = stdin;
225  if (i + 1 < argc) usage();
226  return;
227 
228  case '-':
229  ++i;
230  goto no_more_options;
231 
232  case 'b':
233  if (*++s)
234  file_prefix = s;
235  else if (++i < argc)
236  file_prefix = argv[i];
237  else
238  usage();
239  continue;
240 
241  case 'd':
242  dflag = 1;
243  break;
244 
245  case 'l':
246  lflag = 1;
247  break;
248 
249  case 'p':
250  if (*++s)
251  symbol_prefix = s;
252  else if (++i < argc)
253  symbol_prefix = argv[i];
254  else
255  usage();
256  continue;
257 
258  case 'r':
259  rflag = 1;
260  break;
261 
262  case 't':
263  tflag = 1;
264  break;
265 
266  case 'v':
267  vflag = 1;
268  break;
269 
270  default:
271  usage();
272  }
273 
274  for (;;)
275  {
276  switch (*++s)
277  {
278  case '\0':
279  goto end_of_option;
280 
281  case 'd':
282  dflag = 1;
283  break;
284 
285  case 'l':
286  lflag = 1;
287  break;
288 
289  case 'r':
290  rflag = 1;
291  break;
292 
293  case 't':
294  tflag = 1;
295  break;
296 
297  case 'v':
298  vflag = 1;
299  break;
300 
301  default:
302  usage();
303  }
304  }
305 end_of_option:;
306  }
307 
308 no_more_options:;
309  if (i + 1 != argc) usage();
310  input_file_name = argv[i];
311 }
312 
313 
314 char *
316 unsigned n;
317 {
318  register char *p;
319 
320  p = NULL;
321  if (n)
322  {
323  p = CALLOC(1, n);
324  if (!p) no_space();
325  }
326  return (p);
327 }
328 
329 
331 {
332  int i, len;
333  char *tmpdir;
334 
335  tmpdir = getenv("TMPDIR");
336  if (tmpdir == 0) tmpdir = "/tmp";
337 
338  len = strlen(tmpdir);
339  i = len + 13;
340  if (len && tmpdir[len-1] != '/')
341  ++i;
342 
344  if (action_file_name == 0) no_space();
345  text_file_name = MALLOC(i);
346  if (text_file_name == 0) no_space();
347  union_file_name = MALLOC(i);
348  if (union_file_name == 0) no_space();
349 
350  strcpy(action_file_name, tmpdir);
351  strcpy(text_file_name, tmpdir);
352  strcpy(union_file_name, tmpdir);
353 
354  if (len && tmpdir[len - 1] != '/')
355  {
356  action_file_name[len] = '/';
357  text_file_name[len] = '/';
358  union_file_name[len] = '/';
359  ++len;
360  }
361 
365 
366  action_file_name[len + 5] = 'a';
367  text_file_name[len + 5] = 't';
368  union_file_name[len + 5] = 'u';
369 
373 
374  len = strlen(file_prefix);
375 
376  output_file_name = MALLOC(len + 7);
377  if (output_file_name == 0)
378  no_space();
381 
382  if (rflag)
383  {
384  code_file_name = MALLOC(len + 8);
385  if (code_file_name == 0)
386  no_space();
389  }
390  else
392 
393  if (dflag)
394  {
395  defines_file_name = MALLOC(len + 7);
396  if (defines_file_name == 0)
397  no_space();
400  }
401 
402  if (vflag)
403  {
404  verbose_file_name = MALLOC(len + 8);
405  if (verbose_file_name == 0)
406  no_space();
409  }
410 }
411 
412 
414 {
416 
417  if (input_file == 0)
418  {
419  input_file = fopen(input_file_name, "r");
420  if (input_file == 0)
422  }
423 
424  action_file = fopen(action_file_name, "w");
425  if (action_file == 0)
427 
428  text_file = fopen(text_file_name, "w");
429  if (text_file == 0)
431 
432  if (vflag)
433  {
434  verbose_file = fopen(verbose_file_name, "w");
435  if (verbose_file == 0)
437  }
438 
439  if (dflag)
440  {
441  defines_file = fopen(defines_file_name, "w");
442  if (defines_file == 0)
444  union_file = fopen(union_file_name, "w");
445  if (union_file == 0)
447  }
448 
449  output_file = fopen(output_file_name, "w");
450  if (output_file == 0)
452 
453  if (rflag)
454  {
455  code_file = fopen(code_file_name, "w");
456  if (code_file == 0)
458  }
459  else
461 }
462 
463 
464 int
465 main(argc, argv)
466 int argc;
467 char *argv[];
468 {
469  set_signals();
470  getargs(argc, argv);
471  open_files();
472  reader();
473  lr0();
474  lalr();
475  make_parser();
476  verbose();
477  output();
478  done(0);
479  /*NOTREACHED*/
480 }
char * file_prefix
Definition: main.c:11
short ** derives
List of rules that derive each non-terminal.
Definition: main.c:140
#define VERBOSE_SUFFIX
Definition: defs.h:51
int start_symbol
Index of the starting symbol of the grammar.
Definition: main.c:82
int outline
Definition: main.c:16
lalr()
Definition: lalr.c:37
FILE * code_file
Definition: main.c:29
char * symbol_assoc
Definition: main.c:93
create_file_names()
Definition: main.c:330
open_error(char *filename)
Definition: error.c:34
char * defines_file_name
Definition: main.c:20
FILE * text_file
Definition: main.c:33
#define OUTPUT_SUFFIX
Definition: defs.h:50
#define MALLOC(n)
Definition: defs.h:103
char * output_file_name
Definition: main.c:22
char * action_file_name
Definition: main.c:18
onintr()
Signal handler callback.
Definition: main.c:172
char * symbol_prefix
Definition: main.c:10
char * code_file_name
Definition: main.c:19
char lflag
Definition: main.c:5
char * input_file_name
Definition: main.c:21
int main(int argc, argv)
Definition: main.c:465
char * rassoc
Definition: main.c:128
char tflag
Definition: main.c:7
char rflag
Definition: main.c:6
short * rlhs
List of left-hand sides of all rules.
Definition: main.c:119
char * text_file_name
Definition: main.c:23
char * union_file_name
Definition: main.c:24
int lineno
Definition: main.c:15
char * nullable
Definition: main.c:141
set_signals()
Sets the appropriate signal handlers up.
Definition: main.c:185
short * symbol_prec
Definition: main.c:92
done(int k)
Shutdown function.
Definition: main.c:156
usage()
Definition: main.c:202
lr0()
Definition: lr0.c:593
FILE * verbose_file
Definition: main.c:38
char * temp_form
Definition: main.c:13
getargs(int argc, argv)
Definition: main.c:209
int ntokens
The number of tokens (terminals) in the grammar.
Definition: main.c:64
int nitems
Definition: main.c:40
#define CALLOC(k, n)
Definition: defs.h:101
verbose()
Definition: verbose.c:7
short * rrhs
List of right-hand sides of all rules.
Definition: main.c:126
FILE * action_file
Definition: main.c:27
no_space()
Definition: error.c:27
open_files()
Definition: main.c:413
char * allocate(unsigned n)
Definition: main.c:315
make_parser()
Definition: mkpar.c:23
output()
Definition: output.c:20
int nrules
The number of rules in the grammar.
Definition: main.c:45
short * symbol_value
Definition: main.c:91
FILE * union_file
Definition: main.c:35
char * getenv()
char * mktemp()
char vflag
Definition: main.c:8
char * myname
Definition: main.c:12
FILE * defines_file
Definition: main.c:30
char * verbose_file_name
Definition: main.c:25
FILE * input_file
Definition: main.c:31
short * ritem
Representation of all productions (and items)
Definition: main.c:112
char * strcpy()
#define CODE_SUFFIX
Definition: defs.h:48
FILE * output_file
Definition: main.c:32
reader()
Definition: reader.c:1775
char dflag
Definition: main.c:4
short * rprec
Definition: main.c:127
char ** symbol_name
Array of symbol names.
Definition: main.c:90
int nvars
The number of variables (non-terminals) in the grammar.
Definition: main.c:74
int nsyms
The number of symbols (terminals + non-terminals) in the grammar.
Definition: main.c:55
#define DEFINES_SUFFIX
Definition: defs.h:49