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.

756 lines
14 KiB

  1. #include "defs.h"
  2. #if defined(KYLEP_CHANGE)
  3. /* BYACC prototypes, with type safety */
  4. void set_state_table();
  5. void set_accessing_symbol();
  6. void set_shift_table();
  7. void set_reduction_table();
  8. void set_maxrhs();
  9. void initialize_LA();
  10. void set_goto_map();
  11. void initialize_F();
  12. void build_relations();
  13. void compute_FOLLOWS();
  14. void compute_lookaheads();
  15. void digraph( short **relation );
  16. void add_lookback_edge( int stateno, int ruleno, int gotono );
  17. void traverse( int i );
  18. #endif // KYLEP_CHANGE
  19. typedef
  20. struct shorts
  21. {
  22. struct shorts *next;
  23. short value;
  24. }
  25. shorts;
  26. int tokensetsize;
  27. short *lookaheads;
  28. short *LAruleno;
  29. unsigned *LA;
  30. short *accessing_symbol;
  31. core **state_table;
  32. shifts **shift_table;
  33. reductions **reduction_table;
  34. short *goto_map;
  35. short *from_state;
  36. short *to_state;
  37. short **transpose();
  38. static int infinity;
  39. static int maxrhs;
  40. #if defined(KYLEP_CHANGE)
  41. static short ngotos;
  42. #else
  43. static int ngotos;
  44. #endif // KYLEP_CHANGE
  45. static unsigned *F;
  46. static short **includes;
  47. static shorts **lookback;
  48. static short **R;
  49. static short *INDEX;
  50. static short *VERTICES;
  51. static int top;
  52. #if defined(KYLEP_CHANGE)
  53. void
  54. #endif
  55. lalr()
  56. {
  57. tokensetsize = WORDSIZE(ntokens);
  58. set_state_table();
  59. set_accessing_symbol();
  60. set_shift_table();
  61. set_reduction_table();
  62. set_maxrhs();
  63. initialize_LA();
  64. set_goto_map();
  65. initialize_F();
  66. build_relations();
  67. compute_FOLLOWS();
  68. compute_lookaheads();
  69. }
  70. #if defined(KYLEP_CHANGE)
  71. void
  72. #endif
  73. set_state_table()
  74. {
  75. register core *sp;
  76. state_table = NEW2(nstates, core *);
  77. for (sp = first_state; sp; sp = sp->next)
  78. state_table[sp->number] = sp;
  79. }
  80. #if defined(KYLEP_CHANGE)
  81. void
  82. #endif
  83. set_accessing_symbol()
  84. {
  85. register core *sp;
  86. accessing_symbol = NEW2(nstates, short);
  87. for (sp = first_state; sp; sp = sp->next)
  88. accessing_symbol[sp->number] = sp->accessing_symbol;
  89. }
  90. #if defined(KYLEP_CHANGE)
  91. void
  92. #endif
  93. set_shift_table()
  94. {
  95. register shifts *sp;
  96. shift_table = NEW2(nstates, shifts *);
  97. for (sp = first_shift; sp; sp = sp->next)
  98. shift_table[sp->number] = sp;
  99. }
  100. #if defined(KYLEP_CHANGE)
  101. void
  102. #endif
  103. set_reduction_table()
  104. {
  105. register reductions *rp;
  106. reduction_table = NEW2(nstates, reductions *);
  107. for (rp = first_reduction; rp; rp = rp->next)
  108. reduction_table[rp->number] = rp;
  109. }
  110. #if defined(KYLEP_CHANGE)
  111. void
  112. #endif
  113. set_maxrhs()
  114. {
  115. register short *itemp;
  116. register short *item_end;
  117. register int length;
  118. register int max;
  119. length = 0;
  120. max = 0;
  121. item_end = ritem + nitems;
  122. for (itemp = ritem; itemp < item_end; itemp++)
  123. {
  124. if (*itemp >= 0)
  125. {
  126. length++;
  127. }
  128. else
  129. {
  130. if (length > max) max = length;
  131. length = 0;
  132. }
  133. }
  134. maxrhs = max;
  135. }
  136. #if defined(KYLEP_CHANGE)
  137. void
  138. #endif
  139. initialize_LA()
  140. {
  141. #if defined(KYLEP_CHANGE)
  142. register short i, j, k;
  143. #else
  144. register int i, j, k;
  145. #endif // KYLEP_CHANGE
  146. register reductions *rp;
  147. lookaheads = NEW2(nstates + 1, short);
  148. k = 0;
  149. for (i = 0; i < nstates; i++)
  150. {
  151. lookaheads[i] = k;
  152. rp = reduction_table[i];
  153. if (rp)
  154. k += rp->nreds;
  155. }
  156. lookaheads[nstates] = k;
  157. LA = NEW2(k * tokensetsize, unsigned);
  158. LAruleno = NEW2(k, short);
  159. lookback = NEW2(k, shorts *);
  160. k = 0;
  161. for (i = 0; i < nstates; i++)
  162. {
  163. rp = reduction_table[i];
  164. if (rp)
  165. {
  166. for (j = 0; j < rp->nreds; j++)
  167. {
  168. LAruleno[k] = rp->rules[j];
  169. k++;
  170. }
  171. }
  172. }
  173. }
  174. #if defined(KYLEP_CHANGE)
  175. void
  176. #endif
  177. set_goto_map()
  178. {
  179. register shifts *sp;
  180. register int i;
  181. register int symbol;
  182. #if defined(KYLEP_CHANGE)
  183. register short k;
  184. #else
  185. register int k;
  186. #endif // KYLEP_CHANGE
  187. register short *temp_map;
  188. register int state2;
  189. register int state1;
  190. goto_map = NEW2(nvars + 1, short) - ntokens;
  191. temp_map = NEW2(nvars + 1, short) - ntokens;
  192. ngotos = 0;
  193. for (sp = first_shift; sp; sp = sp->next)
  194. {
  195. for (i = sp->nshifts - 1; i >= 0; i--)
  196. {
  197. symbol = accessing_symbol[sp->shift[i]];
  198. if (ISTOKEN(symbol)) break;
  199. if (ngotos == MAXSHORT)
  200. fatal("too many gotos");
  201. ngotos++;
  202. goto_map[symbol]++;
  203. }
  204. }
  205. k = 0;
  206. for (i = ntokens; i < nsyms; i++)
  207. {
  208. temp_map[i] = k;
  209. k += goto_map[i];
  210. }
  211. for (i = ntokens; i < nsyms; i++)
  212. goto_map[i] = temp_map[i];
  213. goto_map[nsyms] = ngotos;
  214. temp_map[nsyms] = ngotos;
  215. from_state = NEW2(ngotos, short);
  216. to_state = NEW2(ngotos, short);
  217. for (sp = first_shift; sp; sp = sp->next)
  218. {
  219. state1 = sp->number;
  220. for (i = sp->nshifts - 1; i >= 0; i--)
  221. {
  222. state2 = sp->shift[i];
  223. symbol = accessing_symbol[state2];
  224. if (ISTOKEN(symbol)) break;
  225. k = temp_map[symbol]++;
  226. #if defined(KYLEP_CHANGE)
  227. from_state[k] = (short) state1;
  228. to_state[k] = (short) state2;
  229. #else
  230. from_state[k] = state1;
  231. to_state[k] = state2;
  232. #endif // KYLEP_CHANGE
  233. }
  234. }
  235. FREE(temp_map + ntokens);
  236. }
  237. /* Map_goto maps a state/symbol pair into its numeric representation. */
  238. int
  239. map_goto(state, symbol)
  240. int state;
  241. int symbol;
  242. {
  243. register int high;
  244. register int low;
  245. register int middle;
  246. register int s;
  247. low = goto_map[symbol];
  248. high = goto_map[symbol + 1];
  249. for (;;)
  250. {
  251. assert(low <= high);
  252. middle = (low + high) >> 1;
  253. s = from_state[middle];
  254. if (s == state)
  255. return (middle);
  256. else if (s < state)
  257. low = middle + 1;
  258. else
  259. high = middle - 1;
  260. }
  261. }
  262. #if defined(KYLEP_CHANGE)
  263. void
  264. #endif
  265. initialize_F()
  266. {
  267. register int i;
  268. register int j;
  269. register int k;
  270. register shifts *sp;
  271. register short *edge;
  272. register unsigned *rowp;
  273. register short *rp;
  274. register short **reads;
  275. register int nedges;
  276. register int stateno;
  277. register int symbol;
  278. register int nwords;
  279. nwords = ngotos * tokensetsize;
  280. F = NEW2(nwords, unsigned);
  281. reads = NEW2(ngotos, short *);
  282. edge = NEW2(ngotos + 1, short);
  283. nedges = 0;
  284. rowp = F;
  285. for (i = 0; i < ngotos; i++)
  286. {
  287. stateno = to_state[i];
  288. sp = shift_table[stateno];
  289. if (sp)
  290. {
  291. k = sp->nshifts;
  292. for (j = 0; j < k; j++)
  293. {
  294. symbol = accessing_symbol[sp->shift[j]];
  295. if (ISVAR(symbol))
  296. break;
  297. SETBIT(rowp, symbol);
  298. }
  299. for (; j < k; j++)
  300. {
  301. #if defined(KYLEP_CHANGE)
  302. symbol = (short) accessing_symbol[sp->shift[j]];
  303. #else
  304. symbol = accessing_symbol[sp->shift[j]];
  305. #endif // KYLEP_CHANGE
  306. if (nullable[symbol])
  307. #if defined(KYLEP_CHANGE)
  308. edge[nedges++] = (short) map_goto(stateno, symbol);
  309. #else
  310. edge[nedges++] = map_goto(stateno, symbol);
  311. #endif // KYLEP_CHANGE
  312. }
  313. if (nedges)
  314. {
  315. reads[i] = rp = NEW2(nedges + 1, short);
  316. for (j = 0; j < nedges; j++)
  317. rp[j] = edge[j];
  318. rp[nedges] = -1;
  319. nedges = 0;
  320. }
  321. }
  322. rowp += tokensetsize;
  323. }
  324. SETBIT(F, 0);
  325. digraph(reads);
  326. for (i = 0; i < ngotos; i++)
  327. {
  328. if (reads[i])
  329. FREE(reads[i]);
  330. }
  331. FREE(reads);
  332. FREE(edge);
  333. }
  334. #if defined(KYLEP_CHANGE)
  335. void
  336. #endif
  337. build_relations()
  338. {
  339. register int i;
  340. register int j;
  341. register int k;
  342. register short *rulep;
  343. register short *rp;
  344. register shifts *sp;
  345. register int length;
  346. register int nedges;
  347. register int done;
  348. register int state1;
  349. register int stateno;
  350. register int symbol1;
  351. register int symbol2;
  352. register short *shortp;
  353. register short *edge;
  354. register short *states;
  355. register short **new_includes;
  356. includes = NEW2(ngotos, short *);
  357. edge = NEW2(ngotos + 1, short);
  358. states = NEW2(maxrhs + 1, short);
  359. for (i = 0; i < ngotos; i++)
  360. {
  361. nedges = 0;
  362. state1 = from_state[i];
  363. symbol1 = accessing_symbol[to_state[i]];
  364. for (rulep = derives[symbol1]; *rulep >= 0; rulep++)
  365. {
  366. length = 1;
  367. #if defined(KYLEP_CHANGE)
  368. states[0] = (short) state1;
  369. #else
  370. states[0] = state1;
  371. #endif // KYLEP_CHANGE
  372. stateno = state1;
  373. for (rp = ritem + rrhs[*rulep]; *rp >= 0; rp++)
  374. {
  375. symbol2 = *rp;
  376. sp = shift_table[stateno];
  377. k = sp->nshifts;
  378. for (j = 0; j < k; j++)
  379. {
  380. stateno = sp->shift[j];
  381. if (accessing_symbol[stateno] == symbol2) break;
  382. }
  383. #if defined(KYLEP_CHANGE)
  384. states[length++] = (short) stateno;
  385. #else
  386. states[length++] = stateno;
  387. #endif // KYLEP_CHANGE
  388. }
  389. add_lookback_edge(stateno, *rulep, i);
  390. length--;
  391. done = 0;
  392. while (!done)
  393. {
  394. done = 1;
  395. rp--;
  396. if (ISVAR(*rp))
  397. {
  398. stateno = states[--length];
  399. #if defined(KYLEP_CHANGE)
  400. edge[nedges++] = (short) map_goto(stateno, *rp);
  401. #else
  402. edge[nedges++] = map_goto(stateno, *rp);
  403. #endif // KYLEP_CHANGE
  404. if (nullable[*rp] && length > 0) done = 0;
  405. }
  406. }
  407. }
  408. if (nedges)
  409. {
  410. includes[i] = shortp = NEW2(nedges + 1, short);
  411. for (j = 0; j < nedges; j++)
  412. shortp[j] = edge[j];
  413. shortp[nedges] = -1;
  414. }
  415. }
  416. new_includes = transpose(includes, ngotos);
  417. for (i = 0; i < ngotos; i++)
  418. if (includes[i])
  419. FREE(includes[i]);
  420. FREE(includes);
  421. includes = new_includes;
  422. FREE(edge);
  423. FREE(states);
  424. }
  425. #if defined(KYLEP_CHANGE)
  426. void
  427. #endif
  428. add_lookback_edge(stateno, ruleno, gotono)
  429. int stateno, ruleno, gotono;
  430. {
  431. register int i, k;
  432. register int found;
  433. register shorts *sp;
  434. i = lookaheads[stateno];
  435. k = lookaheads[stateno + 1];
  436. found = 0;
  437. while (!found && i < k)
  438. {
  439. if (LAruleno[i] == ruleno)
  440. found = 1;
  441. else
  442. ++i;
  443. }
  444. assert(found);
  445. sp = NEW(shorts);
  446. sp->next = lookback[i];
  447. #if defined(KYLEP_CHANGE)
  448. sp->value = (short) gotono;
  449. #else
  450. sp->value = gotono;
  451. #endif // KYLEP_CHANGE
  452. lookback[i] = sp;
  453. }
  454. short **
  455. transpose(R, n)
  456. short **R;
  457. int n;
  458. {
  459. register short **new_R;
  460. register short **temp_R;
  461. register short *nedges;
  462. register short *sp;
  463. register int i;
  464. register int k;
  465. nedges = NEW2(n, short);
  466. for (i = 0; i < n; i++)
  467. {
  468. sp = R[i];
  469. if (sp)
  470. {
  471. while (*sp >= 0)
  472. nedges[*sp++]++;
  473. }
  474. }
  475. new_R = NEW2(n, short *);
  476. temp_R = NEW2(n, short *);
  477. for (i = 0; i < n; i++)
  478. {
  479. k = nedges[i];
  480. if (k > 0)
  481. {
  482. sp = NEW2(k + 1, short);
  483. new_R[i] = sp;
  484. temp_R[i] = sp;
  485. sp[k] = -1;
  486. }
  487. }
  488. FREE(nedges);
  489. for (i = 0; i < n; i++)
  490. {
  491. sp = R[i];
  492. if (sp)
  493. {
  494. while (*sp >= 0)
  495. #if defined(KYLEP_CHANGE)
  496. *temp_R[*sp++]++ = (short) i;
  497. #else
  498. *temp_R[*sp++]++ = i;
  499. #endif // KYLEP_CHANGE
  500. }
  501. }
  502. FREE(temp_R);
  503. return (new_R);
  504. }
  505. #if defined(KYLEP_CHANGE)
  506. void
  507. #endif
  508. compute_FOLLOWS()
  509. {
  510. digraph(includes);
  511. }
  512. #if defined(KYLEP_CHANGE)
  513. void
  514. #endif
  515. compute_lookaheads()
  516. {
  517. register int i, n;
  518. register unsigned *fp1, *fp2, *fp3;
  519. register shorts *sp, *next;
  520. register unsigned *rowp;
  521. rowp = LA;
  522. n = lookaheads[nstates];
  523. for (i = 0; i < n; i++)
  524. {
  525. fp3 = rowp + tokensetsize;
  526. for (sp = lookback[i]; sp; sp = sp->next)
  527. {
  528. fp1 = rowp;
  529. fp2 = F + tokensetsize * sp->value;
  530. while (fp1 < fp3)
  531. *fp1++ |= *fp2++;
  532. }
  533. rowp = fp3;
  534. }
  535. for (i = 0; i < n; i++)
  536. for (sp = lookback[i]; sp; sp = next)
  537. {
  538. next = sp->next;
  539. FREE(sp);
  540. }
  541. FREE(lookback);
  542. FREE(F);
  543. }
  544. #if defined(KYLEP_CHANGE)
  545. void
  546. #endif
  547. digraph(relation)
  548. short **relation;
  549. {
  550. register int i;
  551. infinity = ngotos + 2;
  552. INDEX = NEW2(ngotos + 1, short);
  553. VERTICES = NEW2(ngotos + 1, short);
  554. top = 0;
  555. R = relation;
  556. for (i = 0; i < ngotos; i++)
  557. INDEX[i] = 0;
  558. for (i = 0; i < ngotos; i++)
  559. {
  560. if (INDEX[i] == 0 && R[i])
  561. traverse(i);
  562. }
  563. FREE(INDEX);
  564. FREE(VERTICES);
  565. }
  566. #if defined(KYLEP_CHANGE)
  567. void
  568. #endif
  569. traverse(i)
  570. register int i;
  571. {
  572. register unsigned *fp1;
  573. register unsigned *fp2;
  574. register unsigned *fp3;
  575. register int j;
  576. register short *rp;
  577. int height;
  578. unsigned *base;
  579. #if defined(KYLEP_CHANGE)
  580. VERTICES[++top] = (short) i;
  581. #else
  582. VERTICES[++top] = i;
  583. #endif // KYLEP_CHANGE
  584. INDEX[i] = height = top;
  585. base = F + i * tokensetsize;
  586. fp3 = base + tokensetsize;
  587. rp = R[i];
  588. if (rp)
  589. {
  590. while ((j = *rp++) >= 0)
  591. {
  592. if (INDEX[j] == 0)
  593. traverse(j);
  594. if (INDEX[i] > INDEX[j])
  595. INDEX[i] = INDEX[j];
  596. fp1 = base;
  597. fp2 = F + j * tokensetsize;
  598. while (fp1 < fp3)
  599. *fp1++ |= *fp2++;
  600. }
  601. }
  602. if (INDEX[i] == height)
  603. {
  604. for (;;)
  605. {
  606. j = VERTICES[top--];
  607. #if defined(KYLEP_CHANGE)
  608. INDEX[j] = (short) infinity;
  609. #else
  610. INDEX[j] = infinity;
  611. #endif // KYLEP_CHANGE
  612. if (i == j)
  613. break;
  614. fp1 = base;
  615. fp2 = F + j * tokensetsize;
  616. while (fp1 < fp3)
  617. *fp2++ = *fp1++;
  618. }
  619. }
  620. }