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