home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / BYACC.ZIP / MAIN.C < prev    next >
C/C++ Source or Header  |  1992-03-16  |  6KB  |  315 lines

  1. #include <signal.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <io.h>
  5. #include "byacc.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 = "ycXXXXXX";
  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. extern char *mktemp();
  60. extern char *getenv();
  61.  
  62.  
  63. void done(int k)
  64. {
  65.     if (action_file) unlink(action_file_name);
  66.     if (text_file) unlink(text_file_name);
  67.     if (union_file) unlink(union_file_name);
  68.     exit(k);
  69. }
  70.  
  71.  
  72. void onintr(void)
  73. {
  74.     done(1);
  75. }
  76.  
  77.  
  78. void set_signals(void)
  79. {
  80.     if (signal(SIGINT, SIG_IGN) == SIG_IGN)
  81.     signal(SIGINT, onintr);
  82.     if (signal(SIGTERM, SIG_IGN) == SIG_IGN)
  83.     signal(SIGTERM, onintr);
  84. }
  85.  
  86.  
  87. void usage(void)
  88. {
  89.     fprintf(stderr, "usage: %s [-dltv] [-b prefix] filename\n", myname);
  90.     exit(1);
  91. }
  92.  
  93.  
  94. void getargs(int argc, char ** argv)
  95. {
  96.     register int i;
  97.     register char *s;
  98.  
  99.     if (argc > 0) myname = argv[0];
  100.     for (i = 1; i < argc; ++i)
  101.     {
  102.     s = argv[i];
  103.     if (*s != '-') break;
  104.     switch (*++s)
  105.     {
  106.     case '\0':
  107.         input_file = stdin;
  108.         if (i + 1 < argc) usage();
  109.         return;
  110.  
  111.     case '_':
  112.         ++i;
  113.         goto no_more_options;
  114.  
  115.     case 'b':
  116.         if (*++s || ++i >= argc) usage();
  117.         prefix = argv[i];
  118.         continue;
  119.  
  120.     case 'd':
  121.         dflag = 1;
  122.         break;
  123.  
  124.     case 'l':
  125.         lflag = 1;
  126.         break;
  127.  
  128.     case 't':
  129.         tflag = 1;
  130.         break;
  131.  
  132.     case 'v':
  133.         vflag = 1;
  134.         break;
  135.  
  136.     default:
  137.         usage();
  138.     }
  139.  
  140.     for (;;)
  141.     {
  142.         switch (*++s)
  143.         {
  144.         case '\0':
  145.         goto end_of_option;
  146.  
  147.         case 'd':
  148.         dflag = 1;
  149.         break;
  150.  
  151.         case 'l':
  152.         lflag = 1;
  153.         break;
  154.  
  155.         case 't':
  156.         tflag = 1;
  157.         break;
  158.  
  159.         case 'v':
  160.         vflag = 1;
  161.         break;
  162.  
  163.         default:
  164.         usage();
  165.         }
  166.     }
  167. end_of_option:;
  168.     }
  169.  
  170. no_more_options:;
  171.     if (i + 1 != argc) usage();
  172.     input_file_name = argv[i];
  173. }
  174.  
  175.  
  176. char *
  177. allocate(unsigned n)
  178. {
  179.     register char *p;
  180.  
  181.     p = calloc((unsigned) 1, n);
  182.     if (!p) no_space();
  183.     return (p);
  184. }
  185.  
  186.  
  187. void create_file_names(void)
  188. {
  189.     int i, len;
  190.     char *tmpdir;
  191.  
  192.     tmpdir = getenv("TMPDIR");
  193.     if (tmpdir == 0) tmpdir = "/tmp";
  194.  
  195.     if (*tmpdir)
  196.     {
  197.     len = 0;
  198.     while (tmpdir[len]) ++len;
  199.     i = len + 14 + (tmpdir[len - 1] == '/');
  200.     }
  201.     else
  202.     {
  203.     i = 14;
  204.     }
  205.  
  206.     action_file_name = MALLOC(i);
  207.     if (action_file_name == 0) no_space();
  208.     text_file_name = MALLOC(i);
  209.     if (text_file_name == 0) no_space();
  210.     union_file_name = MALLOC(i);
  211.     if (union_file_name == 0) no_space();
  212.  
  213.     strcpy(action_file_name, tmpdir);
  214.     strcpy(text_file_name, tmpdir);
  215.     strcpy(union_file_name, tmpdir);
  216.  
  217.     if (*tmpdir && tmpdir[len - 1] != '/')
  218.     {
  219.     action_file_name[len] = '/';
  220.     text_file_name[len] = '/';
  221.     union_file_name[len] = '/';
  222.     ++len;
  223.     }
  224.  
  225.     strcpy(action_file_name + len, temp_form);
  226.     strcpy(text_file_name + len, temp_form);
  227.     strcpy(union_file_name + len, temp_form);
  228.  
  229.     action_file_name[len + 2] = 'a';
  230.     text_file_name[len + 2] = 't';
  231.     union_file_name[len + 2] = 'u';
  232.  
  233.     mktemp(action_file_name);
  234.     mktemp(text_file_name);
  235.     mktemp(union_file_name);
  236.  
  237.     len = strlen(prefix);
  238.     if (dflag)
  239.     {
  240.     /*  the number 7 below is the size of ".tab.h"; sizeof is not used  */
  241.     /*  because of a C compiler that thinks sizeof(".tab.h") == 6        */
  242.     defines_file_name = MALLOC(len + 7);
  243.     if (defines_file_name == 0) no_space();
  244.     strcpy(defines_file_name, prefix);
  245.     strcpy(defines_file_name + len, DEFINES_SUFFIX);
  246.     }
  247.  
  248.     output_file_name = MALLOC(len + 7);
  249.     if (output_file_name == 0) no_space();
  250.     strcpy(output_file_name, prefix);
  251.     strcpy(output_file_name + len, OUTPUT_SUFFIX);
  252.  
  253.     if (vflag)
  254.     {
  255.     verbose_file_name = MALLOC(len + 8);
  256.     if (verbose_file_name == 0) no_space();
  257.     strcpy(verbose_file_name, prefix);
  258.     strcpy(verbose_file_name + len, VERBOSE_SUFFIX);
  259.     }
  260. }
  261.  
  262.  
  263. void open_files(void)
  264. {
  265.     create_file_names();
  266.  
  267.     if (input_file == 0)
  268.     {
  269.     input_file = fopen(input_file_name, "r");
  270.     if (input_file == 0) open_error(input_file_name);
  271.     }
  272.  
  273.     action_file = fopen(action_file_name, "w");
  274.     if (action_file == 0) open_error(action_file_name);
  275.  
  276.     text_file = fopen(text_file_name, "w");
  277.     if (text_file == 0) open_error(text_file_name);
  278.  
  279.     if (vflag)
  280.     {
  281.     verbose_file = fopen(verbose_file_name, "w");
  282.     if (verbose_file == 0) open_error(verbose_file_name);
  283.     }
  284.  
  285.     if (dflag)
  286.     {
  287.     defines_file = fopen(defines_file_name, "w");
  288.     if (defines_file == 0) open_error(defines_file_name);
  289.     union_file = fopen(union_file_name, "w");
  290.     if (union_file ==  0) open_error(union_file_name);
  291.     }
  292.  
  293.     output_file = fopen(output_file_name, "w");
  294.     if (output_file == 0) open_error(output_file_name);
  295. }
  296.  
  297.  
  298. int
  299. main(int argc, char **argv)
  300. {
  301.     register int c;
  302.  
  303.     set_signals();
  304.     getargs(argc, argv);
  305.     open_files();
  306.     reader();
  307.     lr0();
  308.     lalr();
  309.     make_parser();
  310.     verbose();
  311.     output();
  312.     done(0);
  313.     /*NOTREACHED*/
  314. }
  315.