print.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /* Print information on generated parser, for bison,
  2. Copyright (C) 1984, 1986 Bob Corbett and Free Software Foundation, Inc.
  3. BISON is distributed in the hope that it will be useful, but WITHOUT ANY
  4. WARRANTY. No author or distributor accepts responsibility to anyone
  5. for the consequences of using it or for whether it serves any
  6. particular purpose or works at all, unless he says so in writing.
  7. Refer to the BISON General Public License for full details.
  8. Everyone is granted permission to copy, modify and redistribute BISON,
  9. but only under the conditions described in the BISON General Public
  10. License. A copy of this license is supposed to have been given to you
  11. along with BISON so you can know your rights and responsibilities. It
  12. should be in a file named COPYING. Among other things, the copyright
  13. notice and this notice must be preserved on all copies.
  14. In other words, you are welcome to use, share and improve this program.
  15. You are forbidden to forbid anyone else to use, share and improve
  16. what you give them. Help stamp out software-hoarding! */
  17. #include <stdio.h>
  18. #include "machine.h"
  19. #include "new.h"
  20. #include "files.h"
  21. #include "gram.h"
  22. #include "state.h"
  23. extern char **tags;
  24. extern int nstates;
  25. extern short *accessing_symbol;
  26. extern core **state_table;
  27. extern shifts **shift_table;
  28. extern errs **err_table;
  29. extern r2eductions **reduction_table;
  30. extern char *consistent;
  31. extern char any_conflicts;
  32. extern char *c2onflicts;
  33. terse()
  34. {
  35. if (any_conflicts)
  36. {
  37. conflict_log();
  38. }
  39. }
  40. verbose()
  41. {
  42. register int i;
  43. if (any_conflicts)
  44. verbose_conflict_log();
  45. fprintf(foutput, "\n\ntoken types:\n");
  46. print_token (-1, 0);
  47. if (translations)
  48. {
  49. for (i = 0; i <= max_user_token_number; i++)
  50. /* Don't mention all the meaningless ones. */
  51. if (token_translations[i] != 2)
  52. print_token (i, token_translations[i]);
  53. }
  54. else
  55. for (i = 1; i < ntokens; i++)
  56. print_token (i, i);
  57. for (i = 0; i < nstates; i++)
  58. {
  59. print_state(i);
  60. }
  61. }
  62. print_token(extnum, token)
  63. int extnum, token;
  64. {
  65. fprintf(foutput, " type %d is %s\n", extnum, tags[token]);
  66. }
  67. print_state(state)
  68. int state;
  69. {
  70. fprintf(foutput, "\n\nstate %d\n\n", state);
  71. print_core(state);
  72. print_actions(state);
  73. }
  74. print_core(state)
  75. int state;
  76. {
  77. register int i;
  78. register int k;
  79. register int rule;
  80. register core *statep;
  81. register short *sp;
  82. register short *sp1;
  83. statep = state_table[state];
  84. k = statep->nitems;
  85. if (k == 0) return;
  86. for (i = 0; i < k; i++)
  87. {
  88. sp1 = sp = ritem + statep->items[i];
  89. while (*sp > 0)
  90. sp++;
  91. rule = -(*sp);
  92. fprintf(foutput, " %s -> ", tags[rlhs[rule]]);
  93. for (sp = ritem + rrhs[rule]; sp < sp1; sp++)
  94. {
  95. fprintf(foutput, "%s ", tags[*sp]);
  96. }
  97. putc('.', foutput);
  98. while (*sp > 0)
  99. {
  100. fprintf(foutput, " %s", tags[*sp]);
  101. sp++;
  102. }
  103. fprintf (foutput, " (%d)", rule);
  104. putc('\n', foutput);
  105. }
  106. putc('\n', foutput);
  107. }
  108. print_actions(state)
  109. int state;
  110. {
  111. register int i;
  112. register int k;
  113. register int state1;
  114. register int symbol;
  115. register shifts *shiftp;
  116. register errs *errp;
  117. register r2eductions *redp;
  118. register int rule;
  119. shiftp = shift_table[state];
  120. redp = reduction_table[state];
  121. errp = err_table[state];
  122. if (!shiftp && !redp)
  123. {
  124. fprintf(foutput, " NO ACTIONS\n");
  125. return;
  126. }
  127. if (shiftp)
  128. {
  129. k = shiftp->nshifts;
  130. for (i = 0; i < k; i++)
  131. {
  132. if (! shiftp->shifts[i]) continue;
  133. state1 = shiftp->shifts[i];
  134. symbol = accessing_symbol[state1];
  135. /* if (ISVAR(symbol)) break; */
  136. fprintf(foutput, " %-4s\tshift %d\n", tags[symbol], state1);
  137. }
  138. if (i > 0)
  139. putc('\n', foutput);
  140. }
  141. else
  142. {
  143. i = 0;
  144. k = 0;
  145. }
  146. if (errp)
  147. {
  148. k = errp->nerrs;
  149. for (i = 0; i < k; i++)
  150. {
  151. if (! errp->errs[i]) continue;
  152. symbol = errp->errs[i];
  153. fprintf(foutput, " %-4s\terror (nonassociative)\n", tags[symbol]);
  154. }
  155. if (i > 0)
  156. putc('\n', foutput);
  157. }
  158. else
  159. {
  160. i = 0;
  161. k = 0;
  162. }
  163. if (consistent[state] && redp)
  164. {
  165. rule = redp->rules[0];
  166. symbol = rlhs[rule];
  167. fprintf(foutput, " $default\treduce %d (%s)\n\n",
  168. rule, tags[symbol]);
  169. }
  170. else if (redp)
  171. {
  172. print_reductions(state);
  173. }
  174. if (i < k)
  175. {
  176. for (; i < k; i++)
  177. {
  178. if (! shiftp->shifts[i]) continue;
  179. state1 = shiftp->shifts[i];
  180. symbol = accessing_symbol[state1];
  181. fprintf(foutput, " %-4s\tgoto %d\n", tags[symbol], state1);
  182. }
  183. putc('\n', foutput);
  184. }
  185. }