PRINT.C 4.7 KB

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