Home | History | Annotate | Line # | Download | only in dist
main.c revision 1.6
      1  1.6  christos /*	$NetBSD: main.c,v 1.6 2010/12/25 23:43:30 christos Exp $	*/
      2  1.4  christos /* Id: main.c,v 1.30 2010/11/24 15:13:39 tom Exp */
      3  1.4  christos 
      4  1.6  christos #include "defs.h"
      5  1.5     joerg 
      6  1.4  christos #include <sys/cdefs.h>
      7  1.6  christos __RCSID("$NetBSD: main.c,v 1.6 2010/12/25 23:43:30 christos Exp $");
      8  1.1  christos 
      9  1.1  christos #include <signal.h>
     10  1.1  christos #include <unistd.h>		/* for _exit() */
     11  1.1  christos 
     12  1.1  christos 
     13  1.1  christos char dflag;
     14  1.1  christos char gflag;
     15  1.1  christos char lflag;
     16  1.4  christos static char oflag;
     17  1.1  christos char rflag;
     18  1.1  christos char tflag;
     19  1.1  christos char vflag;
     20  1.1  christos 
     21  1.1  christos const char *symbol_prefix;
     22  1.1  christos const char *myname = "yacc";
     23  1.1  christos 
     24  1.1  christos int lineno;
     25  1.1  christos int outline;
     26  1.1  christos 
     27  1.1  christos static char empty_string[] = "";
     28  1.4  christos static char default_file_prefix[] = "y";
     29  1.2  christos static int explicit_file_name;
     30  1.1  christos 
     31  1.4  christos static char *file_prefix = default_file_prefix;
     32  1.1  christos 
     33  1.1  christos char *code_file_name;
     34  1.4  christos char *input_file_name = empty_string;
     35  1.4  christos static char *defines_file_name;
     36  1.4  christos static char *graph_file_name;
     37  1.4  christos static char *output_file_name;
     38  1.4  christos static char *verbose_file_name;
     39  1.1  christos 
     40  1.1  christos FILE *action_file;	/*  a temp file, used to save actions associated    */
     41  1.1  christos 			/*  with rules until the parser is written          */
     42  1.1  christos FILE *code_file;	/*  y.code.c (used when the -r option is specified) */
     43  1.1  christos FILE *defines_file;	/*  y.tab.h                                         */
     44  1.1  christos FILE *input_file;	/*  the input file                                  */
     45  1.1  christos FILE *output_file;	/*  y.tab.c                                         */
     46  1.1  christos FILE *text_file;	/*  a temp file, used to save text until all        */
     47  1.1  christos 			/*  symbols have been defined                       */
     48  1.1  christos FILE *union_file;	/*  a temp file, used to save the union             */
     49  1.1  christos 			/*  definition until all symbol have been           */
     50  1.1  christos 			/*  defined                                         */
     51  1.1  christos FILE *verbose_file;	/*  y.output                                        */
     52  1.1  christos FILE *graph_file;	/*  y.dot                                           */
     53  1.1  christos 
     54  1.1  christos int nitems;
     55  1.1  christos int nrules;
     56  1.1  christos int nsyms;
     57  1.1  christos int ntokens;
     58  1.1  christos int nvars;
     59  1.1  christos 
     60  1.1  christos Value_t start_symbol;
     61  1.1  christos char **symbol_name;
     62  1.1  christos char **symbol_pname;
     63  1.1  christos Value_t *symbol_value;
     64  1.1  christos short *symbol_prec;
     65  1.1  christos char *symbol_assoc;
     66  1.1  christos 
     67  1.4  christos int pure_parser;
     68  1.1  christos int exit_code;
     69  1.1  christos 
     70  1.1  christos Value_t *ritem;
     71  1.1  christos Value_t *rlhs;
     72  1.1  christos Value_t *rrhs;
     73  1.1  christos Value_t *rprec;
     74  1.1  christos Assoc_t *rassoc;
     75  1.1  christos Value_t **derives;
     76  1.1  christos char *nullable;
     77  1.1  christos 
     78  1.1  christos /*
     79  1.1  christos  * Since fclose() is called via the signal handler, it might die.  Don't loop
     80  1.1  christos  * if there is a problem closing a file.
     81  1.1  christos  */
     82  1.1  christos #define DO_CLOSE(fp) \
     83  1.1  christos 	if (fp != 0) { \
     84  1.1  christos 	    FILE *use = fp; \
     85  1.1  christos 	    fp = 0; \
     86  1.1  christos 	    fclose(use); \
     87  1.1  christos 	}
     88  1.1  christos 
     89  1.1  christos static int got_intr = 0;
     90  1.1  christos 
     91  1.1  christos void
     92  1.1  christos done(int k)
     93  1.1  christos {
     94  1.1  christos     DO_CLOSE(input_file);
     95  1.1  christos     DO_CLOSE(output_file);
     96  1.1  christos 
     97  1.1  christos     DO_CLOSE(action_file);
     98  1.1  christos     DO_CLOSE(defines_file);
     99  1.1  christos     DO_CLOSE(graph_file);
    100  1.1  christos     DO_CLOSE(text_file);
    101  1.1  christos     DO_CLOSE(union_file);
    102  1.1  christos     DO_CLOSE(verbose_file);
    103  1.1  christos 
    104  1.1  christos     if (got_intr)
    105  1.1  christos 	_exit(EXIT_FAILURE);
    106  1.1  christos 
    107  1.1  christos #ifdef NO_LEAKS
    108  1.1  christos     if (rflag)
    109  1.1  christos 	DO_FREE(code_file_name);
    110  1.1  christos 
    111  1.1  christos     if (dflag)
    112  1.1  christos 	DO_FREE(defines_file_name);
    113  1.1  christos 
    114  1.1  christos     if (oflag)
    115  1.1  christos 	DO_FREE(output_file_name);
    116  1.1  christos 
    117  1.1  christos     if (vflag)
    118  1.1  christos 	DO_FREE(verbose_file_name);
    119  1.1  christos 
    120  1.1  christos     if (gflag)
    121  1.1  christos 	DO_FREE(graph_file_name);
    122  1.1  christos 
    123  1.1  christos     lr0_leaks();
    124  1.1  christos     lalr_leaks();
    125  1.1  christos     mkpar_leaks();
    126  1.1  christos     output_leaks();
    127  1.1  christos     reader_leaks();
    128  1.1  christos #endif
    129  1.1  christos 
    130  1.4  christos     if (rflag)
    131  1.4  christos 	DO_CLOSE(code_file);
    132  1.4  christos 
    133  1.1  christos     exit(k);
    134  1.1  christos }
    135  1.1  christos 
    136  1.1  christos static void
    137  1.1  christos onintr(int sig GCC_UNUSED)
    138  1.1  christos {
    139  1.1  christos     got_intr = 1;
    140  1.1  christos     done(EXIT_FAILURE);
    141  1.1  christos }
    142  1.1  christos 
    143  1.1  christos static void
    144  1.1  christos set_signals(void)
    145  1.1  christos {
    146  1.1  christos #ifdef SIGINT
    147  1.1  christos     if (signal(SIGINT, SIG_IGN) != SIG_IGN)
    148  1.1  christos 	signal(SIGINT, onintr);
    149  1.1  christos #endif
    150  1.1  christos #ifdef SIGTERM
    151  1.1  christos     if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
    152  1.1  christos 	signal(SIGTERM, onintr);
    153  1.1  christos #endif
    154  1.1  christos #ifdef SIGHUP
    155  1.1  christos     if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
    156  1.1  christos 	signal(SIGHUP, onintr);
    157  1.1  christos #endif
    158  1.1  christos }
    159  1.1  christos 
    160  1.1  christos static void
    161  1.1  christos usage(void)
    162  1.1  christos {
    163  1.1  christos     static const char *msg[] =
    164  1.1  christos     {
    165  1.1  christos 	""
    166  1.1  christos 	,"Options:"
    167  1.1  christos 	,"  -b file_prefix        set filename prefix (default \"y.\")"
    168  1.1  christos 	,"  -d                    write definitions (y.tab.h)"
    169  1.1  christos 	,"  -g                    write a graphical description"
    170  1.1  christos 	,"  -l                    suppress #line directives"
    171  1.1  christos 	,"  -o output_file        (default \"y.tab.c\")"
    172  1.1  christos 	,"  -p symbol_prefix      set symbol prefix (default \"yy\")"
    173  1.4  christos 	,"  -P                    create a reentrant parser, e.g., \"%pure-parser\""
    174  1.1  christos 	,"  -r                    produce separate code and table files (y.code.c)"
    175  1.1  christos 	,"  -t                    add debugging support"
    176  1.1  christos 	,"  -v                    write description (y.output)"
    177  1.1  christos 	,"  -V                    show version information and exit"
    178  1.1  christos     };
    179  1.1  christos     unsigned n;
    180  1.1  christos 
    181  1.1  christos     fflush(stdout);
    182  1.1  christos     fprintf(stderr, "Usage: %s [options] filename\n", myname);
    183  1.1  christos     for (n = 0; n < sizeof(msg) / sizeof(msg[0]); ++n)
    184  1.1  christos 	fprintf(stderr, "%s\n", msg[n]);
    185  1.1  christos 
    186  1.1  christos     exit(1);
    187  1.1  christos }
    188  1.1  christos 
    189  1.1  christos static void
    190  1.1  christos setflag(int ch)
    191  1.1  christos {
    192  1.1  christos     switch (ch)
    193  1.1  christos     {
    194  1.1  christos     case 'd':
    195  1.1  christos 	dflag = 1;
    196  1.1  christos 	break;
    197  1.1  christos 
    198  1.1  christos     case 'g':
    199  1.1  christos 	gflag = 1;
    200  1.1  christos 	break;
    201  1.1  christos 
    202  1.1  christos     case 'l':
    203  1.1  christos 	lflag = 1;
    204  1.1  christos 	break;
    205  1.1  christos 
    206  1.4  christos     case 'P':
    207  1.4  christos 	pure_parser = 1;
    208  1.4  christos 	break;
    209  1.4  christos 
    210  1.1  christos     case 'r':
    211  1.1  christos 	rflag = 1;
    212  1.1  christos 	break;
    213  1.1  christos 
    214  1.1  christos     case 't':
    215  1.1  christos 	tflag = 1;
    216  1.1  christos 	break;
    217  1.1  christos 
    218  1.1  christos     case 'v':
    219  1.1  christos 	vflag = 1;
    220  1.1  christos 	break;
    221  1.1  christos 
    222  1.1  christos     case 'V':
    223  1.1  christos 	printf("%s - %s\n", myname, VERSION);
    224  1.1  christos 	exit(EXIT_SUCCESS);
    225  1.1  christos 
    226  1.4  christos     case 'y':
    227  1.4  christos 	/* noop for bison compatibility. byacc is already designed to be posix
    228  1.4  christos 	 * yacc compatible. */
    229  1.4  christos 	break;
    230  1.4  christos 
    231  1.1  christos     default:
    232  1.1  christos 	usage();
    233  1.1  christos     }
    234  1.1  christos }
    235  1.1  christos 
    236  1.1  christos static void
    237  1.1  christos getargs(int argc, char *argv[])
    238  1.1  christos {
    239  1.1  christos     int i;
    240  1.1  christos     char *s;
    241  1.1  christos     int ch;
    242  1.1  christos 
    243  1.1  christos     if (argc > 0)
    244  1.1  christos 	myname = argv[0];
    245  1.1  christos 
    246  1.1  christos     for (i = 1; i < argc; ++i)
    247  1.1  christos     {
    248  1.1  christos 	s = argv[i];
    249  1.1  christos 	if (*s != '-')
    250  1.1  christos 	    break;
    251  1.1  christos 	switch (ch = *++s)
    252  1.1  christos 	{
    253  1.1  christos 	case '\0':
    254  1.1  christos 	    input_file = stdin;
    255  1.1  christos 	    if (i + 1 < argc)
    256  1.1  christos 		usage();
    257  1.1  christos 	    return;
    258  1.1  christos 
    259  1.1  christos 	case '-':
    260  1.1  christos 	    ++i;
    261  1.1  christos 	    goto no_more_options;
    262  1.1  christos 
    263  1.1  christos 	case 'b':
    264  1.1  christos 	    if (*++s)
    265  1.1  christos 		file_prefix = s;
    266  1.1  christos 	    else if (++i < argc)
    267  1.1  christos 		file_prefix = argv[i];
    268  1.1  christos 	    else
    269  1.1  christos 		usage();
    270  1.1  christos 	    continue;
    271  1.1  christos 
    272  1.1  christos 	case 'o':
    273  1.1  christos 	    if (*++s)
    274  1.1  christos 		output_file_name = s;
    275  1.1  christos 	    else if (++i < argc)
    276  1.1  christos 		output_file_name = argv[i];
    277  1.1  christos 	    else
    278  1.1  christos 		usage();
    279  1.2  christos 	    explicit_file_name = 1;
    280  1.1  christos 	    continue;
    281  1.1  christos 
    282  1.1  christos 	case 'p':
    283  1.1  christos 	    if (*++s)
    284  1.1  christos 		symbol_prefix = s;
    285  1.1  christos 	    else if (++i < argc)
    286  1.1  christos 		symbol_prefix = argv[i];
    287  1.1  christos 	    else
    288  1.1  christos 		usage();
    289  1.1  christos 	    continue;
    290  1.1  christos 
    291  1.1  christos 	default:
    292  1.1  christos 	    setflag(ch);
    293  1.1  christos 	    break;
    294  1.1  christos 	}
    295  1.1  christos 
    296  1.1  christos 	for (;;)
    297  1.1  christos 	{
    298  1.1  christos 	    switch (ch = *++s)
    299  1.1  christos 	    {
    300  1.1  christos 	    case '\0':
    301  1.1  christos 		goto end_of_option;
    302  1.1  christos 
    303  1.1  christos 	    default:
    304  1.1  christos 		setflag(ch);
    305  1.1  christos 		break;
    306  1.1  christos 	    }
    307  1.1  christos 	}
    308  1.1  christos       end_of_option:;
    309  1.1  christos     }
    310  1.1  christos 
    311  1.1  christos   no_more_options:;
    312  1.1  christos     if (i + 1 != argc)
    313  1.1  christos 	usage();
    314  1.1  christos     input_file_name = argv[i];
    315  1.1  christos }
    316  1.1  christos 
    317  1.1  christos char *
    318  1.4  christos allocate(size_t n)
    319  1.1  christos {
    320  1.1  christos     char *p;
    321  1.1  christos 
    322  1.1  christos     p = NULL;
    323  1.1  christos     if (n)
    324  1.1  christos     {
    325  1.1  christos 	p = CALLOC(1, n);
    326  1.4  christos 	NO_SPACE(p);
    327  1.1  christos     }
    328  1.1  christos     return (p);
    329  1.1  christos }
    330  1.1  christos 
    331  1.1  christos #define CREATE_FILE_NAME(dest, suffix) \
    332  1.1  christos 	dest = MALLOC(len + strlen(suffix) + 1); \
    333  1.4  christos 	NO_SPACE(dest); \
    334  1.1  christos 	strcpy(dest, file_prefix); \
    335  1.1  christos 	strcpy(dest + len, suffix)
    336  1.1  christos 
    337  1.1  christos static void
    338  1.1  christos create_file_names(void)
    339  1.1  christos {
    340  1.1  christos     size_t len;
    341  1.1  christos     const char *defines_suffix;
    342  1.1  christos     char *prefix;
    343  1.1  christos 
    344  1.1  christos     prefix = NULL;
    345  1.1  christos     defines_suffix = DEFINES_SUFFIX;
    346  1.1  christos 
    347  1.1  christos     /* compute the file_prefix from the user provided output_file_name */
    348  1.1  christos     if (output_file_name != 0)
    349  1.1  christos     {
    350  1.1  christos 	if (!(prefix = strstr(output_file_name, ".tab.c"))
    351  1.1  christos 	    && (prefix = strstr(output_file_name, ".c")))
    352  1.1  christos 	    defines_suffix = ".h";
    353  1.1  christos     }
    354  1.1  christos 
    355  1.1  christos     if (prefix != NULL)
    356  1.1  christos     {
    357  1.1  christos 	len = (size_t) (prefix - output_file_name);
    358  1.4  christos 	file_prefix = MALLOC(len + 1);
    359  1.4  christos 	NO_SPACE(file_prefix);
    360  1.4  christos 	strncpy(file_prefix, output_file_name, len)[len] = 0;
    361  1.1  christos     }
    362  1.1  christos     else
    363  1.1  christos 	len = strlen(file_prefix);
    364  1.1  christos 
    365  1.1  christos     /* if "-o filename" was not given */
    366  1.1  christos     if (output_file_name == 0)
    367  1.1  christos     {
    368  1.1  christos 	oflag = 1;
    369  1.1  christos 	CREATE_FILE_NAME(output_file_name, OUTPUT_SUFFIX);
    370  1.1  christos     }
    371  1.1  christos 
    372  1.1  christos     if (rflag)
    373  1.1  christos     {
    374  1.1  christos 	CREATE_FILE_NAME(code_file_name, CODE_SUFFIX);
    375  1.1  christos     }
    376  1.1  christos     else
    377  1.1  christos 	code_file_name = output_file_name;
    378  1.1  christos 
    379  1.1  christos     if (dflag)
    380  1.1  christos     {
    381  1.2  christos 	if (explicit_file_name)
    382  1.2  christos 	{
    383  1.2  christos 	    char *suffix;
    384  1.2  christos 	    defines_file_name = strdup(output_file_name);
    385  1.2  christos 	    if (defines_file_name == 0)
    386  1.2  christos 		no_space();
    387  1.2  christos 	    /* does the output_file_name have a known suffix */
    388  1.2  christos             suffix = strrchr(output_file_name, '.');
    389  1.2  christos             if (suffix != 0 &&
    390  1.2  christos 		(!strcmp(suffix, ".c") ||   /* good, old-fashioned C */
    391  1.2  christos                  !strcmp(suffix, ".C") ||   /* C++, or C on Windows */
    392  1.2  christos                  !strcmp(suffix, ".cc") ||  /* C++ */
    393  1.2  christos                  !strcmp(suffix, ".cxx") || /* C++ */
    394  1.2  christos                  !strcmp(suffix, ".cpp")))  /* C++ (Windows) */
    395  1.2  christos             {
    396  1.2  christos                 strncpy(defines_file_name, output_file_name,
    397  1.2  christos                         suffix - output_file_name + 1);
    398  1.2  christos                 defines_file_name[suffix - output_file_name + 1] = 'h';
    399  1.2  christos                 defines_file_name[suffix - output_file_name + 2] = 0;
    400  1.2  christos             } else {
    401  1.2  christos                 fprintf(stderr,"%s: suffix of output file name %s"
    402  1.2  christos                                " not recognized, no -d file generated.\n",
    403  1.2  christos                         myname, output_file_name);
    404  1.2  christos                 dflag = 0;
    405  1.2  christos                 free(defines_file_name);
    406  1.2  christos                 defines_file_name = 0;
    407  1.2  christos             }
    408  1.3  christos 	} else {
    409  1.3  christos 	    CREATE_FILE_NAME(defines_file_name, defines_suffix);
    410  1.2  christos 	}
    411  1.1  christos     }
    412  1.1  christos 
    413  1.1  christos     if (vflag)
    414  1.1  christos     {
    415  1.1  christos 	CREATE_FILE_NAME(verbose_file_name, VERBOSE_SUFFIX);
    416  1.1  christos     }
    417  1.1  christos 
    418  1.1  christos     if (gflag)
    419  1.1  christos     {
    420  1.1  christos 	CREATE_FILE_NAME(graph_file_name, GRAPH_SUFFIX);
    421  1.1  christos     }
    422  1.1  christos 
    423  1.4  christos     if (prefix != NULL)
    424  1.1  christos     {
    425  1.4  christos 	FREE(file_prefix);
    426  1.1  christos     }
    427  1.1  christos }
    428  1.1  christos 
    429  1.1  christos static void
    430  1.1  christos open_files(void)
    431  1.1  christos {
    432  1.1  christos     create_file_names();
    433  1.1  christos 
    434  1.1  christos     if (input_file == 0)
    435  1.1  christos     {
    436  1.1  christos 	input_file = fopen(input_file_name, "r");
    437  1.1  christos 	if (input_file == 0)
    438  1.1  christos 	    open_error(input_file_name);
    439  1.1  christos     }
    440  1.1  christos 
    441  1.1  christos     action_file = tmpfile();
    442  1.1  christos     if (action_file == 0)
    443  1.1  christos 	open_error("action_file");
    444  1.1  christos 
    445  1.1  christos     text_file = tmpfile();
    446  1.1  christos     if (text_file == 0)
    447  1.1  christos 	open_error("text_file");
    448  1.1  christos 
    449  1.1  christos     if (vflag)
    450  1.1  christos     {
    451  1.1  christos 	verbose_file = fopen(verbose_file_name, "w");
    452  1.1  christos 	if (verbose_file == 0)
    453  1.1  christos 	    open_error(verbose_file_name);
    454  1.1  christos     }
    455  1.1  christos 
    456  1.1  christos     if (gflag)
    457  1.1  christos     {
    458  1.1  christos 	graph_file = fopen(graph_file_name, "w");
    459  1.1  christos 	if (graph_file == 0)
    460  1.1  christos 	    open_error(graph_file_name);
    461  1.1  christos 	fprintf(graph_file, "digraph %s {\n", file_prefix);
    462  1.1  christos 	fprintf(graph_file, "\tedge [fontsize=10];\n");
    463  1.1  christos 	fprintf(graph_file, "\tnode [shape=box,fontsize=10];\n");
    464  1.1  christos 	fprintf(graph_file, "\torientation=landscape;\n");
    465  1.1  christos 	fprintf(graph_file, "\trankdir=LR;\n");
    466  1.1  christos 	fprintf(graph_file, "\t/*\n");
    467  1.1  christos 	fprintf(graph_file, "\tmargin=0.2;\n");
    468  1.1  christos 	fprintf(graph_file, "\tpage=\"8.27,11.69\"; // for A4 printing\n");
    469  1.1  christos 	fprintf(graph_file, "\tratio=auto;\n");
    470  1.1  christos 	fprintf(graph_file, "\t*/\n");
    471  1.1  christos     }
    472  1.1  christos 
    473  1.1  christos     if (dflag)
    474  1.1  christos     {
    475  1.1  christos 	defines_file = fopen(defines_file_name, "w");
    476  1.1  christos 	if (defines_file == 0)
    477  1.1  christos 	    open_error(defines_file_name);
    478  1.1  christos 	union_file = tmpfile();
    479  1.1  christos 	if (union_file == 0)
    480  1.1  christos 	    open_error("union_file");
    481  1.1  christos     }
    482  1.1  christos 
    483  1.1  christos     output_file = fopen(output_file_name, "w");
    484  1.1  christos     if (output_file == 0)
    485  1.1  christos 	open_error(output_file_name);
    486  1.1  christos 
    487  1.1  christos     if (rflag)
    488  1.1  christos     {
    489  1.1  christos 	code_file = fopen(code_file_name, "w");
    490  1.1  christos 	if (code_file == 0)
    491  1.1  christos 	    open_error(code_file_name);
    492  1.1  christos     }
    493  1.1  christos     else
    494  1.1  christos 	code_file = output_file;
    495  1.1  christos }
    496  1.1  christos 
    497  1.1  christos int
    498  1.1  christos main(int argc, char *argv[])
    499  1.1  christos {
    500  1.1  christos     SRexpect = -1;
    501  1.1  christos     RRexpect = -1;
    502  1.1  christos     exit_code = EXIT_SUCCESS;
    503  1.1  christos 
    504  1.1  christos     set_signals();
    505  1.1  christos     getargs(argc, argv);
    506  1.1  christos     open_files();
    507  1.1  christos     reader();
    508  1.1  christos     lr0();
    509  1.1  christos     lalr();
    510  1.1  christos     make_parser();
    511  1.1  christos     graph();
    512  1.1  christos     finalize_closure();
    513  1.1  christos     verbose();
    514  1.1  christos     output();
    515  1.1  christos     done(exit_code);
    516  1.1  christos     /*NOTREACHED */
    517  1.1  christos }
    518