]> git.saurik.com Git - bison.git/blob - src/closure.c
2cee4f80903669a1376dd268f1eb3ad60fc040ef
[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 int 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 {
104 short *rhsp;
105 fprintf (stderr, "\t\t%d:", j);
106 for (rhsp = ritem + rule_table[j].rhs; *rhsp > 0; ++rhsp)
107 fprintf (stderr, " %s", tags[*rhsp]);
108 fputc ('\n', stderr);
109 }
110 }
111 fprintf (stderr, "\n\n");
112 }
113 \f
114 /*-------------------------------------------------------------------.
115 | Set FIRSTS to be an NVARS by NVARS bit matrix indicating which |
116 | items can represent the beginning of the input corresponding to |
117 | which other items. |
118 | |
119 | For example, if some rule expands symbol 5 into the sequence of |
120 | symbols 8 3 20, the symbol 8 can be the beginning of the data for |
121 | symbol 5, so the bit [8 - ntokens, 5 - ntokens] in firsts is set. |
122 `-------------------------------------------------------------------*/
123
124 static void
125 set_firsts (void)
126 {
127 unsigned *row;
128 int symbol;
129 short *sp;
130 int rowsize;
131
132 int i;
133
134 varsetsize = rowsize = WORDSIZE (nvars);
135
136 firsts = XCALLOC (unsigned, nvars * rowsize);
137
138 row = firsts;
139 for (i = ntokens; i < nsyms; i++)
140 {
141 sp = derives[i];
142 while (*sp >= 0)
143 {
144 symbol = ritem[rule_table[*sp++].rhs];
145 if (ISVAR (symbol))
146 {
147 symbol -= ntokens;
148 SETBIT (row, symbol);
149 }
150 }
151
152 row += rowsize;
153 }
154
155 RTC (firsts, nvars);
156
157 if (trace_flag)
158 print_firsts ();
159 }
160
161 /*-------------------------------------------------------------------.
162 | Set FDERIVES to an NVARS by NRULES matrix of bits indicating which |
163 | rules can help derive the beginning of the data for each |
164 | nonterminal. |
165 | |
166 | For example, if symbol 5 can be derived as the sequence of symbols |
167 | 8 3 20, and one of the rules for deriving symbol 8 is rule 4, then |
168 | the [5 - NTOKENS, 4] bit in FDERIVES is set. |
169 `-------------------------------------------------------------------*/
170
171 static void
172 set_fderives (void)
173 {
174 unsigned *rrow;
175 unsigned *vrow;
176 int j;
177 unsigned cword;
178 short *rp;
179 int b;
180
181 int ruleno;
182 int i;
183
184 fderives = XCALLOC (unsigned, nvars * rulesetsize) - ntokens * rulesetsize;
185
186 set_firsts ();
187
188 rrow = FDERIVES (ntokens);
189
190 for (i = ntokens; i < nsyms; i++)
191 {
192 vrow = FIRSTS (i - ntokens);
193 cword = *vrow++;
194 b = 0;
195 for (j = ntokens; j < nsyms; j++)
196 {
197 if (cword & (1 << b))
198 {
199 rp = derives[j];
200 while ((ruleno = *rp++) > 0)
201 SETBIT (rrow, ruleno);
202 }
203
204 b++;
205 if (b >= BITS_PER_WORD && j + 1 < nsyms)
206 {
207 cword = *vrow++;
208 b = 0;
209 }
210 }
211
212 rrow += rulesetsize;
213 }
214
215 if (trace_flag)
216 print_fderives ();
217
218 XFREE (firsts);
219 }
220 \f
221
222 void
223 new_closure (int n)
224 {
225 itemset = XCALLOC (short, n);
226
227 rulesetsize = WORDSIZE (nrules + 1);
228 ruleset = XCALLOC (unsigned, rulesetsize);
229
230 set_fderives ();
231 }
232
233
234
235 void
236 closure (short *core, int n)
237 {
238 /* Index over CORE. */
239 int c;
240
241 /* Index over RULESET. */
242 int r;
243
244 /* A bit index over RULESET. */
245 int ruleno;
246
247 if (trace_flag)
248 {
249 fprintf (stderr, "Entering closure (items = {");
250 for (c = 0; c < n; ++c)
251 fprintf (stderr, " %d ", core[c]);
252 fprintf (stderr, "})\n");
253 }
254
255 if (n == 0)
256 {
257 for (r = 0; r < rulesetsize; ++r)
258 ruleset[r] = FDERIVES (start_symbol)[r];
259 }
260 else
261 {
262 for (r = 0; r < rulesetsize; ++r)
263 ruleset[r] = 0;
264
265 for (c = 0; c < n; ++c)
266 if (ISVAR (ritem[core[c]]))
267 for (r = 0; r < rulesetsize; ++r)
268 ruleset[r] |= FDERIVES (ritem[core[c]])[r];
269 }
270
271 itemsetsize = 0;
272 c = 0;
273 for (ruleno = 0; ruleno < rulesetsize * BITS_PER_WORD; ++ruleno)
274 if (BITISSET (ruleset, ruleno))
275 {
276 int itemno = rule_table[ruleno].rhs;
277 while (c < n && core[c] < itemno)
278 {
279 itemset[itemsetsize] = core[c];
280 itemsetsize++;
281 c++;
282 }
283 itemset[itemsetsize] = itemno;
284 itemsetsize++;
285 }
286
287 while (c < n)
288 {
289 itemset[itemsetsize] = core[c];
290 itemsetsize++;
291 c++;
292 }
293
294 if (trace_flag)
295 print_closure (n);
296 }
297
298
299 void
300 free_closure (void)
301 {
302 XFREE (itemset);
303 XFREE (ruleset);
304 XFREE (fderives + ntokens * rulesetsize);
305 }