CLOSURE.C 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 1, 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. void set_fderives();
  41. void set_firsts();
  42. extern void RTC();
  43. short *itemset;
  44. short *itemsetend;
  45. static unsigned *ruleset;
  46. /* internal data. See comments before set_fderives and set_firsts. */
  47. static unsigned *fderives;
  48. static unsigned *firsts;
  49. /* number of words required to hold a bit for each rule */
  50. static int rulesetsize;
  51. /* number of words required to hold a bit for each variable */
  52. static int varsetsize;
  53. void
  54. initialize_closure(n)
  55. int n;
  56. {
  57. itemset = NEW2(n, short);
  58. rulesetsize = WORDSIZE(nrules + 1);
  59. ruleset = NEW2(rulesetsize, unsigned);
  60. set_fderives();
  61. }
  62. /* set fderives to an nvars by nrules matrix of bits
  63. indicating which rules can help derive the beginning of the data
  64. for each nonterminal. For example, if symbol 5 can be derived as
  65. the sequence of symbols 8 3 20, and one of the rules for deriving
  66. symbol 8 is rule 4, then the [5 - ntokens, 4] bit in fderives is set. */
  67. void
  68. set_fderives()
  69. {
  70. register unsigned *rrow;
  71. register unsigned *vrow;
  72. register int j;
  73. register unsigned mask;
  74. register unsigned cword;
  75. register short *rp;
  76. int ruleno;
  77. int i;
  78. fderives = NEW2(nvars * rulesetsize, unsigned) - ntokens * rulesetsize;
  79. set_firsts();
  80. rrow = fderives + ntokens * rulesetsize;
  81. for (i = ntokens; i < nsyms; i++)
  82. {
  83. vrow = firsts + ((i - ntokens) * varsetsize);
  84. cword = *vrow++;
  85. mask = 1;
  86. for (j = ntokens; j < nsyms; j++)
  87. {
  88. if (cword & mask)
  89. {
  90. rp = derives[j];
  91. while ((ruleno = *rp++) > 0)
  92. {
  93. SETBIT(rrow, ruleno);
  94. }
  95. }
  96. mask <<= 1;
  97. if (mask == 0 && j + 1 < nsyms)
  98. {
  99. cword = *vrow++;
  100. mask = 1;
  101. }
  102. }
  103. vrow += varsetsize;
  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 unsigned mask;
  155. register short *csp;
  156. register unsigned *dsp;
  157. register unsigned *rsp;
  158. short *csend;
  159. unsigned *rsend;
  160. int symbol;
  161. int itemno;
  162. rsp = ruleset;
  163. rsend = ruleset + rulesetsize;
  164. csend = core + n;
  165. if (n == 0)
  166. {
  167. dsp = fderives + start_symbol * rulesetsize;
  168. while (rsp < rsend)
  169. *rsp++ = *dsp++;
  170. }
  171. else
  172. {
  173. while (rsp < rsend)
  174. *rsp++ = 0;
  175. csp = core;
  176. while (csp < csend)
  177. {
  178. symbol = ritem[*csp++];
  179. if (ISVAR(symbol))
  180. {
  181. dsp = fderives + symbol * rulesetsize;
  182. rsp = ruleset;
  183. while (rsp < rsend)
  184. *rsp++ |= *dsp++;
  185. }
  186. }
  187. }
  188. ruleno = 0;
  189. itemsetend = itemset;
  190. csp = core;
  191. rsp = ruleset;
  192. while (rsp < rsend)
  193. {
  194. word = *rsp++;
  195. if (word == 0)
  196. {
  197. ruleno += BITS_PER_WORD;
  198. }
  199. else
  200. {
  201. mask = 1;
  202. while (mask)
  203. {
  204. if (word & mask)
  205. {
  206. itemno = rrhs[ruleno];
  207. while (csp < csend && *csp < itemno)
  208. *itemsetend++ = *csp++;
  209. *itemsetend++ = itemno;
  210. }
  211. mask <<= 1;
  212. ruleno++;
  213. }
  214. }
  215. }
  216. while (csp < csend)
  217. *itemsetend++ = *csp++;
  218. #ifdef DEBUG
  219. print_closure(n);
  220. #endif
  221. }
  222. void
  223. finalize_closure()
  224. {
  225. FREE(itemset);
  226. FREE(ruleset);
  227. FREE(fderives + ntokens * rulesetsize);
  228. }
  229. #ifdef DEBUG
  230. print_closure(n)
  231. int n;
  232. {
  233. register short *isp;
  234. printf("\n\nn = %d\n\n", n);
  235. for (isp = itemset; isp < itemsetend; isp++)
  236. printf(" %d\n", *isp);
  237. }
  238. print_firsts()
  239. {
  240. register int i;
  241. register int j;
  242. register unsigned *rowp;
  243. register unsigned cword;
  244. register unsigned mask;
  245. extern char **tags;
  246. printf("\n\n\nFIRSTS\n\n");
  247. for (i = ntokens; i < nsyms; i++)
  248. {
  249. printf("\n\n%s firsts\n\n", tags[i]);
  250. rowp = firsts + ((i - ntokens) * vrowsize);
  251. cword = *rowp++;
  252. mask = 1;
  253. for (j = 0; j < nsyms; j++)
  254. {
  255. if (cword & mask)
  256. printf(" %s\n", tags[j + ntokens]);
  257. mask <<= 1;
  258. if (mask == 0 && j + 1 < nsyms)
  259. {
  260. cword = *rowp++;
  261. mask = 1;
  262. }
  263. }
  264. }
  265. }
  266. print_fderives()
  267. {
  268. register int i;
  269. register int j;
  270. register unsigned *rp;
  271. register unsigned cword;
  272. register unsigned mask;
  273. extern char **tags;
  274. printf("\n\n\nFDERIVES\n");
  275. for (i = ntokens; i < nsyms; i++)
  276. {
  277. printf("\n\n%s derives\n\n", tags[i]);
  278. rp = fderives + i * rrowsize;
  279. cword = *rp++;
  280. mask = 1;
  281. for (j = 0; j <= nrules; j++)
  282. {
  283. if (cword & mask)
  284. printf(" %d\n", j);
  285. mask <<= 1;
  286. if (mask == 0 && j + 1 < nrules)
  287. {
  288. cword = *rp++;
  289. mask = 1;
  290. }
  291. }
  292. }
  293. fflush(stdout);
  294. }
  295. #endif