home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / online / source / c / compilers / Bison.sit.hqx / Bison / Source / print.c < prev    next >
Text File  |  1992-08-21  |  8KB  |  381 lines

  1. /***********************************************************
  2.  *
  3.  * Macintosh/MPW version of GNU Bison 1.18
  4.  * Please read the README_MPW file for more information
  5.  *
  6.  ***********************************************************/
  7.  
  8. /* Print information on generated parser, for bison,
  9.    Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.
  10.  
  11. This file is part of Bison, the GNU Compiler Compiler.
  12.  
  13. Bison is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; either version 2, or (at your option)
  16. any later version.
  17.  
  18. Bison is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. GNU General Public License for more details.
  22.  
  23. You should have received a copy of the GNU General Public License
  24. along with Bison; see the file COPYING.  If not, write to
  25. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  26.  
  27.  
  28. #include <stdio.h>
  29. #include "system.h"
  30. #include "machine.h"
  31. #include "new.h"
  32. #include "files.h"
  33. #include "gram.h"
  34. #include "state.h"
  35.  
  36.  
  37. extern char **tags;
  38. extern int nstates;
  39. extern short *accessing_symbol;
  40. extern core **state_table;
  41. extern shifts **shift_table;
  42. extern errs **err_table;
  43. extern reductions **reduction_table;
  44. extern char *consistent;
  45. extern char any_conflicts;
  46. extern char *conflicts;
  47. extern int final_state;
  48.  
  49. extern void conflict_log();
  50. extern void verbose_conflict_log();
  51. extern void print_reductions();
  52.  
  53. void print_token();
  54. void print_state();
  55. void print_core();
  56. void print_actions();
  57. void print_grammar();
  58.  
  59. void
  60. terse()
  61. {
  62.   if (any_conflicts)
  63.     {
  64.       conflict_log();
  65.     }
  66. }
  67.  
  68.  
  69. void
  70. verbose()
  71. {
  72.   register int i;
  73.  
  74.   if (any_conflicts)
  75.     verbose_conflict_log();
  76.  
  77.   print_grammar();
  78.  
  79.   for (i = 0; i < nstates; i++)
  80.     {
  81.       print_state(i);
  82.     }
  83. }
  84.  
  85.  
  86. void
  87. print_token(extnum, token)
  88. int extnum, token;
  89. {
  90.   fprintf(foutput, " type %d is %s\n", extnum, tags[token]);
  91. }
  92.  
  93.  
  94. void
  95. print_state(state)
  96. int state;
  97. {
  98.   fprintf(foutput, "\n\nstate %d\n\n", state);
  99.   print_core(state);
  100.   print_actions(state);
  101. }
  102.  
  103.  
  104. void
  105. print_core(state)
  106. int state;
  107. {
  108.   register int i;
  109.   register int k;
  110.   register int rule;
  111.   register core *statep;
  112.   register short *sp;
  113.   register short *sp1;
  114.  
  115.   statep = state_table[state];
  116.   k = statep->nitems;
  117.  
  118.   if (k == 0) return;
  119.  
  120.   for (i = 0; i < k; i++)
  121.     {
  122.       sp1 = sp = ritem + statep->items[i];
  123.  
  124.       while (*sp > 0)
  125.     sp++;
  126.  
  127.       rule = -(*sp);
  128.       fprintf(foutput, "    %s  ->  ", tags[rlhs[rule]]);
  129.  
  130.       for (sp = ritem + rrhs[rule]; sp < sp1; sp++)
  131.     {
  132.       fprintf(foutput, "%s ", tags[*sp]);
  133.     }
  134.  
  135.       putc('.', foutput);
  136.  
  137.       while (*sp > 0)
  138.     {
  139.       fprintf(foutput, " %s", tags[*sp]);
  140.       sp++;
  141.     }
  142.  
  143.       fprintf (foutput, "   (rule %d)", rule);
  144.       putc('\n', foutput);
  145.     }
  146.  
  147.   putc('\n', foutput);
  148. }
  149.  
  150.  
  151. void
  152. print_actions(state)
  153. int state;
  154. {
  155.   register int i;
  156.   register int k;
  157.   register int state1;
  158.   register int symbol;
  159.   register shifts *shiftp;
  160.   register errs *errp;
  161.   register reductions *redp;
  162.   register int rule;
  163.  
  164.   shiftp = shift_table[state];
  165.   redp = reduction_table[state];
  166.   errp = err_table[state];
  167.  
  168.   if (!shiftp && !redp)
  169.     {
  170.       if (final_state == state)
  171.     fprintf(foutput, "    $default\taccept\n");
  172.       else
  173.     fprintf(foutput, "    NO ACTIONS\n");
  174.       return;
  175.     }
  176.  
  177.   if (shiftp)
  178.     {
  179.       k = shiftp->nshifts;
  180.  
  181.       for (i = 0; i < k; i++)
  182.     {
  183.       if (! shiftp->shifts[i]) continue;
  184.       state1 = shiftp->shifts[i];
  185.       symbol = accessing_symbol[state1];
  186.       /* The following line used to be turned off.  */
  187.       if (ISVAR(symbol)) break;
  188.           if (symbol==0)      /* I.e. strcmp(tags[symbol],"$")==0 */
  189.             fprintf(foutput, "    $   \tgo to state %d\n", state1);
  190.           else
  191.             fprintf(foutput, "    %-4s\tshift, and go to state %d\n",
  192.                     tags[symbol], state1);
  193.     }
  194.  
  195.       if (i > 0)
  196.     putc('\n', foutput);
  197.     }
  198.   else
  199.     {
  200.       i = 0;
  201.       k = 0;
  202.     }
  203.  
  204.   if (errp)
  205.     {
  206.       int j, nerrs;
  207.  
  208.       nerrs = errp->nerrs;
  209.  
  210.       for (j = 0; j < nerrs; j++)
  211.     {
  212.       if (! errp->errs[j]) continue;
  213.       symbol = errp->errs[j];
  214.       fprintf(foutput, "    %-4s\terror (nonassociative)\n", tags[symbol]);
  215.     }
  216.  
  217.       if (j > 0)
  218.     putc('\n', foutput);
  219.     }
  220.  
  221.   if (consistent[state] && redp)
  222.     {
  223.       rule = redp->rules[0];
  224.       symbol = rlhs[rule];
  225.       fprintf(foutput, "    $default\treduce using rule %d (%s)\n\n",
  226.                  rule, tags[symbol]);
  227.     }
  228.   else if (redp)
  229.     {
  230.       print_reductions(state);
  231.     }
  232.  
  233.   if (i < k)
  234.     {
  235.       for (; i < k; i++)
  236.     {
  237.       if (! shiftp->shifts[i]) continue;
  238.       state1 = shiftp->shifts[i];
  239.       symbol = accessing_symbol[state1];
  240.       fprintf(foutput, "    %-4s\tgo to state %d\n", tags[symbol], state1);
  241.     }
  242.  
  243.       putc('\n', foutput);
  244.     }
  245. }
  246.  
  247. #define END_TEST(end) \
  248.   if (column + strlen(buffer) > (end))                     \
  249.     { fprintf (foutput, "%s\n   ", buffer); column = 3; buffer[0] = 0; } \
  250.   else
  251.  
  252. void
  253. print_grammar()
  254. {
  255.   int i, j;
  256.   short* rule;
  257.   char buffer[90];
  258.   int column = 0;
  259.  
  260.   /* rule # : LHS -> RHS */
  261.   fputs("\nGrammar\n", foutput);
  262.   for (i = 1; i <= nrules; i++)
  263.     /* Don't print rules disabled in reduce_grammar_tables.  */
  264.     if (rlhs[i] >= 0)
  265.       {
  266.     fprintf(foutput, "rule %-4d %s ->", i, tags[rlhs[i]]);
  267.     rule = &ritem[rrhs[i]];
  268.     if (*rule > 0)
  269.       while (*rule > 0)
  270.         fprintf(foutput, " %s", tags[*rule++]);
  271.     else
  272.       fputs ("        /* empty */", foutput);
  273.     putc('\n', foutput);
  274.       }
  275.  
  276.   /* TERMINAL (type #) : rule #s terminal is on RHS */
  277.   fputs("\nTerminals, with rules where they appear\n\n", foutput);
  278.   fprintf(foutput, "%s (-1)\n", tags[0]);
  279.   if (translations)
  280.     {
  281.       for (i = 0; i <= max_user_token_number; i++)
  282.     if (token_translations[i] != 2)
  283.       {
  284.         buffer[0] = 0;
  285.         column = strlen (tags[token_translations[i]]);
  286.         fprintf(foutput, "%s", tags[token_translations[i]]);
  287.         END_TEST (50);
  288.         sprintf (buffer, " (%d)", i);
  289.  
  290.         for (j = 1; j <= nrules; j++)
  291.           {
  292.         for (rule = &ritem[rrhs[j]]; *rule > 0; rule++)
  293.           if (*rule == token_translations[i])
  294.             {
  295.               END_TEST (65);
  296.               sprintf (buffer + strlen(buffer), " %d", j);
  297.               break;
  298.             }
  299.           }
  300.         fprintf (foutput, "%s\n", buffer);
  301.       }
  302.     }
  303.   else
  304.     for (i = 1; i < ntokens; i++)
  305.       {
  306.     buffer[0] = 0;
  307.     column = strlen (tags[i]);
  308.     fprintf(foutput, "%s", tags[i]);
  309.     END_TEST (50);
  310.     sprintf (buffer, " (%d)", i);
  311.  
  312.     for (j = 1; j <= nrules; j++)
  313.       {
  314.         for (rule = &ritem[rrhs[j]]; *rule > 0; rule++)
  315.           if (*rule == i)
  316.         {
  317.           END_TEST (65);
  318.           sprintf (buffer + strlen(buffer), " %d", j);
  319.           break;
  320.         }
  321.       }
  322.     fprintf (foutput, "%s\n", buffer);
  323.       }
  324.  
  325.   fputs("\nNonterminals, with rules where they appear\n\n", foutput);
  326.   for (i = ntokens; i <= nsyms - 1; i++)
  327.     {
  328.       int left_count = 0, right_count = 0;
  329.  
  330.       for (j = 1; j <= nrules; j++)
  331.     {
  332.       if (rlhs[j] == i)
  333.         left_count++;
  334.       for (rule = &ritem[rrhs[j]]; *rule > 0; rule++)
  335.         if (*rule == i)
  336.           {
  337.         right_count++;
  338.         break;
  339.           }
  340.     }
  341.  
  342.       buffer[0] = 0;
  343.       fprintf(foutput, "%s", tags[i]);
  344.       column = strlen (tags[i]);
  345.       sprintf (buffer, " (%d)", i);
  346.       END_TEST (0);
  347.  
  348.       if (left_count > 0)
  349.     {
  350.       END_TEST (50);
  351.       sprintf (buffer + strlen(buffer), " on left:");
  352.  
  353.       for (j = 1; j <= nrules; j++)
  354.         {
  355.           END_TEST (65);
  356.           if (rlhs[j] == i)
  357.         sprintf (buffer + strlen(buffer), " %d", j);
  358.         }
  359.     }
  360.  
  361.       if (right_count > 0)
  362.     {
  363.       if (left_count > 0)
  364.         sprintf (buffer + strlen(buffer), ",");
  365.       END_TEST (50);
  366.       sprintf (buffer + strlen(buffer), " on right:");
  367.       for (j = 1; j <= nrules; j++)
  368.         {
  369.           for (rule = &ritem[rrhs[j]]; *rule > 0; rule++)
  370.         if (*rule == i)
  371.           {
  372.             END_TEST (65);
  373.             sprintf (buffer + strlen(buffer), " %d", j);
  374.             break;
  375.           }
  376.         }
  377.     }
  378.       fprintf (foutput, "%s\n", buffer);
  379.     }
  380. }
  381.