Home | History | Annotate | Line # | Download | only in dist
main.c revision 1.1.1.14
      1 /*	$NetBSD: main.c,v 1.1.1.14 2024/09/14 21:25:37 christos Exp $	*/
      2 
      3 /* Id: main.c,v 1.74 2023/05/11 07:51:36 tom Exp  */
      4 
      5 #include <signal.h>
      6 #if !defined(_WIN32) || defined(__MINGW32__)
      7 #include <unistd.h>		/* for _exit() */
      8 #else
      9 #include <stdlib.h>		/* for _exit() */
     10 #endif
     11 
     12 #include "defs.h"
     13 
     14 #ifdef HAVE_MKSTEMP
     15 # define USE_MKSTEMP 1
     16 #elif defined(HAVE_FCNTL_H)
     17 # define USE_MKSTEMP 1
     18 # include <fcntl.h>		/* for open(), O_EXCL, etc. */
     19 #else
     20 # define USE_MKSTEMP 0
     21 #endif
     22 
     23 #if USE_MKSTEMP
     24 #include <sys/types.h>
     25 #include <sys/stat.h>
     26 
     27 typedef struct _my_tmpfiles
     28 {
     29     struct _my_tmpfiles *next;
     30     char *name;
     31 }
     32 MY_TMPFILES;
     33 
     34 static MY_TMPFILES *my_tmpfiles;
     35 #endif /* USE_MKSTEMP */
     36 
     37 char dflag;
     38 char dflag2;
     39 char gflag;
     40 char iflag;
     41 char lflag;
     42 static char oflag;
     43 char rflag;
     44 char sflag;
     45 char tflag;
     46 char vflag;
     47 
     48 const char *symbol_prefix;
     49 const char *myname = "yacc";
     50 
     51 int lineno;
     52 int outline;
     53 
     54 static char default_file_prefix[] = "y";
     55 
     56 static char *file_prefix = default_file_prefix;
     57 
     58 char *code_file_name;
     59 char *input_file_name;
     60 size_t input_file_name_len = 0;
     61 char *defines_file_name;
     62 char *externs_file_name;
     63 
     64 static char *graph_file_name;
     65 static char *output_file_name;
     66 static char *verbose_file_name;
     67 
     68 FILE *action_file;	/*  a temp file, used to save actions associated    */
     69 			/*  with rules until the parser is written          */
     70 FILE *code_file;	/*  y.code.c (used when the -r option is specified) */
     71 FILE *defines_file;	/*  y.tab.h                                         */
     72 FILE *externs_file;	/*  y.tab.i                                         */
     73 FILE *input_file;	/*  the input file                                  */
     74 FILE *output_file;	/*  y.tab.c                                         */
     75 FILE *text_file;	/*  a temp file, used to save text until all        */
     76 			/*  symbols have been defined                       */
     77 FILE *union_file;	/*  a temp file, used to save the union             */
     78 			/*  definition until all symbol have been           */
     79 			/*  defined                                         */
     80 FILE *verbose_file;	/*  y.output                                        */
     81 FILE *graph_file;	/*  y.dot                                           */
     82 
     83 Value_t nitems;
     84 Value_t nrules;
     85 Value_t nsyms;
     86 Value_t ntokens;
     87 Value_t nvars;
     88 
     89 Value_t start_symbol;
     90 char **symbol_name;
     91 char **symbol_pname;
     92 Value_t *symbol_value;
     93 Value_t *symbol_prec;
     94 char *symbol_assoc;
     95 
     96 int pure_parser;
     97 int token_table;
     98 int error_verbose;
     99 
    100 #if defined(YYBTYACC)
    101 Value_t *symbol_pval;
    102 char **symbol_destructor;
    103 char **symbol_type_tag;
    104 int locations = 0;	/* default to no position processing */
    105 int backtrack = 0;	/* default is no backtracking */
    106 char *initial_action = NULL;
    107 #endif
    108 
    109 int exit_code;
    110 
    111 Value_t *ritem;
    112 Value_t *rlhs;
    113 Value_t *rrhs;
    114 Value_t *rprec;
    115 Assoc_t *rassoc;
    116 Value_t **derives;
    117 char *nullable;
    118 
    119 /*
    120  * Since fclose() is called via the signal handler, it might die.  Don't loop
    121  * if there is a problem closing a file.
    122  */
    123 #define DO_CLOSE(fp) \
    124 	if (fp != 0) { \
    125 	    FILE *use = fp; \
    126 	    fp = 0; \
    127 	    fclose(use); \
    128 	}
    129 
    130 static int got_intr = 0;
    131 
    132 void
    133 done(int k)
    134 {
    135     DO_CLOSE(input_file);
    136     DO_CLOSE(output_file);
    137     if (iflag)
    138 	DO_CLOSE(externs_file);
    139     if (rflag)
    140 	DO_CLOSE(code_file);
    141 
    142     DO_CLOSE(action_file);
    143     DO_CLOSE(defines_file);
    144     DO_CLOSE(graph_file);
    145     DO_CLOSE(text_file);
    146     DO_CLOSE(union_file);
    147     DO_CLOSE(verbose_file);
    148 
    149     if (got_intr)
    150 	_exit(EXIT_FAILURE);
    151 
    152 #ifdef NO_LEAKS
    153     DO_FREE(input_file_name);
    154 
    155     if (rflag)
    156 	DO_FREE(code_file_name);
    157 
    158     if (dflag && !dflag2)
    159 	DO_FREE(defines_file_name);
    160 
    161     if (iflag)
    162 	DO_FREE(externs_file_name);
    163 
    164     if (oflag)
    165 	DO_FREE(output_file_name);
    166 
    167     if (vflag)
    168 	DO_FREE(verbose_file_name);
    169 
    170     if (gflag)
    171 	DO_FREE(graph_file_name);
    172 
    173     lr0_leaks();
    174     lalr_leaks();
    175     mkpar_leaks();
    176     mstring_leaks();
    177     output_leaks();
    178     reader_leaks();
    179 #endif
    180 
    181     exit(k);
    182 }
    183 
    184 static void
    185 onintr(int sig GCC_UNUSED)
    186 {
    187     got_intr = 1;
    188     done(EXIT_FAILURE);
    189 }
    190 
    191 static void
    192 set_signals(void)
    193 {
    194 #ifdef SIGINT
    195     if (signal(SIGINT, SIG_IGN) != SIG_IGN)
    196 	signal(SIGINT, onintr);
    197 #endif
    198 #ifdef SIGTERM
    199     if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
    200 	signal(SIGTERM, onintr);
    201 #endif
    202 #ifdef SIGHUP
    203     if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
    204 	signal(SIGHUP, onintr);
    205 #endif
    206 }
    207 
    208 #define SIZEOF(v) (sizeof(v) / sizeof((v)[0]))
    209 
    210 /*
    211  * Long options are provided only as a compatibility aid for scripters.
    212  */
    213 /* *INDENT-OFF* */
    214 static const struct {
    215     const char long_opt[16];
    216     const char yacc_arg;
    217     const char yacc_opt;
    218 } long_opts[] = {
    219     { "defines",     1, 'H' },
    220     { "file-prefix", 1, 'b' },
    221     { "graph",       0, 'g' },
    222     { "help",        0, 'h' },
    223     { "name-prefix", 1, 'p' },
    224     { "no-lines",    0, 'l' },
    225     { "output",      1, 'o' },
    226     { "version",     0, 'V' }
    227 };
    228 /* *INDENT-ON* */
    229 
    230 /*
    231  * Usage-message is designed for 80 columns, with some unknowns.  Account for
    232  * those in the maximum width so that the usage message uses no relocatable
    233  * pointers.
    234  */
    235 #define USAGE_COLS (80 + sizeof(DEFINES_SUFFIX) + sizeof(OUTPUT_SUFFIX))
    236 
    237 static void
    238 usage(void)
    239 {
    240     /* *INDENT-OFF* */
    241     static const char msg[][USAGE_COLS] =
    242     {
    243 	{ "  -b file_prefix        set filename prefix (default \"y.\")" },
    244 	{ "  -B                    create a backtracking parser" },
    245 	{ "  -d                    write definitions (" DEFINES_SUFFIX ")" },
    246 	{ "  -h                    print this help-message" },
    247 	{ "  -H defines_file       write definitions to defines_file" },
    248 	{ "  -i                    write interface (y.tab.i)" },
    249 	{ "  -g                    write a graphical description" },
    250 	{ "  -l                    suppress #line directives" },
    251 	{ "  -L                    enable position processing, e.g., \"%locations\"" },
    252 	{ "  -o output_file        (default \"" OUTPUT_SUFFIX "\")" },
    253 	{ "  -p symbol_prefix      set symbol prefix (default \"yy\")" },
    254 	{ "  -P                    create a reentrant parser, e.g., \"%pure-parser\"" },
    255 	{ "  -r                    produce separate code and table files (y.code.c)" },
    256 	{ "  -s                    suppress #define's for quoted names in %token lines" },
    257 	{ "  -t                    add debugging support" },
    258 	{ "  -v                    write description (y.output)" },
    259 	{ "  -V                    show version information and exit" },
    260     };
    261     /* *INDENT-ON* */
    262     unsigned n;
    263 
    264     fflush(stdout);
    265     fprintf(stderr, "Usage: %s [options] filename\n", myname);
    266 
    267     fprintf(stderr, "\nOptions:\n");
    268     for (n = 0; n < SIZEOF(msg); ++n)
    269     {
    270 	fprintf(stderr, "%s\n", msg[n]);
    271     }
    272 
    273     fprintf(stderr, "\nLong options:\n");
    274     for (n = 0; n < SIZEOF(long_opts); ++n)
    275     {
    276 	fprintf(stderr, "  --%-20s-%c\n",
    277 		long_opts[n].long_opt,
    278 		long_opts[n].yacc_opt);
    279     }
    280 
    281     exit(EXIT_FAILURE);
    282 }
    283 
    284 static void
    285 invalid_option(const char *option)
    286 {
    287     fprintf(stderr, "invalid option: %s\n", option);
    288     usage();
    289 }
    290 
    291 static void
    292 setflag(int ch)
    293 {
    294     switch (ch)
    295     {
    296     case 'B':
    297 #if defined(YYBTYACC)
    298 	backtrack = 1;
    299 #else
    300 	unsupported_flag_warning("-B", "reconfigure with --enable-btyacc");
    301 #endif
    302 	break;
    303 
    304     case 'd':
    305 	dflag = 1;
    306 	dflag2 = 0;
    307 	break;
    308 
    309     case 'g':
    310 	gflag = 1;
    311 	break;
    312 
    313     case 'i':
    314 	iflag = 1;
    315 	break;
    316 
    317     case 'l':
    318 	lflag = 1;
    319 	break;
    320 
    321     case 'L':
    322 #if defined(YYBTYACC)
    323 	locations = 1;
    324 #else
    325 	unsupported_flag_warning("-L", "reconfigure with --enable-btyacc");
    326 #endif
    327 	break;
    328 
    329     case 'P':
    330 	pure_parser = 1;
    331 	break;
    332 
    333     case 'r':
    334 	rflag = 1;
    335 	break;
    336 
    337     case 's':
    338 	sflag = 1;
    339 	break;
    340 
    341     case 't':
    342 	tflag = 1;
    343 	break;
    344 
    345     case 'v':
    346 	vflag = 1;
    347 	break;
    348 
    349     case 'V':
    350 	printf("%s - %s\n", myname, VERSION);
    351 	exit(EXIT_SUCCESS);
    352 
    353     case 'y':
    354 	/* noop for bison compatibility. byacc is already designed to be posix
    355 	 * yacc compatible. */
    356 	break;
    357 
    358     default:
    359 	usage();
    360     }
    361 }
    362 
    363 static void
    364 getargs(int argc, char *argv[])
    365 {
    366     int i;
    367 #ifdef HAVE_GETOPT
    368     int ch;
    369 #endif
    370 
    371     /*
    372      * Map bison's long-options into yacc short options.
    373      */
    374     for (i = 1; i < argc; ++i)
    375     {
    376 	char *a = argv[i];
    377 
    378 	if (!strncmp(a, "--", 2))
    379 	{
    380 	    char *eqls;
    381 	    size_t lc;
    382 	    size_t len;
    383 
    384 	    if ((len = strlen(a)) == 2)
    385 		break;
    386 
    387 	    if ((eqls = strchr(a, '=')) != NULL)
    388 	    {
    389 		len = (size_t)(eqls - a);
    390 		if (len == 0 || eqls[1] == '\0')
    391 		    invalid_option(a);
    392 	    }
    393 
    394 	    for (lc = 0; lc < SIZEOF(long_opts); ++lc)
    395 	    {
    396 		if (!strncmp(long_opts[lc].long_opt, a + 2, len - 2))
    397 		{
    398 		    if (eqls != NULL && !long_opts[lc].yacc_arg)
    399 			invalid_option(a);
    400 		    *a++ = '-';
    401 		    *a++ = long_opts[lc].yacc_opt;
    402 		    *a = '\0';
    403 		    if (eqls)
    404 		    {
    405 			while ((*a++ = *++eqls) != '\0') /* empty */ ;
    406 		    }
    407 		    break;
    408 		}
    409 	    }
    410 	    if (!strncmp(a, "--", 2))
    411 		invalid_option(a);
    412 	}
    413     }
    414 
    415 #ifdef HAVE_GETOPT
    416     if (argc > 0)
    417 	myname = argv[0];
    418 
    419     while ((ch = getopt(argc, argv, "Bb:dghH:ilLo:Pp:rstVvy")) != -1)
    420     {
    421 	switch (ch)
    422 	{
    423 	case 'b':
    424 	    file_prefix = optarg;
    425 	    break;
    426 	case 'h':
    427 	    usage();
    428 	    break;
    429 	case 'H':
    430 	    dflag = dflag2 = 1;
    431 	    defines_file_name = optarg;
    432 	    break;
    433 	case 'o':
    434 	    output_file_name = optarg;
    435 	    break;
    436 	case 'p':
    437 	    symbol_prefix = optarg;
    438 	    break;
    439 	default:
    440 	    setflag(ch);
    441 	    break;
    442 	}
    443     }
    444     if ((i = optind) < argc)
    445     {
    446 	/* getopt handles "--" specially, while we handle "-" specially */
    447 	if (!strcmp(argv[i], "-"))
    448 	{
    449 	    if ((i + 1) < argc)
    450 		usage();
    451 	    input_file = stdin;
    452 	    return;
    453 	}
    454     }
    455 #else
    456     char *s;
    457     int ch;
    458 
    459     if (argc > 0)
    460 	myname = argv[0];
    461 
    462     for (i = 1; i < argc; ++i)
    463     {
    464 	s = argv[i];
    465 	if (*s != '-')
    466 	    break;
    467 	switch (ch = *++s)
    468 	{
    469 	case '\0':
    470 	    input_file = stdin;
    471 	    if (i + 1 < argc)
    472 		usage();
    473 	    return;
    474 
    475 	case '-':
    476 	    ++i;
    477 	    goto no_more_options;
    478 
    479 	case 'b':
    480 	    if (*++s)
    481 		file_prefix = s;
    482 	    else if (++i < argc)
    483 		file_prefix = argv[i];
    484 	    else
    485 		usage();
    486 	    continue;
    487 
    488 	case 'H':
    489 	    dflag = dflag2 = 1;
    490 	    if (*++s)
    491 		defines_file_name = s;
    492 	    else if (++i < argc)
    493 		defines_file_name = argv[i];
    494 	    else
    495 		usage();
    496 	    continue;
    497 
    498 	case 'o':
    499 	    if (*++s)
    500 		output_file_name = s;
    501 	    else if (++i < argc)
    502 		output_file_name = argv[i];
    503 	    else
    504 		usage();
    505 	    continue;
    506 
    507 	case 'p':
    508 	    if (*++s)
    509 		symbol_prefix = s;
    510 	    else if (++i < argc)
    511 		symbol_prefix = argv[i];
    512 	    else
    513 		usage();
    514 	    continue;
    515 
    516 	default:
    517 	    setflag(ch);
    518 	    break;
    519 	}
    520 
    521 	for (;;)
    522 	{
    523 	    switch (ch = *++s)
    524 	    {
    525 	    case '\0':
    526 		goto end_of_option;
    527 
    528 	    default:
    529 		setflag(ch);
    530 		break;
    531 	    }
    532 	}
    533       end_of_option:;
    534     }
    535 
    536   no_more_options:
    537 
    538 #endif /* HAVE_GETOPT */
    539     if (i + 1 != argc)
    540 	usage();
    541     input_file_name_len = strlen(argv[i]);
    542     input_file_name = TMALLOC(char, input_file_name_len + 1);
    543     NO_SPACE(input_file_name);
    544     strcpy(input_file_name, argv[i]);
    545 }
    546 
    547 void *
    548 allocate(size_t n)
    549 {
    550     void *p;
    551 
    552     p = NULL;
    553     if (n)
    554     {
    555 	p = CALLOC(1, n);
    556 	NO_SPACE(p);
    557     }
    558     return (p);
    559 }
    560 
    561 #define CREATE_FILE_NAME(dest, suffix) \
    562 	dest = alloc_file_name(len, suffix)
    563 
    564 static char *
    565 alloc_file_name(size_t len, const char *suffix)
    566 {
    567     char *result = TMALLOC(char, len + strlen(suffix) + 1);
    568     if (result == NULL)
    569 	on_error();
    570     strcpy(result, file_prefix);
    571     strcpy(result + len, suffix);
    572     return result;
    573 }
    574 
    575 static char *
    576 find_suffix(char *name, const char *suffix)
    577 {
    578     size_t len = strlen(name);
    579     size_t slen = strlen(suffix);
    580     if (len >= slen)
    581     {
    582 	name += len - slen;
    583 	if (strcmp(name, suffix) == 0)
    584 	    return name;
    585     }
    586     return NULL;
    587 }
    588 
    589 static void
    590 create_file_names(void)
    591 {
    592     size_t len;
    593     const char *defines_suffix;
    594     const char *externs_suffix;
    595     char *suffix;
    596 
    597     suffix = NULL;
    598     defines_suffix = DEFINES_SUFFIX;
    599     externs_suffix = EXTERNS_SUFFIX;
    600 
    601     /* compute the file_prefix from the user provided output_file_name */
    602     if (output_file_name != 0)
    603     {
    604 	if (!(suffix = find_suffix(output_file_name, OUTPUT_SUFFIX))
    605 	    && (suffix = find_suffix(output_file_name, ".c")))
    606 	{
    607 	    defines_suffix = ".h";
    608 	    externs_suffix = ".i";
    609 	}
    610     }
    611 
    612     if (suffix != NULL)
    613     {
    614 	len = (size_t)(suffix - output_file_name);
    615 	file_prefix = TMALLOC(char, len + 1);
    616 	NO_SPACE(file_prefix);
    617 	strncpy(file_prefix, output_file_name, len)[len] = 0;
    618     }
    619     else
    620 	len = strlen(file_prefix);
    621 
    622     /* if "-o filename" was not given */
    623     if (output_file_name == 0)
    624     {
    625 	oflag = 1;
    626 	CREATE_FILE_NAME(output_file_name, OUTPUT_SUFFIX);
    627     }
    628 
    629     if (rflag)
    630     {
    631 	CREATE_FILE_NAME(code_file_name, CODE_SUFFIX);
    632     }
    633     else
    634 	code_file_name = output_file_name;
    635 
    636     if (dflag && !dflag2)
    637     {
    638 	CREATE_FILE_NAME(defines_file_name, defines_suffix);
    639     }
    640 
    641     if (iflag)
    642     {
    643 	CREATE_FILE_NAME(externs_file_name, externs_suffix);
    644     }
    645 
    646     if (vflag)
    647     {
    648 	CREATE_FILE_NAME(verbose_file_name, VERBOSE_SUFFIX);
    649     }
    650 
    651     if (gflag)
    652     {
    653 	CREATE_FILE_NAME(graph_file_name, GRAPH_SUFFIX);
    654     }
    655 
    656     if (suffix != NULL)
    657     {
    658 	FREE(file_prefix);
    659     }
    660 }
    661 
    662 #if USE_MKSTEMP
    663 static void
    664 close_tmpfiles(void)
    665 {
    666     while (my_tmpfiles != 0)
    667     {
    668 	MY_TMPFILES *next = my_tmpfiles->next;
    669 
    670 	(void)chmod(my_tmpfiles->name, 0644);
    671 	(void)unlink(my_tmpfiles->name);
    672 
    673 	free(my_tmpfiles->name);
    674 	free(my_tmpfiles);
    675 
    676 	my_tmpfiles = next;
    677     }
    678 }
    679 
    680 #ifndef HAVE_MKSTEMP
    681 static int
    682 my_mkstemp(char *temp)
    683 {
    684     int fd;
    685     char *dname;
    686     char *fname;
    687     char *name;
    688 
    689     /*
    690      * Split-up to use tempnam, rather than tmpnam; the latter (like
    691      * mkstemp) is unusable on Windows.
    692      */
    693     if ((fname = strrchr(temp, '/')) != 0)
    694     {
    695 	dname = strdup(temp);
    696 	dname[++fname - temp] = '\0';
    697     }
    698     else
    699     {
    700 	dname = 0;
    701 	fname = temp;
    702     }
    703     if ((name = tempnam(dname, fname)) != 0)
    704     {
    705 	fd = open(name, O_CREAT | O_EXCL | O_RDWR);
    706 	strcpy(temp, name);
    707     }
    708     else
    709     {
    710 	fd = -1;
    711     }
    712 
    713     if (dname != 0)
    714 	free(dname);
    715 
    716     return fd;
    717 }
    718 #define mkstemp(s) my_mkstemp(s)
    719 #endif
    720 
    721 #endif
    722 
    723 /*
    724  * tmpfile() should be adequate, except that it may require special privileges
    725  * to use, e.g., MinGW and Windows 7 where it tries to use the root directory.
    726  */
    727 static FILE *
    728 open_tmpfile(const char *label)
    729 {
    730 #define MY_FMT "%s/%.*sXXXXXX"
    731     FILE *result;
    732 #if USE_MKSTEMP
    733     const char *tmpdir;
    734     char *name;
    735 
    736     if (((tmpdir = getenv("TMPDIR")) == 0 || access(tmpdir, W_OK) != 0) ||
    737 	((tmpdir = getenv("TEMP")) == 0 || access(tmpdir, W_OK) != 0))
    738     {
    739 #ifdef P_tmpdir
    740 	tmpdir = P_tmpdir;
    741 #else
    742 	tmpdir = "/tmp";
    743 #endif
    744 	if (access(tmpdir, W_OK) != 0)
    745 	    tmpdir = ".";
    746     }
    747 
    748     /* The size of the format is guaranteed to be longer than the result from
    749      * printing empty strings with it; this calculation accounts for the
    750      * string-lengths as well.
    751      */
    752     name = malloc(strlen(tmpdir) + sizeof(MY_FMT) + strlen(label));
    753 
    754     result = 0;
    755     if (name != 0)
    756     {
    757 	int fd;
    758 	const char *mark;
    759 
    760 	mode_t save_umask = umask(0177);
    761 
    762 	if ((mark = strrchr(label, '_')) == 0)
    763 	    mark = label + strlen(label);
    764 
    765 	sprintf(name, MY_FMT, tmpdir, (int)(mark - label), label);
    766 	fd = mkstemp(name);
    767 	if (fd >= 0
    768 	    && (result = fdopen(fd, "w+")) != 0)
    769 	{
    770 	    MY_TMPFILES *item;
    771 
    772 	    if (my_tmpfiles == 0)
    773 	    {
    774 		atexit(close_tmpfiles);
    775 	    }
    776 
    777 	    item = NEW(MY_TMPFILES);
    778 	    NO_SPACE(item);
    779 
    780 	    item->name = name;
    781 	    NO_SPACE(item->name);
    782 
    783 	    item->next = my_tmpfiles;
    784 	    my_tmpfiles = item;
    785 	}
    786 	else
    787 	{
    788 	    FREE(name);
    789 	}
    790 	(void)umask(save_umask);
    791     }
    792 #else
    793     result = tmpfile();
    794 #endif
    795 
    796     if (result == 0)
    797 	open_error(label);
    798     return result;
    799 #undef MY_FMT
    800 }
    801 
    802 static void
    803 open_files(void)
    804 {
    805     create_file_names();
    806 
    807     if (input_file == 0)
    808     {
    809 	input_file = fopen(input_file_name, "r");
    810 	if (input_file == 0)
    811 	    open_error(input_file_name);
    812     }
    813 
    814     action_file = open_tmpfile("action_file");
    815     text_file = open_tmpfile("text_file");
    816 
    817     if (vflag)
    818     {
    819 	verbose_file = fopen(verbose_file_name, "w");
    820 	if (verbose_file == 0)
    821 	    open_error(verbose_file_name);
    822     }
    823 
    824     if (gflag)
    825     {
    826 	graph_file = fopen(graph_file_name, "w");
    827 	if (graph_file == 0)
    828 	    open_error(graph_file_name);
    829 	fprintf(graph_file, "digraph %s {\n", file_prefix);
    830 	fprintf(graph_file, "\tedge [fontsize=10];\n");
    831 	fprintf(graph_file, "\tnode [shape=box,fontsize=10];\n");
    832 	fprintf(graph_file, "\torientation=landscape;\n");
    833 	fprintf(graph_file, "\trankdir=LR;\n");
    834 	fprintf(graph_file, "\t/*\n");
    835 	fprintf(graph_file, "\tmargin=0.2;\n");
    836 	fprintf(graph_file, "\tpage=\"8.27,11.69\"; // for A4 printing\n");
    837 	fprintf(graph_file, "\tratio=auto;\n");
    838 	fprintf(graph_file, "\t*/\n");
    839     }
    840 
    841     if (dflag || dflag2)
    842     {
    843 	defines_file = fopen(defines_file_name, "w");
    844 	if (defines_file == 0)
    845 	    open_error(defines_file_name);
    846 	union_file = open_tmpfile("union_file");
    847     }
    848 
    849     if (iflag)
    850     {
    851 	externs_file = fopen(externs_file_name, "w");
    852 	if (externs_file == 0)
    853 	    open_error(externs_file_name);
    854     }
    855 
    856     output_file = fopen(output_file_name, "w");
    857     if (output_file == 0)
    858 	open_error(output_file_name);
    859 
    860     if (rflag)
    861     {
    862 	code_file = fopen(code_file_name, "w");
    863 	if (code_file == 0)
    864 	    open_error(code_file_name);
    865     }
    866     else
    867 	code_file = output_file;
    868 }
    869 
    870 int
    871 main(int argc, char *argv[])
    872 {
    873     SRexpect = -1;
    874     RRexpect = -1;
    875     exit_code = EXIT_SUCCESS;
    876 
    877     set_signals();
    878     getargs(argc, argv);
    879     open_files();
    880     reader();
    881     lr0();
    882     lalr();
    883     make_parser();
    884     graph();
    885     finalize_closure();
    886     verbose();
    887     output();
    888     done(exit_code);
    889     /*NOTREACHED */
    890 }
    891