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.

408 lines
8.6 KiB

  1. #include "defs.h"
  2. #if defined(KYLEP_CHANGE)
  3. /* BYACC prototypes, with type safety */
  4. void find_final_state();
  5. void remove_conflicts();
  6. void unused_rules();
  7. void total_conflicts();
  8. void defreds();
  9. #endif // KYLEP_CHANGE
  10. action **parser;
  11. int SRtotal;
  12. int RRtotal;
  13. short *SRconflicts;
  14. short *RRconflicts;
  15. short *defred;
  16. short *rules_used;
  17. short nunused;
  18. short final_state;
  19. static int SRcount;
  20. static int RRcount;
  21. extern action *parse_actions();
  22. extern action *get_shifts();
  23. extern action *add_reductions();
  24. extern action *add_reduce();
  25. #if defined(KYLEP_CHANGE)
  26. void
  27. #endif
  28. make_parser()
  29. {
  30. register int i;
  31. parser = NEW2(nstates, action *);
  32. for (i = 0; i < nstates; i++)
  33. parser[i] = parse_actions(i);
  34. find_final_state();
  35. remove_conflicts();
  36. unused_rules();
  37. if (SRtotal + RRtotal > 0) total_conflicts();
  38. defreds();
  39. }
  40. action *
  41. parse_actions(stateno)
  42. register int stateno;
  43. {
  44. register action *actions;
  45. actions = get_shifts(stateno);
  46. actions = add_reductions(stateno, actions);
  47. return (actions);
  48. }
  49. action *
  50. get_shifts(stateno)
  51. int stateno;
  52. {
  53. register action *actions, *temp;
  54. register shifts *sp;
  55. register short *to_state;
  56. register int i, k;
  57. register int symbol;
  58. actions = 0;
  59. sp = shift_table[stateno];
  60. if (sp)
  61. {
  62. to_state = sp->shift;
  63. for (i = sp->nshifts - 1; i >= 0; i--)
  64. {
  65. k = to_state[i];
  66. symbol = accessing_symbol[k];
  67. if (ISTOKEN(symbol))
  68. {
  69. temp = NEW(action);
  70. temp->next = actions;
  71. #if defined(KYLEP_CHANGE)
  72. temp->symbol = (short) symbol;
  73. temp->number = (short) k;
  74. #else
  75. temp->symbol = symbol;
  76. temp->number = k;
  77. #endif // KYLEP_CHANGE
  78. temp->prec = symbol_prec[symbol];
  79. temp->action_code = SHIFT;
  80. temp->assoc = symbol_assoc[symbol];
  81. actions = temp;
  82. }
  83. }
  84. }
  85. return (actions);
  86. }
  87. action *
  88. add_reductions(stateno, actions)
  89. int stateno;
  90. register action *actions;
  91. {
  92. register int i, j, m, n;
  93. register int ruleno, tokensetsize;
  94. register unsigned *rowp;
  95. tokensetsize = WORDSIZE(ntokens);
  96. m = lookaheads[stateno];
  97. n = lookaheads[stateno + 1];
  98. for (i = m; i < n; i++)
  99. {
  100. ruleno = LAruleno[i];
  101. rowp = LA + i * tokensetsize;
  102. for (j = ntokens - 1; j >= 0; j--)
  103. {
  104. if (BIT(rowp, j))
  105. actions = add_reduce(actions, ruleno, j);
  106. }
  107. }
  108. return (actions);
  109. }
  110. action *
  111. add_reduce(actions, ruleno, symbol)
  112. register action *actions;
  113. register int ruleno, symbol;
  114. {
  115. register action *temp, *prev, *next;
  116. prev = 0;
  117. for (next = actions; next && next->symbol < symbol; next = next->next)
  118. prev = next;
  119. while (next && next->symbol == symbol && next->action_code == SHIFT)
  120. {
  121. prev = next;
  122. next = next->next;
  123. }
  124. while (next && next->symbol == symbol &&
  125. next->action_code == REDUCE && next->number < ruleno)
  126. {
  127. prev = next;
  128. next = next->next;
  129. }
  130. temp = NEW(action);
  131. temp->next = next;
  132. #if defined(KYLEP_CHANGE)
  133. temp->symbol = (short) symbol;
  134. temp->number = (short) ruleno;
  135. #else
  136. temp->symbol = symbol;
  137. temp->number = ruleno;
  138. #endif // KYLEP_CHANGE
  139. temp->prec = rprec[ruleno];
  140. temp->action_code = REDUCE;
  141. temp->assoc = rassoc[ruleno];
  142. if (prev)
  143. prev->next = temp;
  144. else
  145. actions = temp;
  146. return (actions);
  147. }
  148. #if defined(KYLEP_CHANGE)
  149. void
  150. #endif
  151. find_final_state()
  152. {
  153. register int goal, i;
  154. register short *to_state;
  155. register shifts *p;
  156. p = shift_table[0];
  157. to_state = p->shift;
  158. goal = ritem[1];
  159. for (i = p->nshifts - 1; i >= 0; --i)
  160. {
  161. final_state = to_state[i];
  162. if (accessing_symbol[final_state] == goal) break;
  163. }
  164. }
  165. #if defined(KYLEP_CHANGE)
  166. void
  167. #endif
  168. unused_rules()
  169. {
  170. register int i;
  171. register action *p;
  172. rules_used = (short *) MALLOC(nrules*sizeof(short));
  173. if (rules_used == 0) no_space();
  174. for (i = 0; i < nrules; ++i)
  175. rules_used[i] = 0;
  176. for (i = 0; i < nstates; ++i)
  177. {
  178. for (p = parser[i]; p; p = p->next)
  179. {
  180. if (p->action_code == REDUCE && p->suppressed == 0)
  181. rules_used[p->number] = 1;
  182. }
  183. }
  184. nunused = 0;
  185. for (i = 3; i < nrules; ++i)
  186. if (!rules_used[i]) ++nunused;
  187. if (nunused)
  188. if (nunused == 1)
  189. fprintf(stderr, "%s: 1 rule never reduced\n", myname);
  190. else
  191. fprintf(stderr, "%s: %d rules never reduced\n", myname, nunused);
  192. }
  193. #if defined(KYLEP_CHANGE)
  194. void
  195. #endif
  196. remove_conflicts()
  197. {
  198. register int i;
  199. register int symbol;
  200. register action *p, *pref;
  201. SRtotal = 0;
  202. RRtotal = 0;
  203. SRconflicts = NEW2(nstates, short);
  204. RRconflicts = NEW2(nstates, short);
  205. for (i = 0; i < nstates; i++)
  206. {
  207. SRcount = 0;
  208. RRcount = 0;
  209. symbol = -1;
  210. for (p = parser[i]; p; p = p->next)
  211. {
  212. if (p->symbol != symbol)
  213. {
  214. pref = p;
  215. symbol = p->symbol;
  216. }
  217. else if (i == final_state && symbol == 0)
  218. {
  219. SRcount++;
  220. p->suppressed = 1;
  221. }
  222. else if (pref->action_code == SHIFT)
  223. {
  224. if (pref->prec > 0 && p->prec > 0)
  225. {
  226. if (pref->prec < p->prec)
  227. {
  228. pref->suppressed = 2;
  229. pref = p;
  230. }
  231. else if (pref->prec > p->prec)
  232. {
  233. p->suppressed = 2;
  234. }
  235. else if (pref->assoc == LEFT)
  236. {
  237. pref->suppressed = 2;
  238. pref = p;
  239. }
  240. else if (pref->assoc == RIGHT)
  241. {
  242. p->suppressed = 2;
  243. }
  244. else
  245. {
  246. pref->suppressed = 2;
  247. p->suppressed = 2;
  248. }
  249. }
  250. else
  251. {
  252. SRcount++;
  253. p->suppressed = 1;
  254. }
  255. }
  256. else
  257. {
  258. RRcount++;
  259. p->suppressed = 1;
  260. }
  261. }
  262. SRtotal += SRcount;
  263. RRtotal += RRcount;
  264. #if defined(KYLEP_CHANGE)
  265. SRconflicts[i] = (short) SRcount;
  266. RRconflicts[i] = (short) RRcount;
  267. #else
  268. SRconflicts[i] = SRcount;
  269. RRconflicts[i] = RRcount;
  270. #endif // KYLEP_CHANGE
  271. }
  272. }
  273. #if defined(KYLEP_CHANGE)
  274. void
  275. #endif
  276. total_conflicts()
  277. {
  278. fprintf(stderr, "%s: ", myname);
  279. if (SRtotal == 1)
  280. fprintf(stderr, "1 shift/reduce conflict");
  281. else if (SRtotal > 1)
  282. fprintf(stderr, "%d shift/reduce conflicts", SRtotal);
  283. if (SRtotal && RRtotal)
  284. fprintf(stderr, ", ");
  285. if (RRtotal == 1)
  286. fprintf(stderr, "1 reduce/reduce conflict");
  287. else if (RRtotal > 1)
  288. fprintf(stderr, "%d reduce/reduce conflicts", RRtotal);
  289. fprintf(stderr, ".\n");
  290. }
  291. int
  292. sole_reduction(stateno)
  293. int stateno;
  294. {
  295. register int count, ruleno;
  296. register action *p;
  297. count = 0;
  298. ruleno = 0;
  299. for (p = parser[stateno]; p; p = p->next)
  300. {
  301. if (p->action_code == SHIFT && p->suppressed == 0)
  302. return (0);
  303. else if (p->action_code == REDUCE && p->suppressed == 0)
  304. {
  305. if (ruleno > 0 && p->number != ruleno)
  306. return (0);
  307. if (p->symbol != 1)
  308. ++count;
  309. ruleno = p->number;
  310. }
  311. }
  312. if (count == 0)
  313. return (0);
  314. return (ruleno);
  315. }
  316. #if defined(KYLEP_CHANGE)
  317. void
  318. #endif
  319. defreds()
  320. {
  321. register int i;
  322. defred = NEW2(nstates, short);
  323. for (i = 0; i < nstates; i++)
  324. #if defined(KYLEP_CHANGE)
  325. defred[i] = (short) sole_reduction(i);
  326. #else
  327. defred[i] = sole_reduction(i);
  328. #endif // KYLEP_CHANGE
  329. }
  330. free_action_row(p)
  331. register action *p;
  332. {
  333. register action *q;
  334. while (p)
  335. {
  336. q = p->next;
  337. FREE(p);
  338. p = q;
  339. }
  340. }
  341. #if defined(KYLEP_CHANGE)
  342. void
  343. #endif
  344. free_parser()
  345. {
  346. register int i;
  347. for (i = 0; i < nstates; i++)
  348. free_action_row(parser[i]);
  349. FREE(parser);
  350. }