]> git.saurik.com Git - bison.git/blame - src/closure.c
* src/output.c (output_obstack): Be static and rename as...
[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
b2872512 29/* NITEMSET is the size of the array ITEMSET. */
d0fb370f 30short *itemset;
b2872512 31int nitemset;
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
03ec521c
AD
39/* Retrieve the FDERIVES/FIRSTS sets of the nonterminals numbered Var. */
40#define FDERIVES(Var) (fderives + ((Var) - ntokens) * rulesetsize)
41#define FIRSTS(Var) (firsts + ((Var) - ntokens) * varsetsize)
c87d4863 42
d0fb370f
RS
43/* number of words required to hold a bit for each rule */
44static int rulesetsize;
45
46/* number of words required to hold a bit for each variable */
47static int varsetsize;
2fa6973e 48\f
d0fb370f 49
2fa6973e
AD
50/*-----------------.
51| Debugging code. |
52`-----------------*/
d0fb370f 53
2fa6973e 54static void
23cbcc6c 55print_closure (const char *title, short *array, size_t size)
2fa6973e 56{
23cbcc6c
AD
57 size_t i;
58 fprintf (stderr, "Closure: %s\n", title);
59 for (i = 0; i < size; ++i)
60 {
61 short *rp;
62 fprintf (stderr, " %2d: .", array[i]);
63 for (rp = &ritem[array[i]]; *rp > 0; ++rp)
64 fprintf (stderr, " %s", tags[*rp]);
65 fprintf (stderr, " (rule %d)\n", -*rp);
66 }
67 fputs ("\n\n", stderr);
2fa6973e
AD
68}
69
70
71static void
72print_firsts (void)
d0fb370f 73{
84182270 74 int i, j;
2fa6973e 75
c87d4863 76 fprintf (stderr, "FIRSTS\n");
2fa6973e
AD
77 for (i = ntokens; i < nsyms; i++)
78 {
c87d4863 79 fprintf (stderr, "\t%s firsts\n", tags[i]);
2fa6973e 80 for (j = 0; j < nvars; j++)
03ec521c 81 if (BITISSET (FIRSTS (i), j))
c87d4863 82 fprintf (stderr, "\t\t%d (%s)\n", j + ntokens, tags[j + ntokens]);
2fa6973e 83 }
c87d4863 84 fprintf (stderr, "\n\n");
d0fb370f
RS
85}
86
87
2fa6973e
AD
88static void
89print_fderives (void)
90{
91 int i;
92 int j;
2fa6973e 93
c87d4863 94 fprintf (stderr, "FDERIVES\n");
2fa6973e
AD
95
96 for (i = ntokens; i < nsyms; i++)
97 {
c87d4863 98 fprintf (stderr, "\t%s derives\n", tags[i]);
2fa6973e 99 for (j = 0; j <= nrules; j++)
84182270 100 if (BITISSET (FDERIVES (i), j))
720e5c1b
AD
101 {
102 short *rhsp;
103 fprintf (stderr, "\t\t%d:", j);
104 for (rhsp = ritem + rule_table[j].rhs; *rhsp > 0; ++rhsp)
105 fprintf (stderr, " %s", tags[*rhsp]);
106 fputc ('\n', stderr);
107 }
2fa6973e 108 }
c87d4863 109 fprintf (stderr, "\n\n");
2fa6973e 110}
2fa6973e
AD
111\f
112/*-------------------------------------------------------------------.
113| Set FIRSTS to be an NVARS by NVARS bit matrix indicating which |
114| items can represent the beginning of the input corresponding to |
115| which other items. |
116| |
117| For example, if some rule expands symbol 5 into the sequence of |
118| symbols 8 3 20, the symbol 8 can be the beginning of the data for |
119| symbol 5, so the bit [8 - ntokens, 5 - ntokens] in firsts is set. |
120`-------------------------------------------------------------------*/
121
122static void
123set_firsts (void)
124{
3f6f053c 125 int i, j;
2fa6973e 126
03ec521c 127 varsetsize = WORDSIZE (nvars);
2fa6973e 128
03ec521c 129 firsts = XCALLOC (unsigned, nvars * varsetsize);
2fa6973e 130
2fa6973e 131 for (i = ntokens; i < nsyms; i++)
3f6f053c
AD
132 for (j = 0; derives[i][j] >= 0; ++j)
133 {
134 int symbol = ritem[rule_table[derives[i][j]].rhs];
135 if (ISVAR (symbol))
03ec521c 136 SETBIT (FIRSTS (i), symbol - ntokens);
3f6f053c 137 }
2fa6973e
AD
138
139 RTC (firsts, nvars);
140
9bfe901c
AD
141 if (trace_flag)
142 print_firsts ();
2fa6973e
AD
143}
144
145/*-------------------------------------------------------------------.
146| Set FDERIVES to an NVARS by NRULES matrix of bits indicating which |
147| rules can help derive the beginning of the data for each |
148| nonterminal. |
149| |
150| For example, if symbol 5 can be derived as the sequence of symbols |
151| 8 3 20, and one of the rules for deriving symbol 8 is rule 4, then |
152| the [5 - NTOKENS, 4] bit in FDERIVES is set. |
153`-------------------------------------------------------------------*/
d0fb370f 154
4a120d45 155static void
d2729d44 156set_fderives (void)
d0fb370f 157{
1cbcf2e7 158 int i, j, k;
d0fb370f 159
03ec521c 160 fderives = XCALLOC (unsigned, nvars * rulesetsize);
d0fb370f 161
2fa6973e 162 set_firsts ();
d0fb370f 163
1cbcf2e7
AD
164 for (i = ntokens; i < nsyms; ++i)
165 for (j = ntokens; j < nsyms; ++j)
03ec521c 166 if (BITISSET (FIRSTS (i), j - ntokens))
1cbcf2e7
AD
167 for (k = 0; derives[j][k] > 0; ++k)
168 SETBIT (FDERIVES (i), derives[j][k]);
d0fb370f 169
9bfe901c
AD
170 if (trace_flag)
171 print_fderives ();
d0fb370f 172
d7913476 173 XFREE (firsts);
d0fb370f 174}
2fa6973e 175\f
d0fb370f 176
2fa6973e
AD
177void
178new_closure (int n)
d0fb370f 179{
d7913476 180 itemset = XCALLOC (short, n);
d0fb370f 181
2fa6973e 182 rulesetsize = WORDSIZE (nrules + 1);
d7913476 183 ruleset = XCALLOC (unsigned, rulesetsize);
d0fb370f 184
2fa6973e 185 set_fderives ();
d0fb370f
RS
186}
187
188
2fa6973e 189
d0fb370f 190void
d2729d44 191closure (short *core, int n)
d0fb370f 192{
576890b7
AD
193 /* Index over CORE. */
194 int c;
195
196 /* Index over RULESET. */
197 int r;
d0fb370f 198
d2b04478 199 /* A bit index over RULESET. */
4b35e1c1 200 int ruleno;
d0fb370f 201
c87d4863 202 if (trace_flag)
23cbcc6c 203 print_closure ("input", core, n);
c87d4863 204
d0fb370f
RS
205 if (n == 0)
206 {
576890b7
AD
207 for (r = 0; r < rulesetsize; ++r)
208 ruleset[r] = FDERIVES (start_symbol)[r];
d0fb370f
RS
209 }
210 else
211 {
576890b7
AD
212 for (r = 0; r < rulesetsize; ++r)
213 ruleset[r] = 0;
d0fb370f 214
576890b7
AD
215 for (c = 0; c < n; ++c)
216 if (ISVAR (ritem[core[c]]))
217 for (r = 0; r < rulesetsize; ++r)
218 ruleset[r] |= FDERIVES (ritem[core[c]])[r];
d0fb370f
RS
219 }
220
b2872512 221 nitemset = 0;
576890b7 222 c = 0;
cb581495 223 for (ruleno = 0; ruleno < nrules + 1; ++ruleno)
4b35e1c1
AD
224 if (BITISSET (ruleset, ruleno))
225 {
226 int itemno = rule_table[ruleno].rhs;
227 while (c < n && core[c] < itemno)
228 {
b2872512
AD
229 itemset[nitemset] = core[c];
230 nitemset++;
4b35e1c1
AD
231 c++;
232 }
b2872512
AD
233 itemset[nitemset] = itemno;
234 nitemset++;
4b35e1c1 235 }
d0fb370f 236
576890b7
AD
237 while (c < n)
238 {
b2872512
AD
239 itemset[nitemset] = core[c];
240 nitemset++;
576890b7
AD
241 c++;
242 }
d0fb370f 243
9bfe901c 244 if (trace_flag)
b2872512 245 print_closure ("output", itemset, nitemset);
d0fb370f
RS
246}
247
248
249void
2fa6973e 250free_closure (void)
d0fb370f 251{
d7913476
AD
252 XFREE (itemset);
253 XFREE (ruleset);
03ec521c 254 XFREE (fderives);
d0fb370f 255}