home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / online / source / c / compilers / mpw-perl-byacc1.8.1.sit.hqx / mpw-perl-byacc1.8.1 / main.c < prev    next >
C/C++ Source or Header  |  1992-10-20  |  8KB  |  413 lines

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