]> git.saurik.com Git - bison.git/blob - src/closure.c
a90b867047119b96c0855f3b0ed9c6ef0ebe11d5
[bison.git] / src / closure.c
1 /* Subroutines for bison
2 Copyright 1984, 1989, 2000, 2001 Free Software Foundation, Inc.
3
4 This file is part of Bison, the GNU Compiler Compiler.
5
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.
10
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.
15
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. */
20
21 #include "system.h"
22 #include "getargs.h"
23 #include "symtab.h"
24 #include "gram.h"
25 #include "reader.h"
26 #include "closure.h"
27 #include "derives.h"
28 #include "warshall.h"
29 #include "bitset.h"
30
31 /* NITEMSET is the size of the array ITEMSET. */
32 short *itemset;
33 int nitemset;
34
35 static unsigned *ruleset;
36
37 /* internal data. See comments before set_fderives and set_firsts. */
38 static bitset *fderives;
39 static unsigned int *firsts;
40
41 /* Retrieve the FDERIVES/FIRSTS sets of the nonterminals numbered Var. */
42 #define FDERIVES(Var) fderives[(Var) - ntokens]
43 #define FIRSTS(Var) (firsts + ((Var) - ntokens) * varsetsize)
44
45 /* number of words required to hold a bit for each rule */
46 static int rulesetsize;
47
48 /* number of words required to hold a bit for each variable */
49 static int varsetsize;
50 \f
51
52 /*-----------------.
53 | Debugging code. |
54 `-----------------*/
55
56 static void
57 print_closure (const char *title, short *array, size_t size)
58 {
59 size_t i;
60 fprintf (stderr, "Closure: %s\n", title);
61 for (i = 0; i < size; ++i)
62 {
63 short *rp;
64 fprintf (stderr, " %2d: .", array[i]);
65 for (rp = &ritem[array[i]]; *rp >= 0; ++rp)
66 fprintf (stderr, " %s", symbols[*rp]->tag);
67 fprintf (stderr, " (rule %d)\n", -*rp - 1);
68 }
69 fputs ("\n\n", stderr);
70 }
71
72
73 static void
74 print_firsts (void)
75 {
76 int i, j;
77
78 fprintf (stderr, "FIRSTS\n");
79 for (i = ntokens; i < nsyms; i++)
80 {
81 fprintf (stderr, "\t%s firsts\n", symbols[i]->tag);
82 for (j = 0; j < nvars; j++)
83 if (BITISSET (FIRSTS (i), j))
84 fprintf (stderr, "\t\t%d (%s)\n",
85 j + ntokens, symbols[j + ntokens]->tag);
86 }
87 fprintf (stderr, "\n\n");
88 }
89
90
91 static void
92 print_fderives (void)
93 {
94 int i;
95 int j;
96
97 fprintf (stderr, "FDERIVES\n");
98
99 for (i = ntokens; i < nsyms; i++)
100 {
101 fprintf (stderr, "\t%s derives\n", symbols[i]->tag);
102 for (j = 0; j <= nrules; j++)
103 if (bitset_test (FDERIVES (i), j))
104 {
105 short *rhsp;
106 fprintf (stderr, "\t\t%d:", j - 1);
107 for (rhsp = &ritem[rules[j].rhs]; *rhsp >= 0; ++rhsp)
108 fprintf (stderr, " %s", symbols[*rhsp]->tag);
109 fputc ('\n', stderr);
110 }
111 }
112 fprintf (stderr, "\n\n");
113 }
114 \f
115 /*-------------------------------------------------------------------.
116 | Set FIRSTS to be an NVARS by NVARS bit matrix indicating which |
117 | items can represent the beginning of the input corresponding to |
118 | which other items. |
119 | |
120 | For example, if some rule expands symbol 5 into the sequence of |
121 | symbols 8 3 20, the symbol 8 can be the beginning of the data for |
122 | symbol 5, so the bit [8 - ntokens, 5 - ntokens] in firsts is set. |
123 `-------------------------------------------------------------------*/
124
125 static void
126 set_firsts (void)
127 {
128 int i, j;
129
130 varsetsize = WORDSIZE (nvars);
131
132 firsts = XCALLOC (unsigned, nvars * varsetsize);
133
134 for (i = ntokens; i < nsyms; i++)
135 for (j = 0; derives[i][j] >= 0; ++j)
136 {
137 int symbol = ritem[rules[derives[i][j]].rhs];
138 if (ISVAR (symbol))
139 SETBIT (FIRSTS (i), symbol - ntokens);
140 }
141
142 RTC (firsts, nvars);
143
144 if (trace_flag)
145 print_firsts ();
146 }
147
148 /*-------------------------------------------------------------------.
149 | Set FDERIVES to an NVARS by NRULES matrix of bits indicating which |
150 | rules can help derive the beginning of the data for each |
151 | nonterminal. |
152 | |
153 | For example, if symbol 5 can be derived as the sequence of symbols |
154 | 8 3 20, and one of the rules for deriving symbol 8 is rule 4, then |
155 | the [5 - NTOKENS, 4] bit in FDERIVES is set. |
156 `-------------------------------------------------------------------*/
157
158 static void
159 set_fderives (void)
160 {
161 int i, j, k;
162
163 fderives = XCALLOC (bitset, nvars);
164 bitset_stats_init ();
165 for (i = 0 ; i < nvars; ++i)
166 {
167 fderives[i] = bitset_create (nrules + 1, BITSET_FIXED);
168 bitset_zero (fderives[i]);
169 }
170
171 set_firsts ();
172
173 for (i = ntokens; i < nsyms; ++i)
174 for (j = ntokens; j < nsyms; ++j)
175 if (BITISSET (FIRSTS (i), j - ntokens))
176 for (k = 0; derives[j][k] > 0; ++k)
177 bitset_set (FDERIVES (i), derives[j][k]);
178
179 if (trace_flag)
180 print_fderives ();
181
182 XFREE (firsts);
183 }
184 \f
185
186 void
187 new_closure (int n)
188 {
189 itemset = XCALLOC (short, n);
190
191 rulesetsize = WORDSIZE (nrules + 1);
192 ruleset = XCALLOC (unsigned, rulesetsize);
193
194 set_fderives ();
195 }
196
197
198
199 void
200 closure (short *core, int n)
201 {
202 /* Index over CORE. */
203 int c;
204
205 /* An index over RULESET. */
206 int r;
207
208 /* A bit index over RULESET. */
209 int ruleno;
210
211 if (trace_flag)
212 print_closure ("input", core, n);
213
214 if (n == 0)
215 {
216 for (ruleno = 0; ruleno < nrules + 1; ++ruleno)
217 if (bitset_test (FDERIVES (start_symbol), ruleno))
218 SETBIT (ruleset, ruleno);
219 else
220 RESETBIT (ruleset, ruleno);
221 }
222 else
223 {
224 for (r = 0; r < rulesetsize; ++r)
225 ruleset[r] = 0;
226
227 for (c = 0; c < n; ++c)
228 if (ISVAR (ritem[core[c]]))
229 for (ruleno = 0; ruleno < nrules + 1; ++ruleno)
230 if (bitset_test (FDERIVES (ritem[core[c]]), ruleno))
231 SETBIT (ruleset, ruleno);
232 }
233
234 nitemset = 0;
235 c = 0;
236 for (ruleno = 0; ruleno < nrules + 1; ++ruleno)
237 if (BITISSET (ruleset, ruleno))
238 {
239 int itemno = rules[ruleno].rhs;
240 while (c < n && core[c] < itemno)
241 {
242 itemset[nitemset] = core[c];
243 nitemset++;
244 c++;
245 }
246 itemset[nitemset] = itemno;
247 nitemset++;
248 }
249
250 while (c < n)
251 {
252 itemset[nitemset] = core[c];
253 nitemset++;
254 c++;
255 }
256
257 if (trace_flag)
258 print_closure ("output", itemset, nitemset);
259 }
260
261
262 void
263 free_closure (void)
264 {
265 int i;
266 XFREE (itemset);
267 XFREE (ruleset);
268
269 for (i = 0 ; i < nvars; ++i)
270 bitset_free (fderives[i]);
271 free (fderives);
272 }