]> git.saurik.com Git - bison.git/blame_incremental - src/closure.c
* src/bison.simple (YYSTACK_REALLOC): Fix typo that caused us to
[bison.git] / src / closure.c
... / ...
CommitLineData
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. */
30short *itemset;
31int itemsetsize;
32
33static unsigned *ruleset;
34
35/* internal data. See comments before set_fderives and set_firsts. */
36static unsigned *fderives;
37static 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 */
43static int rulesetsize;
44
45/* number of words required to hold a bit for each variable */
46static int varsetsize;
47\f
48
49/*-----------------.
50| Debugging code. |
51`-----------------*/
52
53static void
54print_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
64static void
65print_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
87static void
88print_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
124static void
125set_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
171static void
172set_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
222void
223new_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
235void
236closure (short *core, int n)
237{
238 int ruleno;
239 short *csp;
240
241 int itemno;
242 int i;
243
244 if (trace_flag)
245 {
246 fprintf (stderr, "Entering closure (items = {");
247 for (i = 0; i < n; ++i)
248 fprintf (stderr, " %d ", core[i]);
249 fprintf (stderr, "}, nitems = %d)\n", n);
250 }
251
252 if (n == 0)
253 {
254 for (i = 0; i < rulesetsize; ++i)
255 ruleset[i] = FDERIVES (start_symbol)[i];
256 }
257 else
258 {
259 for (i = 0; i < rulesetsize; ++i)
260 ruleset[i] = 0;
261
262 for (i = 0; i < n; ++i)
263 {
264 int symbol = ritem[core[i]];
265 if (ISVAR (symbol))
266 {
267 int j;
268 for (j = 0; j < rulesetsize; ++j)
269 ruleset[j] |= FDERIVES (symbol)[j];
270 }
271 }
272 }
273
274 ruleno = 0;
275 itemsetsize = 0;
276 csp = core;
277 for (i = 0; i < rulesetsize; ++i)
278 {
279 int word = ruleset[i];
280 if (word == 0)
281 {
282 ruleno += BITS_PER_WORD;
283 }
284 else
285 {
286 int b;
287
288 for (b = 0; b < BITS_PER_WORD; b++)
289 {
290 if (word & (1 << b))
291 {
292 itemno = rule_table[ruleno].rhs;
293 while (csp < (core + n) && *csp < itemno)
294 itemset[itemsetsize++] = *csp++;
295 itemset[itemsetsize++] = itemno;
296 }
297
298 ruleno++;
299 }
300 }
301 }
302
303 while (csp < (core + n))
304 itemset[itemsetsize++] = *csp++;
305
306 if (trace_flag)
307 print_closure (n);
308}
309
310
311void
312free_closure (void)
313{
314 XFREE (itemset);
315 XFREE (ruleset);
316 XFREE (fderives + ntokens * rulesetsize);
317}