CLOSURE.C 6.6 KB

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