]> git.saurik.com Git - bison.git/blame - src/closure.c
Add src/state.c to the repo.
[bison.git] / src / closure.c
CommitLineData
d0fb370f 1/* Subroutines for bison
2c5f66ed 2 Copyright 1984, 1989, 2000, 2001 Free Software Foundation, Inc.
d0fb370f 3
8dc26b76 4 This file is part of Bison, the GNU Compiler Compiler.
d0fb370f 5
8dc26b76
AD
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.
d0fb370f 10
8dc26b76
AD
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.
d0fb370f 15
8dc26b76
AD
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. */
d0fb370f 20
d0fb370f 21#include "system.h"
9bfe901c 22#include "getargs.h"
d0fb370f 23#include "gram.h"
2c5f66ed 24#include "reader.h"
2fa6973e 25#include "closure.h"
340ef489 26#include "derives.h"
d7913476 27#include "warshall.h"
d0fb370f 28
fb908786 29/* ITEMSETSIZE is the size of the array ITEMSET. */
d0fb370f 30short *itemset;
6255b435 31int itemsetsize;
fb908786 32
d0fb370f
RS
33static unsigned *ruleset;
34
35/* internal data. See comments before set_fderives and set_firsts. */
36static unsigned *fderives;
37static unsigned *firsts;
38
c87d4863
AD
39#define FDERIVES(Symbol) (fderives + (Symbol) * rulesetsize)
40#define FIRSTS(Symbol) (firsts + (Symbol) * varsetsize)
41
d0fb370f
RS
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;
2fa6973e 47\f
d0fb370f 48
2fa6973e
AD
49/*-----------------.
50| Debugging code. |
51`-----------------*/
d0fb370f 52
2fa6973e
AD
53static void
54print_closure (int n)
55{
fb908786 56 int i;
c87d4863 57 fprintf (stderr, "n = %d\n", n);
fb908786
AD
58 for (i = 0; i < itemsetsize; ++i)
59 fprintf (stderr, " %d\n", itemset[i]);
c87d4863 60 fprintf (stderr, "\n\n");
2fa6973e
AD
61}
62
63
64static void
65print_firsts (void)
d0fb370f 66{
2fa6973e
AD
67 int i;
68 int j;
69 unsigned *rowp;
70
c87d4863 71 fprintf (stderr, "FIRSTS\n");
2fa6973e
AD
72
73 for (i = ntokens; i < nsyms; i++)
74 {
c87d4863 75 fprintf (stderr, "\t%s firsts\n", tags[i]);
d0fb370f 76
c87d4863 77 rowp = FIRSTS (i - ntokens);
d0fb370f 78
2fa6973e
AD
79 for (j = 0; j < nvars; j++)
80 if (BITISSET (rowp, j))
c87d4863 81 fprintf (stderr, "\t\t%d (%s)\n", j + ntokens, tags[j + ntokens]);
2fa6973e 82 }
c87d4863 83 fprintf (stderr, "\n\n");
d0fb370f
RS
84}
85
86
2fa6973e
AD
87static void
88print_fderives (void)
89{
90 int i;
91 int j;
92 unsigned *rp;
93
c87d4863 94 fprintf (stderr, "FDERIVES\n");
2fa6973e
AD
95
96 for (i = ntokens; i < nsyms; i++)
97 {
c87d4863
AD
98 fprintf (stderr, "\t%s derives\n", tags[i]);
99 rp = FDERIVES (i);
2fa6973e
AD
100
101 for (j = 0; j <= nrules; j++)
102 if (BITISSET (rp, j))
720e5c1b
AD
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 }
2fa6973e 110 }
c87d4863 111 fprintf (stderr, "\n\n");
2fa6973e 112}
2fa6973e
AD
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
d7913476 136 firsts = XCALLOC (unsigned, nvars * rowsize);
2fa6973e
AD
137
138 row = firsts;
139 for (i = ntokens; i < nsyms; i++)
140 {
141 sp = derives[i];
142 while (*sp >= 0)
143 {
b2ed6e58 144 symbol = ritem[rule_table[*sp++].rhs];
2fa6973e
AD
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
9bfe901c
AD
157 if (trace_flag)
158 print_firsts ();
2fa6973e
AD
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`-------------------------------------------------------------------*/
d0fb370f 170
4a120d45 171static void
d2729d44 172set_fderives (void)
d0fb370f 173{
2fa6973e
AD
174 unsigned *rrow;
175 unsigned *vrow;
176 int j;
177 unsigned cword;
178 short *rp;
179 int b;
d0fb370f
RS
180
181 int ruleno;
182 int i;
183
d7913476 184 fderives = XCALLOC (unsigned, nvars * rulesetsize) - ntokens * rulesetsize;
d0fb370f 185
2fa6973e 186 set_firsts ();
d0fb370f 187
c87d4863 188 rrow = FDERIVES (ntokens);
d0fb370f
RS
189
190 for (i = ntokens; i < nsyms; i++)
191 {
c87d4863 192 vrow = FIRSTS (i - ntokens);
d0fb370f
RS
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)
c87d4863 201 SETBIT (rrow, ruleno);
d0fb370f
RS
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
9bfe901c
AD
215 if (trace_flag)
216 print_fderives ();
d0fb370f 217
d7913476 218 XFREE (firsts);
d0fb370f 219}
2fa6973e 220\f
d0fb370f 221
2fa6973e
AD
222void
223new_closure (int n)
d0fb370f 224{
d7913476 225 itemset = XCALLOC (short, n);
d0fb370f 226
2fa6973e 227 rulesetsize = WORDSIZE (nrules + 1);
d7913476 228 ruleset = XCALLOC (unsigned, rulesetsize);
d0fb370f 229
2fa6973e 230 set_fderives ();
d0fb370f
RS
231}
232
233
2fa6973e 234
d0fb370f 235void
d2729d44 236closure (short *core, int n)
d0fb370f 237{
576890b7
AD
238 /* Index over CORE. */
239 int c;
240
241 /* Index over RULESET. */
242 int r;
d0fb370f 243
d2b04478 244 /* A bit index over RULESET. */
4b35e1c1 245 int ruleno;
d0fb370f 246
c87d4863
AD
247 if (trace_flag)
248 {
c87d4863 249 fprintf (stderr, "Entering closure (items = {");
576890b7
AD
250 for (c = 0; c < n; ++c)
251 fprintf (stderr, " %d ", core[c]);
252 fprintf (stderr, "})\n");
c87d4863
AD
253 }
254
d0fb370f
RS
255 if (n == 0)
256 {
576890b7
AD
257 for (r = 0; r < rulesetsize; ++r)
258 ruleset[r] = FDERIVES (start_symbol)[r];
d0fb370f
RS
259 }
260 else
261 {
576890b7
AD
262 for (r = 0; r < rulesetsize; ++r)
263 ruleset[r] = 0;
d0fb370f 264
576890b7
AD
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];
d0fb370f
RS
269 }
270
fb908786 271 itemsetsize = 0;
576890b7 272 c = 0;
4b35e1c1
AD
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 }
d0fb370f 286
576890b7
AD
287 while (c < n)
288 {
289 itemset[itemsetsize] = core[c];
290 itemsetsize++;
291 c++;
292 }
d0fb370f 293
9bfe901c
AD
294 if (trace_flag)
295 print_closure (n);
d0fb370f
RS
296}
297
298
299void
2fa6973e 300free_closure (void)
d0fb370f 301{
d7913476
AD
302 XFREE (itemset);
303 XFREE (ruleset);
304 XFREE (fderives + ntokens * rulesetsize);
d0fb370f 305}