]> git.saurik.com Git - bison.git/blame - src/closure.c
More ignores.
[bison.git] / src / closure.c
CommitLineData
d0fb370f 1/* Subroutines for bison
aa7815f5 2 Copyright 1984, 1989, 2000 Free Software Foundation, Inc.
d0fb370f 3
8dc26b76 4 This file is part of Bison, the GNU Compiler Compiler.
d0fb370f 5
8dc26b76
AD
6 Bison is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
d0fb370f 10
8dc26b76
AD
11 Bison is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
d0fb370f 15
8dc26b76
AD
16 You should have received a copy of the GNU General Public License
17 along with Bison; see the file COPYING. If not, write to the Free
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
d0fb370f 20
d0fb370f 21#include "system.h"
d0fb370f 22#include "gram.h"
2fa6973e 23#include "closure.h"
340ef489 24#include "derives.h"
d7913476 25#include "warshall.h"
d0fb370f
RS
26
27short *itemset;
28short *itemsetend;
29static unsigned *ruleset;
30
31/* internal data. See comments before set_fderives and set_firsts. */
32static unsigned *fderives;
33static unsigned *firsts;
34
35/* number of words required to hold a bit for each rule */
36static int rulesetsize;
37
38/* number of words required to hold a bit for each variable */
39static int varsetsize;
2fa6973e
AD
40\f
41#if DEBUG
d0fb370f 42
2fa6973e
AD
43/*-----------------.
44| Debugging code. |
45`-----------------*/
d0fb370f 46
2fa6973e
AD
47static void
48print_closure (int n)
49{
50 short *isp;
51
52 printf ("\n\nn = %d\n\n", n);
53 for (isp = itemset; isp < itemsetend; isp++)
54 printf (" %d\n", *isp);
55}
56
57
58static void
59print_firsts (void)
d0fb370f 60{
2fa6973e
AD
61 int i;
62 int j;
63 unsigned *rowp;
64
65 printf ("\n\n\nFIRSTS\n\n");
66
67 for (i = ntokens; i < nsyms; i++)
68 {
69 printf ("\n\n%s firsts\n\n", tags[i]);
d0fb370f 70
2fa6973e 71 rowp = firsts + ((i - ntokens) * varsetsize);
d0fb370f 72
2fa6973e
AD
73 for (j = 0; j < nvars; j++)
74 if (BITISSET (rowp, j))
75 printf (" %s\n", tags[j + ntokens]);
76 }
d0fb370f
RS
77}
78
79
2fa6973e
AD
80static void
81print_fderives (void)
82{
83 int i;
84 int j;
85 unsigned *rp;
86
87 printf ("\n\n\nFDERIVES\n");
88
89 for (i = ntokens; i < nsyms; i++)
90 {
91 printf ("\n\n%s derives\n\n", tags[i]);
92 rp = fderives + i * rulesetsize;
93
94 for (j = 0; j <= nrules; j++)
95 if (BITISSET (rp, j))
96 printf (" %d\n", j);
97 }
98
99 fflush (stdout);
100}
101#endif
102\f
103/*-------------------------------------------------------------------.
104| Set FIRSTS to be an NVARS by NVARS bit matrix indicating which |
105| items can represent the beginning of the input corresponding to |
106| which other items. |
107| |
108| For example, if some rule expands symbol 5 into the sequence of |
109| symbols 8 3 20, the symbol 8 can be the beginning of the data for |
110| symbol 5, so the bit [8 - ntokens, 5 - ntokens] in firsts is set. |
111`-------------------------------------------------------------------*/
112
113static void
114set_firsts (void)
115{
116 unsigned *row;
117 int symbol;
118 short *sp;
119 int rowsize;
120
121 int i;
122
123 varsetsize = rowsize = WORDSIZE (nvars);
124
d7913476 125 firsts = XCALLOC (unsigned, nvars * rowsize);
2fa6973e
AD
126
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
141 row += rowsize;
142 }
143
144 RTC (firsts, nvars);
145
146#ifdef DEBUG
147 print_firsts ();
148#endif
149}
150
151/*-------------------------------------------------------------------.
152| Set FDERIVES to an NVARS by NRULES matrix of bits indicating which |
153| rules can help derive the beginning of the data for each |
154| nonterminal. |
155| |
156| For example, if symbol 5 can be derived as the sequence of symbols |
157| 8 3 20, and one of the rules for deriving symbol 8 is rule 4, then |
158| the [5 - NTOKENS, 4] bit in FDERIVES is set. |
159`-------------------------------------------------------------------*/
d0fb370f 160
4a120d45 161static void
d2729d44 162set_fderives (void)
d0fb370f 163{
2fa6973e
AD
164 unsigned *rrow;
165 unsigned *vrow;
166 int j;
167 unsigned cword;
168 short *rp;
169 int b;
d0fb370f
RS
170
171 int ruleno;
172 int i;
173
d7913476 174 fderives = XCALLOC (unsigned, nvars * rulesetsize) - ntokens * rulesetsize;
d0fb370f 175
2fa6973e 176 set_firsts ();
d0fb370f
RS
177
178 rrow = fderives + ntokens * rulesetsize;
179
180 for (i = ntokens; i < nsyms; i++)
181 {
182 vrow = firsts + ((i - ntokens) * varsetsize);
183 cword = *vrow++;
184 b = 0;
185 for (j = ntokens; j < nsyms; j++)
186 {
187 if (cword & (1 << b))
188 {
189 rp = derives[j];
190 while ((ruleno = *rp++) > 0)
191 {
2fa6973e 192 SETBIT (rrow, ruleno);
d0fb370f
RS
193 }
194 }
195
196 b++;
197 if (b >= BITS_PER_WORD && j + 1 < nsyms)
198 {
199 cword = *vrow++;
200 b = 0;
201 }
202 }
203
204 rrow += rulesetsize;
205 }
206
207#ifdef DEBUG
2fa6973e 208 print_fderives ();
d0fb370f
RS
209#endif
210
d7913476 211 XFREE (firsts);
d0fb370f 212}
2fa6973e 213\f
d0fb370f 214
2fa6973e
AD
215void
216new_closure (int n)
d0fb370f 217{
d7913476 218 itemset = XCALLOC (short, n);
d0fb370f 219
2fa6973e 220 rulesetsize = WORDSIZE (nrules + 1);
d7913476 221 ruleset = XCALLOC (unsigned, rulesetsize);
d0fb370f 222
2fa6973e 223 set_fderives ();
d0fb370f
RS
224}
225
226
2fa6973e 227
d0fb370f 228void
d2729d44 229closure (short *core, int n)
d0fb370f 230{
2fa6973e
AD
231 int ruleno;
232 unsigned word;
233 short *csp;
234 unsigned *dsp;
235 unsigned *rsp;
d0fb370f
RS
236
237 short *csend;
238 unsigned *rsend;
239 int symbol;
240 int itemno;
241
242 rsp = ruleset;
243 rsend = ruleset + rulesetsize;
244 csend = core + n;
245
246 if (n == 0)
247 {
248 dsp = fderives + start_symbol * rulesetsize;
249 while (rsp < rsend)
250 *rsp++ = *dsp++;
251 }
252 else
253 {
254 while (rsp < rsend)
255 *rsp++ = 0;
256
257 csp = core;
258 while (csp < csend)
259 {
260 symbol = ritem[*csp++];
2fa6973e 261 if (ISVAR (symbol))
d0fb370f
RS
262 {
263 dsp = fderives + symbol * rulesetsize;
264 rsp = ruleset;
265 while (rsp < rsend)
266 *rsp++ |= *dsp++;
267 }
268 }
269 }
270
271 ruleno = 0;
272 itemsetend = itemset;
273 csp = core;
274 rsp = ruleset;
275 while (rsp < rsend)
276 {
277 word = *rsp++;
278 if (word == 0)
279 {
280 ruleno += BITS_PER_WORD;
281 }
282 else
283 {
2fa6973e 284 int b;
d0fb370f
RS
285
286 for (b = 0; b < BITS_PER_WORD; b++)
287 {
288 if (word & (1 << b))
289 {
290 itemno = rrhs[ruleno];
291 while (csp < csend && *csp < itemno)
292 *itemsetend++ = *csp++;
293 *itemsetend++ = itemno;
294 }
295
296 ruleno++;
297 }
298 }
299 }
300
301 while (csp < csend)
302 *itemsetend++ = *csp++;
303
2fa6973e
AD
304#if DEBUG
305 print_closure (n);
d0fb370f
RS
306#endif
307}
308
309
310void
2fa6973e 311free_closure (void)
d0fb370f 312{
d7913476
AD
313 XFREE (itemset);
314 XFREE (ruleset);
315 XFREE (fderives + ntokens * rulesetsize);
d0fb370f 316}