Source code of Windows XP (NT5)
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.

269 lines
5.3 KiB

  1. #include "defs.h"
  2. short *itemset;
  3. short *itemsetend;
  4. unsigned *ruleset;
  5. static unsigned *first_derives;
  6. static unsigned *EFF;
  7. set_EFF()
  8. {
  9. register unsigned *row;
  10. register int symbol;
  11. register short *sp;
  12. register int rowsize;
  13. register int i;
  14. register int rule;
  15. rowsize = WORDSIZE(nvars);
  16. EFF = NEW2(nvars * rowsize, unsigned);
  17. row = EFF;
  18. for (i = start_symbol; i < nsyms; i++)
  19. {
  20. sp = derives[i];
  21. for (rule = *sp; rule > 0; rule = *++sp)
  22. {
  23. symbol = ritem[rrhs[rule]];
  24. if (ISVAR(symbol))
  25. {
  26. symbol -= start_symbol;
  27. SETBIT(row, symbol);
  28. }
  29. }
  30. row += rowsize;
  31. }
  32. reflexive_transitive_closure(EFF, nvars);
  33. #ifdef DEBUG
  34. print_EFF();
  35. #endif
  36. }
  37. #if defined(KYLEP_CHANGE)
  38. void
  39. #endif
  40. set_first_derives()
  41. {
  42. register unsigned *rrow;
  43. register unsigned *vrow;
  44. register int j;
  45. register unsigned k;
  46. register unsigned cword;
  47. register short *rp;
  48. int rule;
  49. int i;
  50. int rulesetsize;
  51. int varsetsize;
  52. rulesetsize = WORDSIZE(nrules);
  53. varsetsize = WORDSIZE(nvars);
  54. first_derives = NEW2(nvars * rulesetsize, unsigned) - ntokens * rulesetsize;
  55. set_EFF();
  56. rrow = first_derives + ntokens * rulesetsize;
  57. for (i = start_symbol; i < nsyms; i++)
  58. {
  59. vrow = EFF + ((i - ntokens) * varsetsize);
  60. k = BITS_PER_WORD;
  61. for (j = start_symbol; j < nsyms; k++, j++)
  62. {
  63. if (k >= BITS_PER_WORD)
  64. {
  65. cword = *vrow++;
  66. k = 0;
  67. }
  68. if (cword & (1 << k))
  69. {
  70. rp = derives[j];
  71. while ((rule = *rp++) >= 0)
  72. {
  73. SETBIT(rrow, rule);
  74. }
  75. }
  76. }
  77. vrow += varsetsize;
  78. rrow += rulesetsize;
  79. }
  80. #ifdef DEBUG
  81. print_first_derives();
  82. #endif
  83. FREE(EFF);
  84. }
  85. #if defined(KYLEP_CHANGE)
  86. void
  87. #endif
  88. closure(nucleus, n)
  89. short *nucleus;
  90. int n;
  91. {
  92. register int ruleno;
  93. register unsigned word;
  94. register unsigned i;
  95. register short *csp;
  96. register unsigned *dsp;
  97. register unsigned *rsp;
  98. register int rulesetsize;
  99. short *csend;
  100. unsigned *rsend;
  101. int symbol;
  102. int itemno;
  103. rulesetsize = WORDSIZE(nrules);
  104. rsp = ruleset;
  105. rsend = ruleset + rulesetsize;
  106. for (rsp = ruleset; rsp < rsend; rsp++)
  107. *rsp = 0;
  108. csend = nucleus + n;
  109. for (csp = nucleus; csp < csend; ++csp)
  110. {
  111. symbol = ritem[*csp];
  112. if (ISVAR(symbol))
  113. {
  114. dsp = first_derives + symbol * rulesetsize;
  115. rsp = ruleset;
  116. while (rsp < rsend)
  117. *rsp++ |= *dsp++;
  118. }
  119. }
  120. ruleno = 0;
  121. itemsetend = itemset;
  122. csp = nucleus;
  123. for (rsp = ruleset; rsp < rsend; ++rsp)
  124. {
  125. word = *rsp;
  126. if (word)
  127. {
  128. for (i = 0; i < BITS_PER_WORD; ++i)
  129. {
  130. if (word & (1 << i))
  131. {
  132. itemno = rrhs[ruleno+i];
  133. while (csp < csend && *csp < itemno)
  134. #if defined(KYLEP_CHANGE)
  135. *itemsetend++ = (short)*csp++;
  136. *itemsetend++ = (short)itemno;
  137. #else
  138. *itemsetend++ = *csp++;
  139. *itemsetend++ = itemno;
  140. #endif // KYLEP_CHANGE
  141. while (csp < csend && *csp == itemno)
  142. ++csp;
  143. }
  144. }
  145. }
  146. ruleno += BITS_PER_WORD;
  147. }
  148. while (csp < csend)
  149. *itemsetend++ = *csp++;
  150. #ifdef DEBUG
  151. print_closure(n);
  152. #endif
  153. }
  154. #if defined(KYLEP_CHANGE)
  155. void
  156. #endif
  157. finalize_closure()
  158. {
  159. FREE(itemset);
  160. FREE(ruleset);
  161. FREE(first_derives + ntokens * WORDSIZE(nrules));
  162. }
  163. #ifdef DEBUG
  164. print_closure(n)
  165. int n;
  166. {
  167. register short *isp;
  168. printf("\n\nn = %d\n\n", n);
  169. for (isp = itemset; isp < itemsetend; isp++)
  170. printf(" %d\n", *isp);
  171. }
  172. print_EFF()
  173. {
  174. register int i, j;
  175. register unsigned *rowp;
  176. register unsigned word;
  177. register unsigned k;
  178. printf("\n\nEpsilon Free Firsts\n");
  179. for (i = start_symbol; i < nsyms; i++)
  180. {
  181. printf("\n%s", symbol_name[i]);
  182. rowp = EFF + ((i - start_symbol) * WORDSIZE(nvars));
  183. word = *rowp++;
  184. k = BITS_PER_WORD;
  185. for (j = 0; j < nvars; k++, j++)
  186. {
  187. if (k >= BITS_PER_WORD)
  188. {
  189. word = *rowp++;
  190. k = 0;
  191. }
  192. if (word & (1 << k))
  193. printf(" %s", symbol_name[start_symbol + j]);
  194. }
  195. }
  196. }
  197. print_first_derives()
  198. {
  199. register int i;
  200. register int j;
  201. register unsigned *rp;
  202. register unsigned cword;
  203. register unsigned k;
  204. printf("\n\n\nFirst Derives\n");
  205. for (i = start_symbol; i < nsyms; i++)
  206. {
  207. printf("\n%s derives\n", symbol_name[i]);
  208. rp = first_derives + i * WORDSIZE(nrules);
  209. k = BITS_PER_WORD;
  210. for (j = 0; j <= nrules; k++, j++)
  211. {
  212. if (k >= BITS_PER_WORD)
  213. {
  214. cword = *rp++;
  215. k = 0;
  216. }
  217. if (cword & (1 << k))
  218. printf(" %d\n", j);
  219. }
  220. }
  221. fflush(stdout);
  222. }
  223. #endif