home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff299.lzh / Yacc / main.c < prev    next >
C/C++ Source or Header  |  1989-12-30  |  7KB  |  337 lines

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