]> git.saurik.com Git - bison.git/blob - src/closure.c
ac37b2c018021e162b9520e5d84670f585618bed
[bison.git] / src / closure.c
1 /* Subroutines for bison
2 Copyright 1984, 1989, 2000, 2001 Free Software Foundation, Inc.
3
4 This file is part of Bison, the GNU Compiler Compiler.
5
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.
10
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.
15
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. */
20
21 #include "system.h"
22 #include "getargs.h"
23 #include "gram.h"
24 #include "reader.h"
25 #include "closure.h"
26 #include "derives.h"
27 #include "warshall.h"
28
29 /* ITEMSETSIZE is the size of the array ITEMSET. */
30 short *itemset;
31 size_t itemsetsize;
32
33 static unsigned *ruleset;
34
35 /* internal data. See comments before set_fderives and set_firsts. */
36 static unsigned *fderives;
37 static unsigned *firsts;
38
39 #define FDERIVES(Symbol) (fderives + (Symbol) * rulesetsize)
40 #define FIRSTS(Symbol) (firsts + (Symbol) * varsetsize)
41
42 /* number of words required to hold a bit for each rule */
43 static int rulesetsize;
44
45 /* number of words required to hold a bit for each variable */
46 static int varsetsize;
47 \f
48
49 /*-----------------.
50 | Debugging code. |
51 `-----------------*/
52
53 static void
54 print_closure (int n)
55 {
56 int i;
57 fprintf (stderr, "n = %d\n", n);
58 for (i = 0; i < itemsetsize; ++i)
59 fprintf (stderr, " %d\n", itemset[i]);
60 fprintf (stderr, "\n\n");
61 }
62
63
64 static void
65 print_firsts (void)
66 {
67 int i;
68 int j;
69 unsigned *rowp;
70
71 fprintf (stderr, "FIRSTS\n");
72
73 for (i = ntokens; i < nsyms; i++)
74 {
75 fprintf (stderr, "\t%s firsts\n", tags[i]);
76
77 rowp = FIRSTS (i - ntokens);
78
79 for (j = 0; j < nvars; j++)
80 if (BITISSET (rowp, j))
81 fprintf (stderr, "\t\t%d (%s)\n", j + ntokens, tags[j + ntokens]);
82 }
83 fprintf (stderr, "\n\n");
84 }
85
86
87 static void
88 print_fderives (void)
89 {
90 int i;
91 int j;
92 unsigned *rp;
93
94 fprintf (stderr, "FDERIVES\n");
95
96 for (i = ntokens; i < nsyms; i++)
97 {
98 fprintf (stderr, "\t%s derives\n", tags[i]);
99 rp = FDERIVES (i);
100
101 for (j = 0; j <= nrules; j++)
102 if (BITISSET (rp, j))
103 fprintf (stderr, "\t\t%d (%s)\n", j, tags[j]);
104 }
105 fprintf (stderr, "\n\n");
106 }
107 \f
108 /*-------------------------------------------------------------------.
109 | Set FIRSTS to be an NVARS by NVARS bit matrix indicating which |
110 | items can represent the beginning of the input corresponding to |
111 | which other items. |
112 | |
113 | For example, if some rule expands symbol 5 into the sequence of |
114 | symbols 8 3 20, the symbol 8 can be the beginning of the data for |
115 | symbol 5, so the bit [8 - ntokens, 5 - ntokens] in firsts is set. |
116 `-------------------------------------------------------------------*/
117
118 static void
119 set_firsts (void)
120 {
121 unsigned *row;
122 int symbol;
123 short *sp;
124 int rowsize;
125
126 int i;
127
128 varsetsize = rowsize = WORDSIZE (nvars);
129
130 firsts = XCALLOC (unsigned, nvars * rowsize);
131
132 row = firsts;
133 for (i = ntokens; i < nsyms; i++)
134 {
135 sp = derives[i];
136 while (*sp >= 0)
137 {
138 symbol = ritem[rule_table[*sp++].rhs];
139 if (ISVAR (symbol))
140 {
141 symbol -= ntokens;
142 SETBIT (row, symbol);
143 }
144 }
145
146 row += rowsize;
147 }
148
149 RTC (firsts, nvars);
150
151 if (trace_flag)
152 print_firsts ();
153 }
154
155 /*-------------------------------------------------------------------.
156 | Set FDERIVES to an NVARS by NRULES matrix of bits indicating which |
157 | rules can help derive the beginning of the data for each |
158 | nonterminal. |
159 | |
160 | For example, if symbol 5 can be derived as the sequence of symbols |
161 | 8 3 20, and one of the rules for deriving symbol 8 is rule 4, then |
162 | the [5 - NTOKENS, 4] bit in FDERIVES is set. |
163 `-------------------------------------------------------------------*/
164
165 static void
166 set_fderives (void)
167 {
168 unsigned *rrow;
169 unsigned *vrow;
170 int j;
171 unsigned cword;
172 short *rp;
173 int b;
174
175 int ruleno;
176 int i;
177
178 fderives = XCALLOC (unsigned, nvars * rulesetsize) - ntokens * rulesetsize;
179
180 set_firsts ();
181
182 rrow = FDERIVES (ntokens);
183
184 for (i = ntokens; i < nsyms; i++)
185 {
186 vrow = FIRSTS (i - ntokens);
187 cword = *vrow++;
188 b = 0;
189 for (j = ntokens; j < nsyms; j++)
190 {
191 if (cword & (1 << b))
192 {
193 rp = derives[j];
194 while ((ruleno = *rp++) > 0)
195 SETBIT (rrow, ruleno);
196 }
197
198 b++;
199 if (b >= BITS_PER_WORD && j + 1 < nsyms)
200 {
201 cword = *vrow++;
202 b = 0;
203 }
204 }
205
206 rrow += rulesetsize;
207 }
208
209 if (trace_flag)
210 print_fderives ();
211
212 XFREE (firsts);
213 }
214 \f
215
216 void
217 new_closure (int n)
218 {
219 itemset = XCALLOC (short, n);
220
221 rulesetsize = WORDSIZE (nrules + 1);
222 ruleset = XCALLOC (unsigned, rulesetsize);
223
224 set_fderives ();
225 }
226
227
228
229 void
230 closure (short *core, int n)
231 {
232 int ruleno;
233 short *csp;
234
235 int itemno;
236 int i;
237
238 if (trace_flag)
239 {
240 fprintf (stderr, "Entering closure (items = {");
241 for (i = 0; i < n; ++i)
242 fprintf (stderr, " %d ", core[i]);
243 fprintf (stderr, "}, nitems = %d)\n", n);
244 }
245
246 if (n == 0)
247 {
248 for (i = 0; i < rulesetsize; ++i)
249 ruleset[i] = FDERIVES (start_symbol)[i];
250 }
251 else
252 {
253 for (i = 0; i < rulesetsize; ++i)
254 ruleset[i] = 0;
255
256 for (i = 0; i < n; ++i)
257 {
258 int symbol = ritem[core[i]];
259 if (ISVAR (symbol))
260 {
261 int j;
262 for (j = 0; j < rulesetsize; ++j)
263 ruleset[j] |= FDERIVES (symbol)[j];
264 }
265 }
266 }
267
268 ruleno = 0;
269 itemsetsize = 0;
270 csp = core;
271 for (i = 0; i < rulesetsize; ++i)
272 {
273 int word = ruleset[i];
274 if (word == 0)
275 {
276 ruleno += BITS_PER_WORD;
277 }
278 else
279 {
280 int b;
281
282 for (b = 0; b < BITS_PER_WORD; b++)
283 {
284 if (word & (1 << b))
285 {
286 itemno = rule_table[ruleno].rhs;
287 while (csp < (core + n) && *csp < itemno)
288 itemset[itemsetsize++] = *csp++;
289 itemset[itemsetsize++] = itemno;
290 }
291
292 ruleno++;
293 }
294 }
295 }
296
297 while (csp < (core + n))
298 itemset[itemsetsize++] = *csp++;
299
300 if (trace_flag)
301 print_closure (n);
302 }
303
304
305 void
306 free_closure (void)
307 {
308 XFREE (itemset);
309 XFREE (ruleset);
310 XFREE (fderives + ntokens * rulesetsize);
311 }