home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / compcomp / byacc / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-08  |  8.6 KB  |  380 lines

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