]> git.saurik.com Git - bison.git/blame - src/closure.c
Don't define PACKAGE here, since config.h defines it.
[bison.git] / src / closure.c
CommitLineData
d0fb370f
RS
1/* Subroutines for bison
2 Copyright (C) 1984, 1989 Free Software Foundation, Inc.
3
4This file is part of Bison, the GNU Compiler Compiler.
5
6Bison is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11Bison is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with Bison; see the file COPYING. If not, write to
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21/* subroutines of file LR0.c.
22
23Entry points:
24
25 closure (items, n)
26
27Given a vector of item numbers items, of length n,
28set up ruleset and itemset to indicate what rules could be run
29and which items could be accepted when those items are the active ones.
30
31ruleset contains a bit for each rule. closure sets the bits
32for all rules which could potentially describe the next input to be read.
33
34itemset is a vector of item numbers; itemsetend points to just beyond the end
35 of the part of it that is significant.
36closure places there the indices of all items which represent units of
37input that could arrive next.
38
39 initialize_closure (n)
40
41Allocates the itemset and ruleset vectors,
42and precomputes useful data so that closure can be called.
43n is the number of elements to allocate for itemset.
44
45 finalize_closure ()
46
47Frees itemset, ruleset and internal data.
48
49*/
50
51#include <stdio.h>
52#include "system.h"
53#include "machine.h"
7612000c 54#include "alloc.h"
d0fb370f
RS
55#include "gram.h"
56
57
58extern short **derives;
59extern char **tags;
60
d2729d44
JT
61void initialize_closure PARAMS((int));
62void set_fderives PARAMS((void));
63void set_firsts PARAMS((void));
64void closure PARAMS((short *, int));
65void finalize_closure PARAMS((void));
d0fb370f 66
d2729d44 67extern void RTC PARAMS((unsigned *, int));
d0fb370f
RS
68
69short *itemset;
70short *itemsetend;
71static unsigned *ruleset;
72
73/* internal data. See comments before set_fderives and set_firsts. */
74static unsigned *fderives;
75static unsigned *firsts;
76
77/* number of words required to hold a bit for each rule */
78static int rulesetsize;
79
80/* number of words required to hold a bit for each variable */
81static int varsetsize;
82
83
84void
d2729d44 85initialize_closure (int n)
d0fb370f
RS
86{
87 itemset = NEW2(n, short);
88
89 rulesetsize = WORDSIZE(nrules + 1);
90 ruleset = NEW2(rulesetsize, unsigned);
91
92 set_fderives();
93}
94
95
96
97/* set fderives to an nvars by nrules matrix of bits
98 indicating which rules can help derive the beginning of the data
99 for each nonterminal. For example, if symbol 5 can be derived as
100 the sequence of symbols 8 3 20, and one of the rules for deriving
101 symbol 8 is rule 4, then the [5 - ntokens, 4] bit in fderives is set. */
102void
d2729d44 103set_fderives (void)
d0fb370f
RS
104{
105 register unsigned *rrow;
106 register unsigned *vrow;
107 register int j;
108 register unsigned cword;
109 register short *rp;
110 register int b;
111
112 int ruleno;
113 int i;
114
115 fderives = NEW2(nvars * rulesetsize, unsigned) - ntokens * rulesetsize;
116
117 set_firsts();
118
119 rrow = fderives + ntokens * rulesetsize;
120
121 for (i = ntokens; i < nsyms; i++)
122 {
123 vrow = firsts + ((i - ntokens) * varsetsize);
124 cword = *vrow++;
125 b = 0;
126 for (j = ntokens; j < nsyms; j++)
127 {
128 if (cword & (1 << b))
129 {
130 rp = derives[j];
131 while ((ruleno = *rp++) > 0)
132 {
133 SETBIT(rrow, ruleno);
134 }
135 }
136
137 b++;
138 if (b >= BITS_PER_WORD && j + 1 < nsyms)
139 {
140 cword = *vrow++;
141 b = 0;
142 }
143 }
144
145 rrow += rulesetsize;
146 }
147
148#ifdef DEBUG
149 print_fderives();
150#endif
151
152 FREE(firsts);
153}
154
155
156
157/* set firsts to be an nvars by nvars bit matrix indicating which items
158 can represent the beginning of the input corresponding to which other items.
159 For example, if some rule expands symbol 5 into the sequence of symbols 8 3 20,
160 the symbol 8 can be the beginning of the data for symbol 5,
161 so the bit [8 - ntokens, 5 - ntokens] in firsts is set. */
162void
d2729d44 163set_firsts (void)
d0fb370f
RS
164{
165 register unsigned *row;
166/* register int done; JF unused */
167 register int symbol;
168 register short *sp;
169 register int rowsize;
170
171 int i;
172
173 varsetsize = rowsize = WORDSIZE(nvars);
174
175 firsts = NEW2(nvars * rowsize, unsigned);
176
177 row = firsts;
178 for (i = ntokens; i < nsyms; i++)
179 {
180 sp = derives[i];
181 while (*sp >= 0)
182 {
183 symbol = ritem[rrhs[*sp++]];
184 if (ISVAR(symbol))
185 {
186 symbol -= ntokens;
187 SETBIT(row, symbol);
188 }
189 }
190
191 row += rowsize;
192 }
193
194 RTC(firsts, nvars);
195
196#ifdef DEBUG
197 print_firsts();
198#endif
199}
200
201
202void
d2729d44 203closure (short *core, int n)
d0fb370f
RS
204{
205 register int ruleno;
206 register unsigned word;
207 register short *csp;
208 register unsigned *dsp;
209 register unsigned *rsp;
210
211 short *csend;
212 unsigned *rsend;
213 int symbol;
214 int itemno;
215
216 rsp = ruleset;
217 rsend = ruleset + rulesetsize;
218 csend = core + n;
219
220 if (n == 0)
221 {
222 dsp = fderives + start_symbol * rulesetsize;
223 while (rsp < rsend)
224 *rsp++ = *dsp++;
225 }
226 else
227 {
228 while (rsp < rsend)
229 *rsp++ = 0;
230
231 csp = core;
232 while (csp < csend)
233 {
234 symbol = ritem[*csp++];
235 if (ISVAR(symbol))
236 {
237 dsp = fderives + symbol * rulesetsize;
238 rsp = ruleset;
239 while (rsp < rsend)
240 *rsp++ |= *dsp++;
241 }
242 }
243 }
244
245 ruleno = 0;
246 itemsetend = itemset;
247 csp = core;
248 rsp = ruleset;
249 while (rsp < rsend)
250 {
251 word = *rsp++;
252 if (word == 0)
253 {
254 ruleno += BITS_PER_WORD;
255 }
256 else
257 {
258 register int b;
259
260 for (b = 0; b < BITS_PER_WORD; b++)
261 {
262 if (word & (1 << b))
263 {
264 itemno = rrhs[ruleno];
265 while (csp < csend && *csp < itemno)
266 *itemsetend++ = *csp++;
267 *itemsetend++ = itemno;
268 }
269
270 ruleno++;
271 }
272 }
273 }
274
275 while (csp < csend)
276 *itemsetend++ = *csp++;
277
278#ifdef DEBUG
279 print_closure(n);
280#endif
281}
282
283
284void
d2729d44 285finalize_closure (void)
d0fb370f
RS
286{
287 FREE(itemset);
288 FREE(ruleset);
289 FREE(fderives + ntokens * rulesetsize);
290}
291
292
293
294#ifdef DEBUG
295
296print_closure(n)
297int n;
298{
299 register short *isp;
300
301 printf("\n\nn = %d\n\n", n);
302 for (isp = itemset; isp < itemsetend; isp++)
303 printf(" %d\n", *isp);
304}
305
306
d2729d44
JT
307void
308print_firsts (void)
d0fb370f
RS
309{
310 register int i;
311 register int j;
312 register unsigned *rowp;
313
a083fbbf 314 printf(_("\n\n\nFIRSTS\n\n"));
d0fb370f
RS
315
316 for (i = ntokens; i < nsyms; i++)
317 {
a083fbbf 318 printf(_("\n\n%s firsts\n\n"), tags[i]);
d0fb370f
RS
319
320 rowp = firsts + ((i - ntokens) * varsetsize);
321
322 for (j = 0; j < nvars; j++)
323 if (BITISSET (rowp, j))
324 printf(" %s\n", tags[j + ntokens]);
325 }
326}
327
328
d2729d44
JT
329void
330print_fderives (void)
d0fb370f
RS
331{
332 register int i;
333 register int j;
334 register unsigned *rp;
335
a083fbbf 336 printf(_("\n\n\nFDERIVES\n"));
d0fb370f
RS
337
338 for (i = ntokens; i < nsyms; i++)
339 {
a083fbbf 340 printf(_("\n\n%s derives\n\n"), tags[i]);
d0fb370f
RS
341 rp = fderives + i * rulesetsize;
342
343 for (j = 0; j <= nrules; j++)
344 if (BITISSET (rp, j))
345 printf(" %d\n", j);
346 }
347
348 fflush(stdout);
349}
350
351#endif