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