home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / BYACC.ZIP / RCS / MAIN.C_V < prev    next >
Text File  |  1992-06-10  |  6KB  |  341 lines

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