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.

664 lines
13 KiB

  1. #include "defs.h"
  2. #if defined(KYLEP_CHANGE)
  3. /* BYACC prototypes, with type safety */
  4. void initialize_states();
  5. void save_reductions();
  6. void new_itemsets();
  7. void save_shifts();
  8. #endif // KYLEP_CHANGE
  9. extern short *itemset;
  10. extern short *itemsetend;
  11. extern unsigned *ruleset;
  12. int nstates;
  13. core *first_state;
  14. shifts *first_shift;
  15. reductions *first_reduction;
  16. int get_state();
  17. core *new_state();
  18. static core **state_set;
  19. static core *this_state;
  20. static core *last_state;
  21. static shifts *last_shift;
  22. static reductions *last_reduction;
  23. static int nshifts;
  24. static short *shift_symbol;
  25. static short *redset;
  26. static short *shiftset;
  27. static short **kernel_base;
  28. static short **kernel_end;
  29. static short *kernel_items;
  30. allocate_itemsets()
  31. {
  32. register short *itemp;
  33. register short *item_end;
  34. register int symbol;
  35. register int i;
  36. register int count;
  37. register int max;
  38. register short *symbol_count;
  39. count = 0;
  40. symbol_count = NEW2(nsyms, short);
  41. item_end = ritem + nitems;
  42. for (itemp = ritem; itemp < item_end; itemp++)
  43. {
  44. symbol = *itemp;
  45. if (symbol >= 0)
  46. {
  47. count++;
  48. symbol_count[symbol]++;
  49. }
  50. }
  51. kernel_base = NEW2(nsyms, short *);
  52. kernel_items = NEW2(count, short);
  53. count = 0;
  54. max = 0;
  55. for (i = 0; i < nsyms; i++)
  56. {
  57. kernel_base[i] = kernel_items + count;
  58. count += symbol_count[i];
  59. if (max < symbol_count[i])
  60. max = symbol_count[i];
  61. }
  62. shift_symbol = symbol_count;
  63. kernel_end = NEW2(nsyms, short *);
  64. }
  65. allocate_storage()
  66. {
  67. allocate_itemsets();
  68. shiftset = NEW2(nsyms, short);
  69. redset = NEW2(nrules + 1, short);
  70. state_set = NEW2(nitems, core *);
  71. }
  72. append_states()
  73. {
  74. register int i;
  75. register int j;
  76. register int symbol;
  77. #ifdef TRACE
  78. fprintf(stderr, "Entering append_states()\n");
  79. #endif
  80. for (i = 1; i < nshifts; i++)
  81. {
  82. symbol = shift_symbol[i];
  83. j = i;
  84. while (j > 0 && shift_symbol[j - 1] > symbol)
  85. {
  86. shift_symbol[j] = shift_symbol[j - 1];
  87. j--;
  88. }
  89. #if defined(KYLEP_CHANGE)
  90. shift_symbol[j] = (short) symbol;
  91. #else
  92. shift_symbol[j] = symbol;
  93. #endif // KYLEP_CHANGE
  94. }
  95. for (i = 0; i < nshifts; i++)
  96. {
  97. symbol = shift_symbol[i];
  98. #if defined(KYLEP_CHANGE)
  99. shiftset[i] = (short) get_state(symbol);
  100. #else
  101. shiftset[i] = get_state(symbol);
  102. #endif // KYLEP_CHANGE
  103. }
  104. }
  105. free_storage()
  106. {
  107. FREE(shift_symbol);
  108. FREE(redset);
  109. FREE(shiftset);
  110. FREE(kernel_base);
  111. FREE(kernel_end);
  112. FREE(kernel_items);
  113. FREE(state_set);
  114. }
  115. generate_states()
  116. {
  117. allocate_storage();
  118. itemset = NEW2(nitems, short);
  119. ruleset = NEW2(WORDSIZE(nrules), unsigned);
  120. set_first_derives();
  121. initialize_states();
  122. while (this_state)
  123. {
  124. closure(this_state->items, this_state->nitems);
  125. save_reductions();
  126. new_itemsets();
  127. append_states();
  128. if (nshifts > 0)
  129. save_shifts();
  130. this_state = this_state->next;
  131. }
  132. finalize_closure();
  133. free_storage();
  134. }
  135. int
  136. get_state(symbol)
  137. int symbol;
  138. {
  139. register int key;
  140. register short *isp1;
  141. register short *isp2;
  142. register short *iend;
  143. register core *sp;
  144. register int found;
  145. register int n;
  146. #ifdef TRACE
  147. fprintf(stderr, "Entering get_state(%d)\n", symbol);
  148. #endif
  149. isp1 = kernel_base[symbol];
  150. iend = kernel_end[symbol];
  151. #if defined(KYLEP_CHANGE)
  152. n = (int) (iend - isp1);
  153. #else
  154. n = iend - isp1;
  155. #endif
  156. key = *isp1;
  157. assert(0 <= key && key < nitems);
  158. sp = state_set[key];
  159. if (sp)
  160. {
  161. found = 0;
  162. while (!found)
  163. {
  164. if (sp->nitems == n)
  165. {
  166. found = 1;
  167. isp1 = kernel_base[symbol];
  168. isp2 = sp->items;
  169. while (found && isp1 < iend)
  170. {
  171. if (*isp1++ != *isp2++)
  172. found = 0;
  173. }
  174. }
  175. if (!found)
  176. {
  177. if (sp->link)
  178. {
  179. sp = sp->link;
  180. }
  181. else
  182. {
  183. sp = sp->link = new_state(symbol);
  184. found = 1;
  185. }
  186. }
  187. }
  188. }
  189. else
  190. {
  191. state_set[key] = sp = new_state(symbol);
  192. }
  193. return (sp->number);
  194. }
  195. #if defined(KYLEP_CHANGE)
  196. void
  197. #endif
  198. initialize_states()
  199. {
  200. register int i;
  201. register short *start_derives;
  202. register core *p;
  203. start_derives = derives[start_symbol];
  204. for (i = 0; start_derives[i] >= 0; ++i)
  205. continue;
  206. p = (core *) MALLOC(sizeof(core) + i*sizeof(short));
  207. if (p == 0) no_space();
  208. p->next = 0;
  209. p->link = 0;
  210. p->number = 0;
  211. p->accessing_symbol = 0;
  212. #if defined(KYLEP_CHANGE)
  213. p->nitems = (short) i;
  214. #else
  215. p->nitems = i;
  216. #endif // KYLEP_CHANGE
  217. for (i = 0; start_derives[i] >= 0; ++i)
  218. p->items[i] = rrhs[start_derives[i]];
  219. first_state = last_state = this_state = p;
  220. nstates = 1;
  221. }
  222. #if defined(KYLEP_CHANGE)
  223. void
  224. #endif
  225. new_itemsets()
  226. {
  227. register int i;
  228. register int shiftcount;
  229. register short *isp;
  230. register short *ksp;
  231. register int symbol;
  232. for (i = 0; i < nsyms; i++)
  233. kernel_end[i] = 0;
  234. shiftcount = 0;
  235. isp = itemset;
  236. while (isp < itemsetend)
  237. {
  238. i = *isp++;
  239. symbol = ritem[i];
  240. if (symbol > 0)
  241. {
  242. ksp = kernel_end[symbol];
  243. if (!ksp)
  244. {
  245. #if defined(KYLEP_CHANGE)
  246. shift_symbol[shiftcount++] = (short) symbol;
  247. #else
  248. shift_symbol[shiftcount++] = symbol;
  249. #endif // KYLEP_CHANGE
  250. ksp = kernel_base[symbol];
  251. }
  252. *ksp++ = i + 1;
  253. kernel_end[symbol] = ksp;
  254. }
  255. }
  256. nshifts = shiftcount;
  257. }
  258. core *
  259. new_state(symbol)
  260. int symbol;
  261. {
  262. register int n;
  263. register core *p;
  264. register short *isp1;
  265. register short *isp2;
  266. register short *iend;
  267. #ifdef TRACE
  268. fprintf(stderr, "Entering new_state(%d)\n", symbol);
  269. #endif
  270. if (nstates >= MAXSHORT)
  271. fatal("too many states");
  272. isp1 = kernel_base[symbol];
  273. iend = kernel_end[symbol];
  274. #if defined(KYLEP_CHANGE)
  275. n = (int) (iend - isp1);
  276. #else
  277. n = iend - isp1;
  278. #endif
  279. p = (core *) allocate((unsigned) (sizeof(core) + (n - 1) * sizeof(short)));
  280. #if defined(KYLEP_CHANGE)
  281. p->accessing_symbol = (short) symbol;
  282. p->number = (short) nstates;
  283. p->nitems = (short) n;
  284. #else
  285. p->accessing_symbol = symbol;
  286. p->number = nstates;
  287. p->nitems = n;
  288. #endif // KYLEP_CHANGE
  289. isp2 = p->items;
  290. while (isp1 < iend)
  291. *isp2++ = *isp1++;
  292. last_state->next = p;
  293. last_state = p;
  294. nstates++;
  295. return (p);
  296. }
  297. /* show_cores is used for debugging */
  298. show_cores()
  299. {
  300. core *p;
  301. int i, j, k, n;
  302. int itemno;
  303. k = 0;
  304. for (p = first_state; p; ++k, p = p->next)
  305. {
  306. if (k) printf("\n");
  307. printf("state %d, number = %d, accessing symbol = %s\n",
  308. k, p->number, symbol_name[p->accessing_symbol]);
  309. n = p->nitems;
  310. for (i = 0; i < n; ++i)
  311. {
  312. itemno = p->items[i];
  313. printf("%4d ", itemno);
  314. j = itemno;
  315. while (ritem[j] >= 0) ++j;
  316. printf("%s :", symbol_name[rlhs[-ritem[j]]]);
  317. j = rrhs[-ritem[j]];
  318. while (j < itemno)
  319. printf(" %s", symbol_name[ritem[j++]]);
  320. printf(" .");
  321. while (ritem[j] >= 0)
  322. printf(" %s", symbol_name[ritem[j++]]);
  323. printf("\n");
  324. fflush(stdout);
  325. }
  326. }
  327. }
  328. /* show_ritems is used for debugging */
  329. show_ritems()
  330. {
  331. int i;
  332. for (i = 0; i < nitems; ++i)
  333. printf("ritem[%d] = %d\n", i, ritem[i]);
  334. }
  335. /* show_rrhs is used for debugging */
  336. show_rrhs()
  337. {
  338. int i;
  339. for (i = 0; i < nrules; ++i)
  340. printf("rrhs[%d] = %d\n", i, rrhs[i]);
  341. }
  342. /* show_shifts is used for debugging */
  343. show_shifts()
  344. {
  345. shifts *p;
  346. int i, j, k;
  347. k = 0;
  348. for (p = first_shift; p; ++k, p = p->next)
  349. {
  350. if (k) printf("\n");
  351. printf("shift %d, number = %d, nshifts = %d\n", k, p->number,
  352. p->nshifts);
  353. j = p->nshifts;
  354. for (i = 0; i < j; ++i)
  355. printf("\t%d\n", p->shift[i]);
  356. }
  357. }
  358. #if defined(KYLEP_CHANGE)
  359. void
  360. #endif
  361. save_shifts()
  362. {
  363. register shifts *p;
  364. register short *sp1;
  365. register short *sp2;
  366. register short *send;
  367. p = (shifts *) allocate((unsigned) (sizeof(shifts) +
  368. (nshifts - 1) * sizeof(short)));
  369. p->number = this_state->number;
  370. #if defined(KYLEP_CHANGE)
  371. p->nshifts = (short) nshifts;
  372. #else
  373. p->nshifts = nshifts;
  374. #endif // KYLEP_CHANGE
  375. sp1 = shiftset;
  376. sp2 = p->shift;
  377. send = shiftset + nshifts;
  378. while (sp1 < send)
  379. *sp2++ = *sp1++;
  380. if (last_shift)
  381. {
  382. last_shift->next = p;
  383. last_shift = p;
  384. }
  385. else
  386. {
  387. first_shift = p;
  388. last_shift = p;
  389. }
  390. }
  391. #if defined(KYLEP_CHANGE)
  392. void
  393. #endif
  394. save_reductions()
  395. {
  396. register short *isp;
  397. register short *rp1;
  398. register short *rp2;
  399. register int item;
  400. register int count;
  401. register reductions *p;
  402. register short *rend;
  403. count = 0;
  404. for (isp = itemset; isp < itemsetend; isp++)
  405. {
  406. item = ritem[*isp];
  407. if (item < 0)
  408. {
  409. redset[count++] = -item;
  410. }
  411. }
  412. if (count)
  413. {
  414. p = (reductions *) allocate((unsigned) (sizeof(reductions) +
  415. (count - 1) * sizeof(short)));
  416. p->number = this_state->number;
  417. #if defined(KYLEP_CHANGE)
  418. p->nreds = (short) count;
  419. #else
  420. p->nreds = count;
  421. #endif // KYLEP_CHANGE
  422. rp1 = redset;
  423. rp2 = p->rules;
  424. rend = rp1 + count;
  425. while (rp1 < rend)
  426. *rp2++ = *rp1++;
  427. if (last_reduction)
  428. {
  429. last_reduction->next = p;
  430. last_reduction = p;
  431. }
  432. else
  433. {
  434. first_reduction = p;
  435. last_reduction = p;
  436. }
  437. }
  438. }
  439. set_derives()
  440. {
  441. register int i, k;
  442. register int lhs;
  443. register short *rules;
  444. derives = NEW2(nsyms, short *);
  445. rules = NEW2(nvars + nrules, short);
  446. k = 0;
  447. for (lhs = start_symbol; lhs < nsyms; lhs++)
  448. {
  449. derives[lhs] = rules + k;
  450. for (i = 0; i < nrules; i++)
  451. {
  452. if (rlhs[i] == lhs)
  453. {
  454. #if defined(KYLEP_CHANGE)
  455. rules[k] = (short) i;
  456. #else
  457. rules[k] = i;
  458. #endif // KYLEP_CHANGE
  459. k++;
  460. }
  461. }
  462. rules[k] = -1;
  463. k++;
  464. }
  465. #ifdef DEBUG
  466. print_derives();
  467. #endif
  468. }
  469. free_derives()
  470. {
  471. FREE(derives[start_symbol]);
  472. FREE(derives);
  473. }
  474. #ifdef DEBUG
  475. print_derives()
  476. {
  477. register int i;
  478. register short *sp;
  479. printf("\nDERIVES\n\n");
  480. for (i = start_symbol; i < nsyms; i++)
  481. {
  482. printf("%s derives ", symbol_name[i]);
  483. for (sp = derives[i]; *sp >= 0; sp++)
  484. {
  485. printf(" %d", *sp);
  486. }
  487. putchar('\n');
  488. }
  489. putchar('\n');
  490. }
  491. #endif
  492. set_nullable()
  493. {
  494. register int i, j;
  495. register int empty;
  496. int done;
  497. nullable = MALLOC(nsyms);
  498. if (nullable == 0) no_space();
  499. for (i = 0; i < nsyms; ++i)
  500. nullable[i] = 0;
  501. done = 0;
  502. while (!done)
  503. {
  504. done = 1;
  505. for (i = 1; i < nitems; i++)
  506. {
  507. empty = 1;
  508. while ((j = ritem[i]) >= 0)
  509. {
  510. if (!nullable[j])
  511. empty = 0;
  512. ++i;
  513. }
  514. if (empty)
  515. {
  516. j = rlhs[-j];
  517. if (!nullable[j])
  518. {
  519. nullable[j] = 1;
  520. done = 0;
  521. }
  522. }
  523. }
  524. }
  525. #ifdef DEBUG
  526. for (i = 0; i < nsyms; i++)
  527. {
  528. if (nullable[i])
  529. printf("%s is nullable\n", symbol_name[i]);
  530. else
  531. printf("%s is not nullable\n", symbol_name[i]);
  532. }
  533. #endif
  534. }
  535. free_nullable()
  536. {
  537. FREE(nullable);
  538. }
  539. #if defined(KYLEP_CHANGE)
  540. void
  541. #endif
  542. lr0()
  543. {
  544. set_derives();
  545. set_nullable();
  546. generate_states();
  547. }