home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD2.bin / bbs / dev / yacc-1.9.lha / Yacc / src / main.c < prev    next >
C/C++ Source or Header  |  1993-07-10  |  9KB  |  429 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 *symbol_prefix;
  11. char *file_prefix = "y";
  12. char *myname = "yacc";
  13. char *temp_form = "yacc.XXXXXXX";
  14.  
  15. int lineno;
  16. int outline;
  17.  
  18. char *action_file_name;
  19. char *code_file_name;
  20. char *defines_file_name;
  21. char *input_file_name = "";
  22. char *output_file_name;
  23. char *text_file_name;
  24. char *union_file_name;
  25. char *verbose_file_name;
  26.  
  27. FILE *action_file;      /*  a temp file, used to save actions associated    */
  28.                         /*  with rules until the parser is written          */
  29. FILE *code_file;        /*  y.code.c (used when the -r option is specified) */
  30. FILE *defines_file;     /*  y.tab.h                                         */
  31. FILE *input_file;       /*  the input file                                  */
  32. FILE *output_file;      /*  y.tab.c                                         */
  33. FILE *text_file;        /*  a temp file, used to save text until all        */
  34.                         /*  symbols have been defined                       */
  35. FILE *union_file;       /*  a temp file, used to save the union             */
  36.                         /*  definition until all symbol have been           */
  37.                         /*  defined                                         */
  38. FILE *verbose_file;     /*  y.output                                        */
  39.  
  40. int nitems;
  41. int nrules;
  42. int nsyms;
  43. int ntokens;
  44. int nvars;
  45.  
  46. int   start_symbol;
  47. char  **symbol_name;
  48. short *symbol_value;
  49. short *symbol_prec;
  50. char  *symbol_assoc;
  51.  
  52. short *ritem;
  53. short *rlhs;
  54. short *rrhs;
  55. short *rprec;
  56. char  *rassoc;
  57. short **derives;
  58. char *nullable;
  59.  
  60. extern char *mktemp();
  61. extern char *getenv();
  62.  
  63.  
  64. done(k)
  65. int k;
  66. {
  67.     if (action_file) { fclose(action_file); unlink(action_file_name); }
  68.     if (text_file) { fclose(text_file); unlink(text_file_name); }
  69.     if (union_file) { fclose(union_file); unlink(union_file_name); }
  70.     exit(k);
  71. }
  72.  
  73.  
  74. void
  75. onintr(signo)
  76.     int signo;
  77. {
  78.     done(1);
  79. }
  80.  
  81.  
  82. set_signals()
  83. {
  84. #ifdef SIGINT
  85.     if (signal(SIGINT, SIG_IGN) != SIG_IGN)
  86.         signal(SIGINT, onintr);
  87. #endif
  88. #ifdef SIGTERM
  89.     if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
  90.         signal(SIGTERM, onintr);
  91. #endif
  92. #ifdef SIGHUP
  93.     if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
  94.         signal(SIGHUP, onintr);
  95. #endif
  96. }
  97.  
  98.  
  99. usage()
  100. {
  101.     fprintf(stderr, "usage: %s [-dlrtv] [-b file_prefix] [-p symbol_prefix] filename\n", myname);
  102.     exit(1);
  103. }
  104.  
  105.  
  106. getargs(argc, argv)
  107. int argc;
  108. char *argv[];
  109. {
  110.     register int i;
  111.     register char *s;
  112.  
  113.     if (argc > 0) myname = argv[0];
  114.     for (i = 1; i < argc; ++i)
  115.     {
  116.         s = argv[i];
  117.         if (*s != '-') break;
  118.         switch (*++s)
  119.         {
  120.         case '\0':
  121.             input_file = stdin;
  122.             if (i + 1 < argc) usage();
  123.             return;
  124.  
  125.         case '-':
  126.             ++i;
  127.             goto no_more_options;
  128.  
  129.         case 'b':
  130.             if (*++s)
  131.                  file_prefix = s;
  132.             else if (++i < argc)
  133.                 file_prefix = argv[i];
  134.             else
  135.                 usage();
  136.             continue;
  137.  
  138.         case 'd':
  139.             dflag = 1;
  140.             break;
  141.  
  142.         case 'l':
  143.             lflag = 1;
  144.             break;
  145.  
  146.         case 'p':
  147.             if (*++s)
  148.                 symbol_prefix = s;
  149.             else if (++i < argc)
  150.                 symbol_prefix = argv[i];
  151.             else
  152.                 usage();
  153.             continue;
  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.     tmpdir = getenv("TMPDIR");
  233. #ifndef AMIGA
  234.     if (tmpdir == 0) tmpdir = "/tmp";
  235. else
  236.     if (tmpdir == 0) tmpdir = "T:";
  237. #endif
  238.  
  239.     len = strlen(tmpdir);
  240.     i = len + 13;
  241. #ifndef AMIGA
  242.     if (len && tmpdir[len-1] != '/')
  243.         ++i;
  244. #else
  245.     if (len && tmpdir[len-1] != '/' && tmpdir[len-1] != ':')
  246.         ++i;
  247. #endif
  248.  
  249.     action_file_name = MALLOC(i);
  250.     if (action_file_name == 0) no_space();
  251.     text_file_name = MALLOC(i);
  252.     if (text_file_name == 0) no_space();
  253.     union_file_name = MALLOC(i);
  254.     if (union_file_name == 0) no_space();
  255.  
  256.     strcpy(action_file_name, tmpdir);
  257.     strcpy(text_file_name, tmpdir);
  258.     strcpy(union_file_name, tmpdir);
  259.  
  260. #ifndef AMIGA
  261.     if (len && tmpdir[len - 1] != '/')
  262.     {
  263.         action_file_name[len] = '/';
  264.         text_file_name[len] = '/';
  265.         union_file_name[len] = '/';
  266.         ++len;
  267.     }
  268. #else
  269.     if (len && tmpdir[len - 1] != '/' && tmpdir[len - 1] != ':')
  270.     {
  271.         action_file_name[len] = '/';
  272.         text_file_name[len] = '/';
  273.         union_file_name[len] = '/';
  274.         ++len;
  275.     }
  276. #endif
  277.  
  278.     strcpy(action_file_name + len, temp_form);
  279.     strcpy(text_file_name + len, temp_form);
  280.     strcpy(union_file_name + len, temp_form);
  281.  
  282.     action_file_name[len + 5] = 'a';
  283.     text_file_name[len + 5] = 't';
  284.     union_file_name[len + 5] = 'u';
  285.  
  286.     mktemp(action_file_name);
  287.     mktemp(text_file_name);
  288.     mktemp(union_file_name);
  289.  
  290.     len = strlen(file_prefix);
  291.  
  292.     output_file_name = MALLOC(len + 7);
  293.     if (output_file_name == 0)
  294.         no_space();
  295.     strcpy(output_file_name, file_prefix);
  296.     strcpy(output_file_name + len, OUTPUT_SUFFIX);
  297.  
  298.     if (rflag)
  299.     {
  300.         code_file_name = MALLOC(len + 8);
  301.         if (code_file_name == 0)
  302.             no_space();
  303.         strcpy(code_file_name, file_prefix);
  304.         strcpy(code_file_name + len, CODE_SUFFIX);
  305.     }
  306.     else
  307.         code_file_name = output_file_name;
  308.  
  309.     if (dflag)
  310.     {
  311.         defines_file_name = MALLOC(len + 7);
  312.         if (defines_file_name == 0)
  313.             no_space();
  314.         strcpy(defines_file_name, file_prefix);
  315.         strcpy(defines_file_name + len, DEFINES_SUFFIX);
  316.     }
  317.  
  318.     if (vflag)
  319.     {
  320.         verbose_file_name = MALLOC(len + 8);
  321.         if (verbose_file_name == 0)
  322.             no_space();
  323.         strcpy(verbose_file_name, file_prefix);
  324.         strcpy(verbose_file_name + len, VERBOSE_SUFFIX);
  325.     }
  326. }
  327.  
  328.  
  329. open_files()
  330. {
  331.     create_file_names();
  332.  
  333.     if (input_file == 0)
  334.     {
  335.         input_file = fopen(input_file_name, "r");
  336.         if (input_file == 0)
  337.             open_error(input_file_name);
  338.     }
  339.  
  340.     action_file = fopen(action_file_name, "w");
  341.     if (action_file == 0)
  342.         open_error(action_file_name);
  343.  
  344.     text_file = fopen(text_file_name, "w");
  345.     if (text_file == 0)
  346.         open_error(text_file_name);
  347.  
  348.     if (vflag)
  349.     {
  350.         verbose_file = fopen(verbose_file_name, "w");
  351.         if (verbose_file == 0)
  352.             open_error(verbose_file_name);
  353.     }
  354.  
  355.     if (dflag)
  356.     {
  357.         defines_file = fopen(defines_file_name, "w");
  358.         if (defines_file == 0)
  359.             open_error(defines_file_name);
  360.         union_file = fopen(union_file_name, "w");
  361.         if (union_file ==  0)
  362.             open_error(union_file_name);
  363.     }
  364.  
  365.     output_file = fopen(output_file_name, "w");
  366.     if (output_file == 0)
  367.         open_error(output_file_name);
  368.  
  369.     if (rflag)
  370.     {
  371.         code_file = fopen(code_file_name, "w");
  372.         if (code_file == 0)
  373.             open_error(code_file_name);
  374.     }
  375.     else
  376.         code_file = output_file;
  377. }
  378.  
  379.  
  380. int
  381. main(argc, argv)
  382. int argc;
  383. char *argv[];
  384. {
  385.     set_signals();
  386.     getargs(argc, argv);
  387.     open_files();
  388.     reader();
  389.     lr0();
  390.     lalr()