home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume21 / berkeley_yacc / part01 / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-04-05  |  5.7 KB  |  318 lines

  1. #include <signal.h>
  2. #include "defs.h"
  3.  
  4. char dflag;
  5. char lflag;
  6. char tflag;
  7. char vflag;
  8.  
  9. char *prefix = "y";
  10. char *myname = "yacc";
  11. char *temp_form = "yacc.XXXXXXX";
  12.  
  13. int lineno;
  14. int outline;
  15.  
  16. char *action_file_name;
  17. char *defines_file_name;
  18. char *input_file_name = "";
  19. char *output_file_name;
  20. char *text_file_name;
  21. char *union_file_name;
  22. char *verbose_file_name;
  23.  
  24. FILE *action_file;    /*  a temp file, used to save actions associated    */
  25.             /*  with rules until the parser is written        */
  26. FILE *defines_file;    /*  y.tab.h                        */
  27. FILE *input_file;    /*  the input file                    */
  28. FILE *output_file;    /*  y.tab.c                        */
  29. FILE *text_file;    /*  a temp file, used to save text until all        */
  30.             /*  symbols have been defined                */
  31. FILE *union_file;    /*  a temp file, used to save the union            */
  32.             /*  definition until all symbol have been        */
  33.             /*  defined                        */
  34. FILE *verbose_file;    /*  y.output                        */
  35.  
  36. int nitems;
  37. int nrules;
  38. int nsyms;
  39. int ntokens;
  40. int nvars;
  41.  
  42. int   start_symbol;
  43. char  **symbol_name;
  44. short *symbol_value;
  45. short *symbol_prec;
  46. char  *symbol_assoc;
  47.  
  48. short *ritem;
  49. short *rlhs;
  50. short *rrhs;
  51. short *rprec;
  52. char  *rassoc;
  53. short **derives;
  54. char *nullable;
  55.  
  56. extern char *mktemp();
  57. extern char *getenv();
  58.  
  59.  
  60. done(k)
  61. int k;
  62. {
  63.     if (action_file) { fclose(action_file); unlink(action_file_name); }
  64.     if (text_file) { fclose(text_file); unlink(text_file_name); }
  65.     if (union_file) { fclose(union_file); unlink(union_file_name); }
  66.     exit(k);
  67. }
  68.  
  69.  
  70. onintr()
  71. {
  72.     done(1);
  73. }
  74.  
  75.  
  76. set_signals()
  77. {
  78. #ifdef SIGINT
  79.     if (signal(SIGINT, SIG_IGN) != SIG_IGN)
  80.     signal(SIGINT, onintr);
  81. #endif
  82. #ifdef SIGTERM
  83.     if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
  84.     signal(SIGTERM, onintr);
  85. #endif
  86. #ifdef SIGHUP
  87.     if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
  88.     signal(SIGHUP, onintr);
  89. #endif
  90. }
  91.  
  92.  
  93. usage()
  94. {
  95.     fprintf(stderr, "usage: %s [-dltv] [-b prefix] filename\n", myname);
  96.     exit(1);
  97. }
  98.  
  99.  
  100. getargs(argc, argv)
  101. int argc;
  102. char *argv[];
  103. {
  104.     register int i;
  105.     register char *s;
  106.  
  107.     if (argc > 0) myname = argv[0];
  108.     for (i = 1; i < argc; ++i)
  109.     {
  110.     s = argv[i];
  111.     if (*s != '-') break;
  112.     switch (*++s)
  113.     {
  114.     case '\0':
  115.         input_file = stdin;
  116.         if (i + 1 < argc) usage();
  117.         return;
  118.  
  119.     case '_':
  120.         ++i;
  121.         goto no_more_options;
  122.  
  123.     case 'b':
  124.         if (*++s || ++i >= argc) usage();
  125.         prefix = argv[i];
  126.         continue;
  127.  
  128.     case 'd':
  129.         dflag = 1;
  130.         break;
  131.  
  132.     case 'l':
  133.         lflag = 1;
  134.         break;
  135.  
  136.     case 't':
  137.         tflag = 1;
  138.         break;
  139.  
  140.     case 'v':
  141.         vflag = 1;
  142.         break;
  143.  
  144.     default:
  145.         usage();
  146.     }
  147.  
  148.     for (;;)
  149.     {
  150.         switch (*++s)
  151.         {
  152.         case '\0':
  153.         goto end_of_option;
  154.  
  155.         case 'd':
  156.         dflag = 1;
  157.         break;
  158.  
  159.         case 'l':
  160.         lflag = 1;
  161.         break;
  162.  
  163.         case 't':
  164.         tflag = 1;
  165.         break;
  166.  
  167.         case 'v':
  168.         vflag = 1;
  169.         break;
  170.  
  171.         default:
  172.         usage();
  173.         }
  174.     }
  175. end_of_option:;
  176.     }
  177.  
  178. no_more_options:;
  179.     if (i + 1 != argc) usage();
  180.     input_file_name = argv[i];
  181. }
  182.  
  183.  
  184. char *
  185. allocate(n)
  186. unsigned n;
  187. {
  188.     register char *p;
  189.  
  190.     p = calloc((unsigned) 1, n);
  191.     if (!p) no_space();
  192.     return (p);
  193. }
  194.  
  195.  
  196. create_file_names()
  197. {
  198.     int i, len;
  199.     char *tmpdir;
  200.  
  201.     tmpdir = getenv("TMPDIR");
  202.     if (tmpdir == 0) tmpdir = "/tmp";
  203.  
  204.     len = strlen(tmpdir);
  205.     i = len + 13;
  206.     if (len && tmpdir[len-1] != '/')
  207.     ++i;
  208.  
  209.     action_file_name = MALLOC(i);
  210.     if (action_file_name == 0) no_space();
  211.     text_file_name = MALLOC(i);
  212.     if (text_file_name == 0) no_space();
  213.     union_file_name = MALLOC(i);
  214.     if (union_file_name == 0) no_space();
  215.  
  216.     strcpy(action_file_name, tmpdir);
  217.     strcpy(text_file_name, tmpdir);
  218.     strcpy(union_file_name, tmpdir);
  219.  
  220.     if (len && tmpdir[len - 1] != '/')
  221.     {
  222.     action_file_name[len] = '/';
  223.     text_file_name[len] = '/';
  224.     union_file_name[len] = '/';
  225.     ++len;
  226.     }
  227.  
  228.     strcpy(action_file_name + len, temp_form);
  229.     strcpy(text_file_name + len, temp_form);
  230.     strcpy(union_file_name + len, temp_form);
  231.  
  232.     action_file_name[len + 5] = 'a';
  233.     text_file_name[len + 5] = 't';
  234.     union_file_name[len + 5] = 'u';
  235.  
  236.     mktemp(action_file_name);
  237.     mktemp(text_file_name);
  238.     mktemp(union_file_name);
  239.  
  240.     len = strlen(prefix);
  241.     if (dflag)
  242.     {
  243.     /*  the number 7 below is the size of ".tab.h"; sizeof is not used  */
  244.     /*  because of a C compiler that thinks sizeof(".tab.h") == 6        */
  245.     defines_file_name = MALLOC(len + 7);
  246.     if (defines_file_name == 0) no_space();
  247.     strcpy(defines_file_name, prefix);
  248.     strcpy(defines_file_name + len, DEFINES_SUFFIX);
  249.     }
  250.  
  251.     output_file_name = MALLOC(len + 7);
  252.     if (output_file_name == 0) no_space();
  253.     strcpy(output_file_name, prefix);
  254.     strcpy(output_file_name + len, OUTPUT_SUFFIX);
  255.  
  256.     if (vflag)
  257.     {
  258.     verbose_file_name = MALLOC(len + 8);
  259.     if (verbose_file_name == 0) no_space();
  260.     strcpy(verbose_file_name, prefix);
  261.     strcpy(verbose_file_name + len, VERBOSE_SUFFIX);
  262.     }
  263. }
  264.  
  265.  
  266. open_files()
  267. {
  268.     create_file_names();
  269.  
  270.     if (input_file == 0)
  271.     {
  272.     input_file = fopen(input_file_name, "r");
  273.     if (input_file == 0) open_error(input_file_name);
  274.     }
  275.  
  276.     action_file = fopen(action_file_name, "w");
  277.     if (action_file == 0) open_error(action_file_name);
  278.  
  279.     text_file = fopen(text_file_name, "w");
  280.     if (text_file == 0) open_error(text_file_name);
  281.  
  282.     if (vflag)
  283.     {
  284.     verbose_file = fopen(verbose_file_name, "w");
  285.     if (verbose_file == 0) open_error(verbose_file_name);
  286.     }
  287.  
  288.     if (dflag)
  289.     {
  290.     defines_file = fopen(defines_file_name, "w");
  291.     if (defines_file == 0) open_error(defines_file_name);
  292.     union_file = fopen(union_file_name, "w");
  293.     if (union_file ==  0) open_error(union_file_name);
  294.     }
  295.  
  296.     output_file = fopen(output_file_name, "w");
  297.     if (output_file == 0) open_error(output_file_name);
  298. }
  299.  
  300.  
  301. int
  302. main(argc, argv)
  303. int argc;
  304. char *argv[];
  305. {
  306.     set_signals();
  307.     getargs(argc, argv);
  308.     open_files();
  309.     reader();
  310.     lr0();
  311.     lalr();
  312.     make_parser();
  313.     verbose();
  314.     output();
  315.     done(0);
  316.     /*NOTREACHED*/
  317. }
  318.