]> git.saurik.com Git - bison.git/blame - src/closure.c
* po/sv.po: New.
[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;
fb908786
AD
31size_t itemsetsize;
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{
2fa6973e 238 int ruleno;
2fa6973e 239 short *csp;
d0fb370f 240
d0fb370f 241 int itemno;
7bec0760 242 int i;
d0fb370f 243
c87d4863
AD
244 if (trace_flag)
245 {
c87d4863
AD
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
d0fb370f
RS
252 if (n == 0)
253 {
7bec0760
AD
254 for (i = 0; i < rulesetsize; ++i)
255 ruleset[i] = FDERIVES (start_symbol)[i];
d0fb370f
RS
256 }
257 else
258 {
7bec0760
AD
259 for (i = 0; i < rulesetsize; ++i)
260 ruleset[i] = 0;
d0fb370f 261
7bec0760 262 for (i = 0; i < n; ++i)
d0fb370f 263 {
7bec0760 264 int symbol = ritem[core[i]];
2fa6973e 265 if (ISVAR (symbol))
d0fb370f 266 {
7bec0760
AD
267 int j;
268 for (j = 0; j < rulesetsize; ++j)
269 ruleset[j] |= FDERIVES (symbol)[j];
d0fb370f
RS
270 }
271 }
272 }
273
274 ruleno = 0;
fb908786 275 itemsetsize = 0;
d0fb370f 276 csp = core;
fb908786 277 for (i = 0; i < rulesetsize; ++i)
d0fb370f 278 {
7bec0760 279 int word = ruleset[i];
d0fb370f
RS
280 if (word == 0)
281 {
282 ruleno += BITS_PER_WORD;
283 }
284 else
285 {
2fa6973e 286 int b;
d0fb370f
RS
287
288 for (b = 0; b < BITS_PER_WORD; b++)
289 {
290 if (word & (1 << b))
291 {
b2ed6e58 292 itemno = rule_table[ruleno].rhs;
fb908786
AD
293 while (csp < (core + n) && *csp < itemno)
294 itemset[itemsetsize++] = *csp++;
295 itemset[itemsetsize++] = itemno;
d0fb370f
RS
296 }
297
298 ruleno++;
299 }
300 }
301 }
302
7bec0760 303 while (csp < (core + n))
fb908786 304 itemset[itemsetsize++] = *csp++;
d0fb370f 305
9bfe901c
AD
306 if (trace_flag)
307 print_closure (n);
d0fb370f
RS
308}
309
310
311void
2fa6973e 312free_closure (void)
d0fb370f 313{
d7913476
AD
314 XFREE (itemset);
315 XFREE (ruleset);
316 XFREE (fderives + ntokens * rulesetsize);
d0fb370f 317}