home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / program / language / bison / Old_Bison / C / Print < prev    next >
Encoding:
Text File  |  1992-07-08  |  4.3 KB  |  226 lines

  1. /* Print information on generated parser, for bison,
  2.    Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of Bison, the GNU Compiler Compiler.
  5.  
  6. Bison is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. Bison is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with Bison; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include <stdio.h>
  22. #include "system.h"
  23. #include "machine.h"
  24. #include "new.h"
  25. #include "files.h"
  26. #include "gram.h"
  27. #include "state.h"
  28.  
  29.  
  30. extern char **tags;
  31. extern int nstates;
  32. extern short *accessing_symbol;
  33. extern core **state_table;
  34. extern shifts **shift_table;
  35. extern errs **err_table;
  36. extern reductions **reduction_table;
  37. extern char *consistent;
  38. extern char any_conflicts;
  39. extern char *conflicts;
  40.  
  41. static void print_core(int);
  42.  
  43. void terse(void)
  44. {
  45.   if (any_conflicts)
  46.     {
  47.       conflict_log();
  48.     }
  49. }
  50.  
  51.  
  52. void verbose(void)
  53. {
  54.   register int i;
  55.  
  56.   if (any_conflicts)
  57.     verbose_conflict_log();
  58.  
  59.   fprintf(foutput, "\n\ntoken types:\n");
  60.   print_token (-1, 0);
  61.   if (translations)
  62.     {
  63.       for (i = 0; i <= max_user_token_number; i++)
  64.     /* Don't mention all the meaningless ones.  */
  65.     if (token_translations[i] != 2)
  66.       print_token (i, token_translations[i]);
  67.     }
  68.   else
  69.     for (i = 1; i < ntokens; i++)
  70.       print_token (i, i);
  71.  
  72.   for (i = 0; i < nstates; i++)
  73.     {
  74.       print_state(i);
  75.     }
  76. }
  77.  
  78.  
  79. void print_token(int extnum, int token)
  80. {
  81.   fprintf(foutput, " type %d is %s\n", extnum, tags[token]);
  82. }
  83.  
  84.  
  85. void print_state(int state)
  86. {
  87.   fprintf(foutput, "\n\nstate %d\n\n", state);
  88.   print_core(state);
  89.   print_actions(state);
  90. }
  91.  
  92.  
  93. static void print_core(int state)
  94. {
  95.   register int i;
  96.   register int k;
  97.   register int rule;
  98.   register core *statep;
  99.   register short *sp;
  100.   register short *sp1;
  101.  
  102.   statep = state_table[state];
  103.   k = statep->nitems;
  104.  
  105.   if (k == 0) return;
  106.  
  107.   for (i = 0; i < k; i++)
  108.     {
  109.       sp1 = sp = ritem + statep->items[i];
  110.  
  111.       while (*sp > 0)
  112.     sp++;
  113.  
  114.       rule = -(*sp);
  115.       fprintf(foutput, "    %s  ->  ", tags[rlhs[rule]]);
  116.  
  117.       for (sp = ritem + rrhs[rule]; sp < sp1; sp++)
  118.     {
  119.       fprintf(foutput, "%s ", tags[*sp]);
  120.     }
  121.  
  122.       putc('.', foutput);
  123.  
  124.       while (*sp > 0)
  125.     {
  126.       fprintf(foutput, " %s", tags[*sp]);
  127.       sp++;
  128.     }
  129.  
  130.       fprintf (foutput, "   (%d)", rule);
  131.       putc('\n', foutput);
  132.     }
  133.  
  134.   putc('\n', foutput);
  135. }
  136.  
  137.  
  138. void print_actions(int state)
  139. {
  140.   register int i;
  141.   register int k;
  142.   register int state1;
  143.   register int symbol;
  144.   register shifts *shiftp;
  145.   register errs *errp;
  146.   register reductions *redp;
  147.   register int rule;
  148.  
  149.   shiftp = shift_table[state];
  150.   redp = reduction_table[state];
  151.   errp = err_table[state];
  152.  
  153.   if (!shiftp && !redp)
  154.     {
  155.       fprintf(foutput, "    NO ACTIONS\n");
  156.       return;
  157.     }
  158.  
  159.   if (shiftp)
  160.     {
  161.       k = shiftp->nshifts;
  162.  
  163.       for (i = 0; i < k; i++)
  164.     {
  165.       if (! shiftp->shifts[i]) continue;
  166.       state1 = shiftp->shifts[i];
  167.       symbol = accessing_symbol[state1];
  168. /*      if (ISVAR(symbol)) break;  */
  169.       fprintf(foutput, "    %-4s\tshift  %d\n", tags[symbol], state1);
  170.     }
  171.  
  172.       if (i > 0)
  173.     putc('\n', foutput);
  174.     }
  175.   else
  176.     {
  177.       i = 0;
  178.       k = 0;
  179.     }
  180.  
  181.   if (errp)
  182.     {
  183.       k = errp->nerrs;
  184.  
  185.       for (i = 0; i < k; i++)
  186.     {
  187.       if (! errp->errs[i]) continue;
  188.       symbol = errp->errs[i];
  189.       fprintf(foutput, "    %-4s\terror (nonassociative)\n", tags[symbol]);
  190.     }
  191.  
  192.       if (i > 0)
  193.     putc('\n', foutput);
  194.     }
  195.   else
  196.     {
  197.       i = 0;
  198.       k = 0;
  199.     }
  200.  
  201.   if (consistent[state] && redp)
  202.     {
  203.       rule = redp->rules[0];
  204.       symbol = rlhs[rule];
  205.       fprintf(foutput, "    $default\treduce  %d  (%s)\n\n",
  206.                  rule, tags[symbol]);
  207.     }
  208.   else if (redp)
  209.     {
  210.       print_reductions(state);
  211.     }
  212.  
  213.   if (i < k)
  214.     {
  215.       for (; i < k; i++)
  216.     {
  217.       if (! shiftp->shifts[i]) continue;
  218.       state1 = shiftp->shifts[i];
  219.       symbol = accessing_symbol[state1];
  220.       fprintf(foutput, "    %-4s\tgoto  %d\n", tags[symbol], state1);
  221.     }
  222.  
  223.       putc('\n', foutput);
  224.     }
  225. }
  226.