Annotation of 43BSDReno/pgrm/yacc/main.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * Copyright (c) 1989 The Regents of the University of California.
                      3:  * All rights reserved.
                      4:  *
                      5:  * This code is derived from software contributed to Berkeley by
                      6:  * Robert Paul Corbett.
                      7:  *
                      8:  * Redistribution and use in source and binary forms are permitted provided
                      9:  * that: (1) source distributions retain this entire copyright notice and
                     10:  * comment, and (2) distributions including binaries display the following
                     11:  * acknowledgement:  ``This product includes software developed by the
                     12:  * University of California, Berkeley and its contributors'' in the
                     13:  * documentation or other materials provided with the distribution and in
                     14:  * all advertising materials mentioning features or use of this software.
                     15:  * Neither the name of the University nor the names of its contributors may
                     16:  * be used to endorse or promote products derived from this software without
                     17:  * specific prior written permission.
                     18:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
                     19:  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
                     20:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
                     21:  */
                     22: 
                     23: #ifndef lint
                     24: char copyright[] =
                     25: "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
                     26:  All rights reserved.\n";
                     27: #endif /* not lint */
                     28: 
                     29: #ifndef lint
                     30: static char sccsid[] = "@(#)main.c     5.2 (Berkeley) 6/1/90";
                     31: #endif /* not lint */
                     32: 
                     33: #include <signal.h>
                     34: #include "defs.h"
                     35: 
                     36: char dflag;
                     37: char lflag;
                     38: char tflag;
                     39: char vflag;
                     40: 
                     41: char *prefix = "y";
                     42: char *myname = "yacc";
                     43: char *temp_form = "yacc.XXXXXXX";
                     44: 
                     45: int lineno;
                     46: int outline;
                     47: 
                     48: char *action_file_name;
                     49: char *defines_file_name;
                     50: char *input_file_name = "";
                     51: char *output_file_name;
                     52: char *text_file_name;
                     53: char *union_file_name;
                     54: char *verbose_file_name;
                     55: 
                     56: FILE *action_file;     /*  a temp file, used to save actions associated    */
                     57:                        /*  with rules until the parser is written          */
                     58: FILE *defines_file;    /*  y.tab.h                                         */
                     59: FILE *input_file;      /*  the input file                                  */
                     60: FILE *output_file;     /*  y.tab.c                                         */
                     61: FILE *text_file;       /*  a temp file, used to save text until all        */
                     62:                        /*  symbols have been defined                       */
                     63: FILE *union_file;      /*  a temp file, used to save the union             */
                     64:                        /*  definition until all symbol have been           */
                     65:                        /*  defined                                         */
                     66: FILE *verbose_file;    /*  y.output                                        */
                     67: 
                     68: int nitems;
                     69: int nrules;
                     70: int nsyms;
                     71: int ntokens;
                     72: int nvars;
                     73: 
                     74: int   start_symbol;
                     75: char  **symbol_name;
                     76: short *symbol_value;
                     77: short *symbol_prec;
                     78: char  *symbol_assoc;
                     79: 
                     80: short *ritem;
                     81: short *rlhs;
                     82: short *rrhs;
                     83: short *rprec;
                     84: char  *rassoc;
                     85: short **derives;
                     86: char *nullable;
                     87: 
                     88: extern char *mktemp();
                     89: extern char *getenv();
                     90: 
                     91: 
                     92: done(k)
                     93: int k;
                     94: {
                     95:     if (action_file) { fclose(action_file); unlink(action_file_name); }
                     96:     if (text_file) { fclose(text_file); unlink(text_file_name); }
                     97:     if (union_file) { fclose(union_file); unlink(union_file_name); }
                     98:     exit(k);
                     99: }
                    100: 
                    101: 
                    102: onintr()
                    103: {
                    104:     done(1);
                    105: }
                    106: 
                    107: 
                    108: set_signals()
                    109: {
                    110: #ifdef SIGINT
                    111:     if (signal(SIGINT, SIG_IGN) != SIG_IGN)
                    112:        signal(SIGINT, onintr);
                    113: #endif
                    114: #ifdef SIGTERM
                    115:     if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
                    116:        signal(SIGTERM, onintr);
                    117: #endif
                    118: #ifdef SIGHUP
                    119:     if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
                    120:        signal(SIGHUP, onintr);
                    121: #endif
                    122: }
                    123: 
                    124: 
                    125: usage()
                    126: {
                    127:     fprintf(stderr, "usage: %s [-dltv] [-b prefix] filename\n", myname);
                    128:     exit(1);
                    129: }
                    130: 
                    131: 
                    132: getargs(argc, argv)
                    133: int argc;
                    134: char *argv[];
                    135: {
                    136:     register int i;
                    137:     register char *s;
                    138: 
                    139:     if (argc > 0) myname = argv[0];
                    140:     for (i = 1; i < argc; ++i)
                    141:     {
                    142:        s = argv[i];
                    143:        if (*s != '-') break;
                    144:        switch (*++s)
                    145:        {
                    146:        case '\0':
                    147:            input_file = stdin;
                    148:            if (i + 1 < argc) usage();
                    149:            return;
                    150: 
                    151:        case '_':
                    152:            ++i;
                    153:            goto no_more_options;
                    154: 
                    155:        case 'b':
                    156:            if (*++s || ++i >= argc) usage();
                    157:            prefix = argv[i];
                    158:            continue;
                    159: 
                    160:        case 'd':
                    161:            dflag = 1;
                    162:            break;
                    163: 
                    164:        case 'l':
                    165:            lflag = 1;
                    166:            break;
                    167: 
                    168:        case 't':
                    169:            tflag = 1;
                    170:            break;
                    171: 
                    172:        case 'v':
                    173:            vflag = 1;
                    174:            break;
                    175: 
                    176:        default:
                    177:            usage();
                    178:        }
                    179: 
                    180:        for (;;)
                    181:        {
                    182:            switch (*++s)
                    183:            {
                    184:            case '\0':
                    185:                goto end_of_option;
                    186: 
                    187:            case 'd':
                    188:                dflag = 1;
                    189:                break;
                    190: 
                    191:            case 'l':
                    192:                lflag = 1;
                    193:                break;
                    194: 
                    195:            case 't':
                    196:                tflag = 1;
                    197:                break;
                    198: 
                    199:            case 'v':
                    200:                vflag = 1;
                    201:                break;
                    202: 
                    203:            default:
                    204:                usage();
                    205:            }
                    206:        }
                    207: end_of_option:;
                    208:     }
                    209: 
                    210: no_more_options:;
                    211:     if (i + 1 != argc) usage();
                    212:     input_file_name = argv[i];
                    213: }
                    214: 
                    215: 
                    216: char *
                    217: allocate(n)
                    218: unsigned n;
                    219: {
                    220:     register char *p;
                    221: 
                    222:     p = calloc((unsigned) 1, n);
                    223:     if (!p) no_space();
                    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(prefix);
                    273:     if (dflag)
                    274:     {
                    275:        /*  the number 7 below is the size of ".tab.h"; sizeof is not used  */
                    276:        /*  because of a C compiler that thinks sizeof(".tab.h") == 6       */
                    277:        defines_file_name = MALLOC(len + 7);
                    278:        if (defines_file_name == 0) no_space();
                    279:        strcpy(defines_file_name, prefix);
                    280:        strcpy(defines_file_name + len, DEFINES_SUFFIX);
                    281:     }
                    282: 
                    283:     output_file_name = MALLOC(len + 7);
                    284:     if (output_file_name == 0) no_space();
                    285:     strcpy(output_file_name, prefix);
                    286:     strcpy(output_file_name + len, OUTPUT_SUFFIX);
                    287: 
                    288:     if (vflag)
                    289:     {
                    290:        verbose_file_name = MALLOC(len + 8);
                    291:        if (verbose_file_name == 0) no_space();
                    292:        strcpy(verbose_file_name, prefix);
                    293:        strcpy(verbose_file_name + len, VERBOSE_SUFFIX);
                    294:     }
                    295: }
                    296: 
                    297: 
                    298: open_files()
                    299: {
                    300:     create_file_names();
                    301: 
                    302:     if (input_file == 0)
                    303:     {
                    304:        input_file = fopen(input_file_name, "r");
                    305:        if (input_file == 0) open_error(input_file_name);
                    306:     }
                    307: 
                    308:     action_file = fopen(action_file_name, "w");
                    309:     if (action_file == 0) open_error(action_file_name);
                    310: 
                    311:     text_file = fopen(text_file_name, "w");
                    312:     if (text_file == 0) open_error(text_file_name);
                    313: 
                    314:     if (vflag)
                    315:     {
                    316:        verbose_file = fopen(verbose_file_name, "w");
                    317:        if (verbose_file == 0) open_error(verbose_file_name);
                    318:     }
                    319: 
                    320:     if (dflag)
                    321:     {
                    322:        defines_file = fopen(defines_file_name, "w");
                    323:        if (defines_file == 0) open_error(defines_file_name);
                    324:        union_file = fopen(union_file_name, "w");
                    325:        if (union_file ==  0) open_error(union_file_name);
                    326:     }
                    327: 
                    328:     output_file = fopen(output_file_name, "w");
                    329:     if (output_file == 0) open_error(output_file_name);
                    330: }
                    331: 
                    332: 
                    333: int
                    334: main(argc, argv)
                    335: int argc;
                    336: char *argv[];
                    337: {
                    338:     set_signals();
                    339:     getargs(argc, argv);
                    340:     open_files();
                    341:     reader();
                    342:     lr0();
                    343:     lalr();
                    344:     make_parser();
                    345:     verbose();
                    346:     output();
                    347:     done(0);
                    348:     /*NOTREACHED*/
                    349: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.