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