Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

384 lines
8.6 KiB

  1. #include "defs.h"
  2. #if defined(KYLEP_CHANGE)
  3. /* BYACC prototypes, with type safety */
  4. void print_state( int state );
  5. void log_unused();
  6. void log_conflicts();
  7. void print_conflicts( int state );
  8. void print_core( int state );
  9. void print_nulls( int state );
  10. void print_actions( int stateno );
  11. void print_shifts( register action *p );
  12. void print_reductions( register action *p, register int defred );
  13. void print_gotos( int stateno );
  14. #endif // KYLEP_CHANGE
  15. static short *null_rules;
  16. #if defined(KYLEP_CHANGE)
  17. void
  18. #endif
  19. verbose()
  20. {
  21. register int i;
  22. if (!vflag) return;
  23. null_rules = (short *) MALLOC(nrules*sizeof(short));
  24. if (null_rules == 0) no_space();
  25. fprintf(verbose_file, "\f\n");
  26. for (i = 0; i < nstates; i++)
  27. print_state(i);
  28. FREE(null_rules);
  29. if (nunused)
  30. log_unused();
  31. if (SRtotal || RRtotal)
  32. log_conflicts();
  33. fprintf(verbose_file, "\n\n%d terminals, %d nonterminals\n", ntokens,
  34. nvars);
  35. fprintf(verbose_file, "%d grammar rules, %d states\n", nrules - 2, nstates);
  36. }
  37. #if defined(KYLEP_CHANGE)
  38. void
  39. #endif
  40. log_unused()
  41. {
  42. register int i;
  43. register short *p;
  44. fprintf(verbose_file, "\n\nRules never reduced:\n");
  45. for (i = 3; i < nrules; ++i)
  46. {
  47. if (!rules_used[i])
  48. {
  49. fprintf(verbose_file, "\t%s :", symbol_name[rlhs[i]]);
  50. for (p = ritem + rrhs[i]; *p >= 0; ++p)
  51. fprintf(verbose_file, " %s", symbol_name[*p]);
  52. fprintf(verbose_file, " (%d)\n", i - 2);
  53. }
  54. }
  55. }
  56. #if defined(KYLEP_CHANGE)
  57. void
  58. #endif
  59. log_conflicts()
  60. {
  61. register int i;
  62. fprintf(verbose_file, "\n\n");
  63. for (i = 0; i < nstates; i++)
  64. {
  65. if (SRconflicts[i] || RRconflicts[i])
  66. {
  67. fprintf(verbose_file, "State %d contains ", i);
  68. if (SRconflicts[i] == 1)
  69. fprintf(verbose_file, "1 shift/reduce conflict");
  70. else if (SRconflicts[i] > 1)
  71. fprintf(verbose_file, "%d shift/reduce conflicts",
  72. SRconflicts[i]);
  73. if (SRconflicts[i] && RRconflicts[i])
  74. fprintf(verbose_file, ", ");
  75. if (RRconflicts[i] == 1)
  76. fprintf(verbose_file, "1 reduce/reduce conflict");
  77. else if (RRconflicts[i] > 1)
  78. fprintf(verbose_file, "%d reduce/reduce conflicts",
  79. RRconflicts[i]);
  80. fprintf(verbose_file, ".\n");
  81. }
  82. }
  83. }
  84. #if defined(KYLEP_CHANGE)
  85. void
  86. #endif
  87. print_state(state)
  88. int state;
  89. {
  90. if (state)
  91. fprintf(verbose_file, "\n\n");
  92. if (SRconflicts[state] || RRconflicts[state])
  93. print_conflicts(state);
  94. fprintf(verbose_file, "state %d\n", state);
  95. print_core(state);
  96. print_nulls(state);
  97. print_actions(state);
  98. }
  99. #if defined(KYLEP_CHANGE)
  100. void
  101. #endif
  102. print_conflicts(state)
  103. int state;
  104. {
  105. register int symbol, act, number;
  106. register action *p;
  107. symbol = -1;
  108. for (p = parser[state]; p; p = p->next)
  109. {
  110. if (p->suppressed == 2)
  111. continue;
  112. if (p->symbol != symbol)
  113. {
  114. symbol = p->symbol;
  115. number = p->number;
  116. if (p->action_code == SHIFT)
  117. act = SHIFT;
  118. else
  119. act = REDUCE;
  120. }
  121. else if (p->suppressed == 1)
  122. {
  123. if (state == final_state && symbol == 0)
  124. {
  125. fprintf(verbose_file, "%d: shift/reduce conflict \
  126. (accept, reduce %d) on $end\n", state, p->number - 2);
  127. }
  128. else
  129. {
  130. if (act == SHIFT)
  131. {
  132. fprintf(verbose_file, "%d: shift/reduce conflict \
  133. (shift %d, reduce %d) on %s\n", state, number, p->number - 2,
  134. symbol_name[symbol]);
  135. }
  136. else
  137. {
  138. fprintf(verbose_file, "%d: reduce/reduce conflict \
  139. (reduce %d, reduce %d) on %s\n", state, number - 2, p->number - 2,
  140. symbol_name[symbol]);
  141. }
  142. }
  143. }
  144. }
  145. }
  146. #if defined(KYLEP_CHANGE)
  147. void
  148. #endif
  149. print_core(state)
  150. int state;
  151. {
  152. register int i;
  153. register int k;
  154. register int rule;
  155. register core *statep;
  156. register short *sp;
  157. register short *sp1;
  158. statep = state_table[state];
  159. k = statep->nitems;
  160. for (i = 0; i < k; i++)
  161. {
  162. sp1 = sp = ritem + statep->items[i];
  163. while (*sp >= 0) ++sp;
  164. rule = -(*sp);
  165. fprintf(verbose_file, "\t%s : ", symbol_name[rlhs[rule]]);
  166. for (sp = ritem + rrhs[rule]; sp < sp1; sp++)
  167. fprintf(verbose_file, "%s ", symbol_name[*sp]);
  168. putc('.', verbose_file);
  169. while (*sp >= 0)
  170. {
  171. fprintf(verbose_file, " %s", symbol_name[*sp]);
  172. sp++;
  173. }
  174. fprintf(verbose_file, " (%d)\n", -2 - *sp);
  175. }
  176. }
  177. #if defined(KYLEP_CHANGE)
  178. void
  179. #endif
  180. print_nulls(state)
  181. int state;
  182. {
  183. register action *p;
  184. register int i, j, k, nnulls;
  185. nnulls = 0;
  186. for (p = parser[state]; p; p = p->next)
  187. {
  188. if (p->action_code == REDUCE &&
  189. (p->suppressed == 0 || p->suppressed == 1))
  190. {
  191. i = p->number;
  192. if (rrhs[i] + 1 == rrhs[i+1])
  193. {
  194. for (j = 0; j < nnulls && i > null_rules[j]; ++j)
  195. continue;
  196. if (j == nnulls)
  197. {
  198. ++nnulls;
  199. #if defined(KYLEP_CHANGE)
  200. null_rules[j] = (short) i;
  201. #else
  202. null_rules[j] = i;
  203. #endif // KYLEP_CHANGE
  204. }
  205. else if (i != null_rules[j])
  206. {
  207. ++nnulls;
  208. for (k = nnulls - 1; k > j; --k)
  209. null_rules[k] = null_rules[k-1];
  210. #if defined(KYLEP_CHANGE)
  211. null_rules[j] = (short) i;
  212. #else
  213. null_rules[j] = i;
  214. #endif // KYLEP_CHANGE
  215. }
  216. }
  217. }
  218. }
  219. for (i = 0; i < nnulls; ++i)
  220. {
  221. j = null_rules[i];
  222. fprintf(verbose_file, "\t%s : . (%d)\n", symbol_name[rlhs[j]],
  223. j - 2);
  224. }
  225. fprintf(verbose_file, "\n");
  226. }
  227. #if defined(KYLEP_CHANGE)
  228. void
  229. #endif
  230. print_actions(stateno)
  231. int stateno;
  232. {
  233. register action *p;
  234. register shifts *sp;
  235. register int as;
  236. if (stateno == final_state)
  237. fprintf(verbose_file, "\t$end accept\n");
  238. p = parser[stateno];
  239. if (p)
  240. {
  241. print_shifts(p);
  242. print_reductions(p, defred[stateno]);
  243. }
  244. sp = shift_table[stateno];
  245. if (sp && sp->nshifts > 0)
  246. {
  247. as = accessing_symbol[sp->shift[sp->nshifts - 1]];
  248. if (ISVAR(as))
  249. print_gotos(stateno);
  250. }
  251. }
  252. #if defined(KYLEP_CHANGE)
  253. void
  254. #endif
  255. print_shifts(p)
  256. register action *p;
  257. {
  258. register int count;
  259. register action *q;
  260. count = 0;
  261. for (q = p; q; q = q->next)
  262. {
  263. if (q->suppressed < 2 && q->action_code == SHIFT)
  264. ++count;
  265. }
  266. if (count > 0)
  267. {
  268. for (; p; p = p->next)
  269. {
  270. if (p->action_code == SHIFT && p->suppressed == 0)
  271. fprintf(verbose_file, "\t%s shift %d\n",
  272. symbol_name[p->symbol], p->number);
  273. }
  274. }
  275. }
  276. #if defined(KYLEP_CHANGE)
  277. void
  278. #endif
  279. print_reductions(p, defred)
  280. register action *p;
  281. register int defred;
  282. {
  283. register int k, anyreds;
  284. register action *q;
  285. anyreds = 0;
  286. for (q = p; q ; q = q->next)
  287. {
  288. if (q->action_code == REDUCE && q->suppressed < 2)
  289. {
  290. anyreds = 1;
  291. break;
  292. }
  293. }
  294. if (anyreds == 0)
  295. fprintf(verbose_file, "\t. error\n");
  296. else
  297. {
  298. for (; p; p = p->next)
  299. {
  300. if (p->action_code == REDUCE && p->number != defred)
  301. {
  302. k = p->number - 2;
  303. if (p->suppressed == 0)
  304. fprintf(verbose_file, "\t%s reduce %d\n",
  305. symbol_name[p->symbol], k);
  306. }
  307. }
  308. if (defred > 0)
  309. fprintf(verbose_file, "\t. reduce %d\n", defred - 2);
  310. }
  311. }
  312. #if defined(KYLEP_CHANGE)
  313. void
  314. #endif
  315. print_gotos(stateno)
  316. int stateno;
  317. {
  318. register int i, k;
  319. register int as;
  320. register short *to_state;
  321. register shifts *sp;
  322. putc('\n', verbose_file);
  323. sp = shift_table[stateno];
  324. to_state = sp->shift;
  325. for (i = 0; i < sp->nshifts; ++i)
  326. {
  327. k = to_state[i];
  328. as = accessing_symbol[k];
  329. if (ISVAR(as))
  330. fprintf(verbose_file, "\t%s goto %d\n", symbol_name[as], k);
  331. }
  332. }