print.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 2, 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 int final_state;
  33. extern void conflict_log();
  34. extern void verbose_conflict_log();
  35. extern void print_reductions();
  36. void print_token();
  37. void print_state();
  38. void print_core();
  39. void print_actions();
  40. void print_grammar();
  41. void
  42. terse()
  43. {
  44. if (any_conflicts)
  45. {
  46. conflict_log();
  47. }
  48. }
  49. void
  50. verbose()
  51. {
  52. register int i;
  53. if (any_conflicts)
  54. verbose_conflict_log();
  55. print_grammar();
  56. for (i = 0; i < nstates; i++)
  57. {
  58. print_state(i);
  59. }
  60. }
  61. void
  62. print_token(extnum, token)
  63. int extnum, token;
  64. {
  65. fprintf(foutput, " type %d is %s\n", extnum, tags[token]);
  66. }
  67. void
  68. print_state(state)
  69. int state;
  70. {
  71. fprintf(foutput, "\n\nstate %d\n\n", state);
  72. print_core(state);
  73. print_actions(state);
  74. }
  75. void
  76. print_core(state)
  77. int state;
  78. {
  79. register int i;
  80. register int k;
  81. register int rule;
  82. register core *statep;
  83. register short *sp;
  84. register short *sp1;
  85. statep = state_table[state];
  86. k = statep->nitems;
  87. if (k == 0) return;
  88. for (i = 0; i < k; i++)
  89. {
  90. sp1 = sp = ritem + statep->items[i];
  91. while (*sp > 0)
  92. sp++;
  93. rule = -(*sp);
  94. fprintf(foutput, " %s -> ", tags[rlhs[rule]]);
  95. for (sp = ritem + rrhs[rule]; sp < sp1; sp++)
  96. {
  97. fprintf(foutput, "%s ", tags[*sp]);
  98. }
  99. putc('.', foutput);
  100. while (*sp > 0)
  101. {
  102. fprintf(foutput, " %s", tags[*sp]);
  103. sp++;
  104. }
  105. fprintf (foutput, " (rule %d)", rule);
  106. putc('\n', foutput);
  107. }
  108. putc('\n', foutput);
  109. }
  110. void
  111. print_actions(state)
  112. int state;
  113. {
  114. register int i;
  115. register int k;
  116. register int state1;
  117. register int symbol;
  118. register shifts *shiftp;
  119. register errs *errp;
  120. register reductions *redp;
  121. register int rule;
  122. shiftp = shift_table[state];
  123. redp = reduction_table[state];
  124. errp = err_table[state];
  125. if (!shiftp && !redp)
  126. {
  127. if (final_state == state)
  128. fprintf(foutput, " $default\taccept\n");
  129. else
  130. fprintf(foutput, " NO ACTIONS\n");
  131. return;
  132. }
  133. if (shiftp)
  134. {
  135. k = shiftp->nshifts;
  136. for (i = 0; i < k; i++)
  137. {
  138. if (! shiftp->shifts[i]) continue;
  139. state1 = shiftp->shifts[i];
  140. symbol = accessing_symbol[state1];
  141. /* The following line used to be turned off. */
  142. if (ISVAR(symbol)) break;
  143. if (symbol==0) /* I.e. strcmp(tags[symbol],"$")==0 */
  144. fprintf(foutput, " $ \tgo to state %d\n", state1);
  145. else
  146. fprintf(foutput, " %-4s\tshift, and go to state %d\n",
  147. tags[symbol], state1);
  148. }
  149. if (i > 0)
  150. putc('\n', foutput);
  151. }
  152. else
  153. {
  154. i = 0;
  155. k = 0;
  156. }
  157. if (errp)
  158. {
  159. int j, nerrs;
  160. nerrs = errp->nerrs;
  161. for (j = 0; j < nerrs; j++)
  162. {
  163. if (! errp->errs[j]) continue;
  164. symbol = errp->errs[j];
  165. fprintf(foutput, " %-4s\terror (nonassociative)\n", tags[symbol]);
  166. }
  167. if (j > 0)
  168. putc('\n', foutput);
  169. }
  170. if (consistent[state] && redp)
  171. {
  172. rule = redp->rules[0];
  173. symbol = rlhs[rule];
  174. fprintf(foutput, " $default\treduce using rule %d (%s)\n\n",
  175. rule, tags[symbol]);
  176. }
  177. else if (redp)
  178. {
  179. print_reductions(state);
  180. }
  181. if (i < k)
  182. {
  183. for (; i < k; i++)
  184. {
  185. if (! shiftp->shifts[i]) continue;
  186. state1 = shiftp->shifts[i];
  187. symbol = accessing_symbol[state1];
  188. fprintf(foutput, " %-4s\tgo to state %d\n", tags[symbol], state1);
  189. }
  190. putc('\n', foutput);
  191. }
  192. }
  193. #define END_TEST(end) \
  194. if (column + strlen(buffer) > (end)) \
  195. { fprintf (foutput, "%s\n ", buffer); column = 3; buffer[0] = 0; } \
  196. else
  197. void
  198. print_grammar()
  199. {
  200. int i, j;
  201. short* rule;
  202. char buffer[90];
  203. int column = 0;
  204. /* rule # : LHS -> RHS */
  205. fputs("\nGrammar\n", foutput);
  206. for (i = 1; i <= nrules; i++)
  207. /* Don't print rules disabled in reduce_grammar_tables. */
  208. if (rlhs[i] >= 0)
  209. {
  210. fprintf(foutput, "rule %-4d %s ->", i, tags[rlhs[i]]);
  211. rule = &ritem[rrhs[i]];
  212. if (*rule > 0)
  213. while (*rule > 0)
  214. fprintf(foutput, " %s", tags[*rule++]);
  215. else
  216. fputs (" /* empty */", foutput);
  217. putc('\n', foutput);
  218. }
  219. /* TERMINAL (type #) : rule #s terminal is on RHS */
  220. fputs("\nTerminals, with rules where they appear\n\n", foutput);
  221. fprintf(foutput, "%s (-1)\n", tags[0]);
  222. if (translations)
  223. {
  224. for (i = 0; i <= max_user_token_number; i++)
  225. if (token_translations[i] != 2)
  226. {
  227. buffer[0] = 0;
  228. column = strlen (tags[token_translations[i]]);
  229. fprintf(foutput, "%s", tags[token_translations[i]]);
  230. END_TEST (50);
  231. sprintf (buffer, " (%d)", i);
  232. for (j = 1; j <= nrules; j++)
  233. {
  234. for (rule = &ritem[rrhs[j]]; *rule > 0; rule++)
  235. if (*rule == token_translations[i])
  236. {
  237. END_TEST (65);
  238. sprintf (buffer + strlen(buffer), " %d", j);
  239. break;
  240. }
  241. }
  242. fprintf (foutput, "%s\n", buffer);
  243. }
  244. }
  245. else
  246. for (i = 1; i < ntokens; i++)
  247. {
  248. buffer[0] = 0;
  249. column = strlen (tags[i]);
  250. fprintf(foutput, "%s", tags[i]);
  251. END_TEST (50);
  252. sprintf (buffer, " (%d)", i);
  253. for (j = 1; j <= nrules; j++)
  254. {
  255. for (rule = &ritem[rrhs[j]]; *rule > 0; rule++)
  256. if (*rule == i)
  257. {
  258. END_TEST (65);
  259. sprintf (buffer + strlen(buffer), " %d", j);
  260. break;
  261. }
  262. }
  263. fprintf (foutput, "%s\n", buffer);
  264. }
  265. fputs("\nNonterminals, with rules where they appear\n\n", foutput);
  266. for (i = ntokens; i <= nsyms - 1; i++)
  267. {
  268. int left_count = 0, right_count = 0;
  269. for (j = 1; j <= nrules; j++)
  270. {
  271. if (rlhs[j] == i)
  272. left_count++;
  273. for (rule = &ritem[rrhs[j]]; *rule > 0; rule++)
  274. if (*rule == i)
  275. {
  276. right_count++;
  277. break;
  278. }
  279. }
  280. buffer[0] = 0;
  281. fprintf(foutput, "%s", tags[i]);
  282. column = strlen (tags[i]);
  283. sprintf (buffer, " (%d)", i);
  284. END_TEST (0);
  285. if (left_count > 0)
  286. {
  287. END_TEST (50);
  288. sprintf (buffer + strlen(buffer), " on left:");
  289. for (j = 1; j <= nrules; j++)
  290. {
  291. END_TEST (65);
  292. if (rlhs[j] == i)
  293. sprintf (buffer + strlen(buffer), " %d", j);
  294. }
  295. }
  296. if (right_count > 0)
  297. {
  298. if (left_count > 0)
  299. sprintf (buffer + strlen(buffer), ",");
  300. END_TEST (50);
  301. sprintf (buffer + strlen(buffer), " on right:");
  302. for (j = 1; j <= nrules; j++)
  303. {
  304. for (rule = &ritem[rrhs[j]]; *rule > 0; rule++)
  305. if (*rule == i)
  306. {
  307. END_TEST (65);
  308. sprintf (buffer + strlen(buffer), " %d", j);
  309. break;
  310. }
  311. }
  312. }
  313. fprintf (foutput, "%s\n", buffer);
  314. }
  315. }