closure.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /* Subroutines for bison
  2. Copyright (C) 1984 Bob Corbett and Free Software Foundation, Inc.
  3. BISON is distributed in the hope that it will be useful, but WITHOUT ANY
  4. WARRANTY. No author or distributor accepts responsibility to anyone
  5. for the consequences of using it or for whether it serves any
  6. particular purpose or works at all, unless he says so in writing.
  7. Refer to the BISON General Public License for full details.
  8. Everyone is granted permission to copy, modify and redistribute BISON,
  9. but only under the conditions described in the BISON General Public
  10. License. A copy of this license is supposed to have been given to you
  11. along with BISON so you can know your rights and responsibilities. It
  12. should be in a file named COPYING. Among other things, the copyright
  13. notice and this notice must be preserved on all copies.
  14. In other words, you are welcome to use, share and improve this program.
  15. You are forbidden to forbid anyone else to use, share and improve
  16. what you give them. Help stamp out software-hoarding! */
  17. /* subroutines of file LR0.c.
  18. Entry points:
  19. closure (items, n)
  20. Given a vector of item numbers items, of length n,
  21. set up ruleset and itemset to indicate what rules could be run
  22. and which items could be accepted when those items are the active ones.
  23. ruleset contains a bit for each rule. closure sets the bits
  24. for all rules which could potentially describe the next input to be read.
  25. itemset is a vector of item numbers; itemsetend points to just beyond the end
  26. of the part of it that is significant.
  27. closure places there the indices of all items which represent units of
  28. input that could arrive next.
  29. initialize_closure (n)
  30. Allocates the itemset and ruleset vectors,
  31. and precomputes useful data so that closure can be called.
  32. n is the number of elements to allocate for itemset.
  33. finalize_closure ()
  34. Frees itemset, ruleset and internal data.
  35. */
  36. #include <stdio.h>
  37. #include "machine.h"
  38. #include "new.h"
  39. #include "gram.h"
  40. extern short **derives;
  41. short *itemset;
  42. short *itemsetend;
  43. static unsigned *ruleset;
  44. /* internal data. See comments before set_fderives and set_firsts. */
  45. static unsigned *fderives;
  46. static unsigned *firsts;
  47. /* number of words required to hold a bit for each rule */
  48. static int rulesetsize;
  49. /* number of words required to hold a bit for each variable */
  50. static int varsetsize;
  51. initialize_closure(n)
  52. int n;
  53. {
  54. itemset = NEW2(n, short);
  55. rulesetsize = WORDSIZE(nrules + 1);
  56. ruleset = NEW2(rulesetsize, unsigned);
  57. set_fderives();
  58. }
  59. /* set fderives to an nvars by nrules matrix of bits
  60. indicating which rules can help derive the beginning of the data
  61. for each nonterminal. For example, if symbol 5 can be derived as
  62. the sequence of symbols 8 3 20, and one of the rules for deriving
  63. symbol 8 is rule 4, then the [5 - ntokens, 4] bit in fderives is set. */
  64. set_fderives()
  65. {
  66. register unsigned *rrow;
  67. register unsigned *vrow;
  68. register int j;
  69. register unsigned mask;
  70. register unsigned cword;
  71. register short *rp;
  72. int ruleno;
  73. int i;
  74. fderives = NEW2(nvars * rulesetsize, unsigned) - ntokens * rulesetsize;
  75. set_firsts();
  76. rrow = fderives + ntokens * rulesetsize;
  77. for (i = ntokens; i < nsyms; i++)
  78. {
  79. vrow = firsts + ((i - ntokens) * varsetsize);
  80. cword = *vrow++;
  81. mask = 1;
  82. for (j = ntokens; j < nsyms; j++)
  83. {
  84. if (cword & mask)
  85. {
  86. rp = derives[j];
  87. while ((ruleno = *rp++) > 0)
  88. {
  89. SETBIT(rrow, ruleno);
  90. }
  91. }
  92. mask <<= 1;
  93. if (mask == 0 && j + 1 < nsyms)
  94. {
  95. cword = *vrow++;
  96. mask = 1;
  97. }
  98. }
  99. vrow += varsetsize;
  100. rrow += rulesetsize;
  101. }
  102. #ifdef DEBUG
  103. print_fderives();
  104. #endif
  105. FREE(firsts);
  106. }
  107. /* set firsts to be an nvars by nvars bit matrix indicating which items
  108. can represent the beginning of the input corresponding to which other items.
  109. For example, if some rule expands symbol 5 into the sequence of symbols 8 3 20,
  110. the symbol 8 can be the beginning of the data for symbol 5,
  111. so the bit [8 - ntokens, 5 - ntokens] in firsts is set. */
  112. set_firsts()
  113. {
  114. register unsigned *row;
  115. /* register int done; JF unused */
  116. register int symbol;
  117. register short *sp;
  118. register int rowsize;
  119. int i;
  120. varsetsize = rowsize = WORDSIZE(nvars);
  121. firsts = NEW2(nvars * rowsize, unsigned);
  122. row = firsts;
  123. for (i = ntokens; i < nsyms; i++)
  124. {
  125. sp = derives[i];
  126. while (*sp >= 0)
  127. {
  128. symbol = ritem[rrhs[*sp++]];
  129. if (ISVAR(symbol))
  130. {
  131. symbol -= ntokens;
  132. SETBIT(row, symbol);
  133. }
  134. }
  135. row += rowsize;
  136. }
  137. RTC(firsts, nvars);
  138. #ifdef DEBUG
  139. print_firsts();
  140. #endif
  141. }
  142. closure(core, n)
  143. short *core;
  144. int n;
  145. {
  146. register int ruleno;
  147. register unsigned word;
  148. register unsigned mask;
  149. register short *csp;
  150. register unsigned *dsp;
  151. register unsigned *rsp;
  152. short *csend;
  153. unsigned *rsend;
  154. int symbol;
  155. int itemno;
  156. rsp = ruleset;
  157. rsend = ruleset + rulesetsize;
  158. csend = core + n;
  159. if (n == 0)
  160. {
  161. dsp = fderives + start_symbol * rulesetsize;
  162. while (rsp < rsend)
  163. *rsp++ = *dsp++;
  164. }
  165. else
  166. {
  167. while (rsp < rsend)
  168. *rsp++ = 0;
  169. csp = core;
  170. while (csp < csend)
  171. {
  172. symbol = ritem[*csp++];
  173. if (ISVAR(symbol))
  174. {
  175. dsp = fderives + symbol * rulesetsize;
  176. rsp = ruleset;
  177. while (rsp < rsend)
  178. *rsp++ |= *dsp++;
  179. }
  180. }
  181. }
  182. ruleno = 0;
  183. itemsetend = itemset;
  184. csp = core;
  185. rsp = ruleset;
  186. while (rsp < rsend)
  187. {
  188. word = *rsp++;
  189. if (word == 0)
  190. {
  191. ruleno += BITS_PER_WORD;
  192. }
  193. else
  194. {
  195. mask = 1;
  196. while (mask)
  197. {
  198. if (word & mask)
  199. {
  200. itemno = rrhs[ruleno];
  201. while (csp < csend && *csp < itemno)
  202. *itemsetend++ = *csp++;
  203. *itemsetend++ = itemno;
  204. }
  205. mask <<= 1;
  206. ruleno++;
  207. }
  208. }
  209. }
  210. while (csp < csend)
  211. *itemsetend++ = *csp++;
  212. #ifdef DEBUG
  213. print_closure(n);
  214. #endif
  215. }
  216. finalize_closure()
  217. {
  218. FREE(itemset);
  219. FREE(ruleset);
  220. FREE(fderives + ntokens * rulesetsize);
  221. }
  222. #ifdef DEBUG
  223. print_closure(n)
  224. int n;
  225. {
  226. register short *isp;
  227. printf("\n\nn = %d\n\n", n);
  228. for (isp = itemset; isp < itemsetend; isp++)
  229. printf(" %d\n", *isp);
  230. }
  231. print_firsts()
  232. {
  233. register int i;
  234. register int j;
  235. register unsigned *rowp;
  236. register unsigned cword;
  237. register unsigned mask;
  238. extern char **tags;
  239. printf("\n\n\nFIRSTS\n\n");
  240. for (i = ntokens; i < nsyms; i++)
  241. {
  242. printf("\n\n%s firsts\n\n", tags[i]);
  243. rowp = firsts + ((i - ntokens) * vrowsize);
  244. cword = *rowp++;
  245. mask = 1;
  246. for (j = 0; j < nsyms; j++)
  247. {
  248. if (cword & mask)
  249. printf(" %s\n", tags[j + ntokens]);
  250. mask <<= 1;
  251. if (mask == 0 && j + 1 < nsyms)
  252. {
  253. cword = *rowp++;
  254. mask = 1;
  255. }
  256. }
  257. }
  258. }
  259. print_fderives()
  260. {
  261. register int i;
  262. register int j;
  263. register unsigned *rp;
  264. register unsigned cword;
  265. register unsigned mask;
  266. extern char **tags;
  267. printf("\n\n\nFDERIVES\n");
  268. for (i = ntokens; i < nsyms; i++)
  269. {
  270. printf("\n\n%s derives\n\n", tags[i]);
  271. rp = fderives + i * rrowsize;
  272. cword = *rp++;
  273. mask = 1;
  274. for (j = 0; j <= nrules; j++)
  275. {
  276. if (cword & mask)
  277. printf(" %d\n", j);
  278. mask <<= 1;
  279. if (mask == 0 && j + 1 < nrules)
  280. {
  281. cword = *rp++;
  282. mask = 1;
  283. }
  284. }
  285. }
  286. fflush(stdout);
  287. }
  288. #endif