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