* src/output.c (prepare_actions): Free `tally' and `width'.
[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 "quotearg.h"
23 #include "bitset.h"
24 #include "bitsetv.h"
25 #include "bitsetv-print.h"
26 #include "getargs.h"
27 #include "symtab.h"
28 #include "gram.h"
29 #include "reader.h"
30 #include "closure.h"
31 #include "derives.h"
32
33 /* NITEMSET is the size of the array ITEMSET. */
34 item_number_t *itemset;
35 int nritemset;
36
37 static bitset ruleset;
38
39 /* internal data. See comments before set_fderives and set_firsts. */
40 static bitsetv fderives = NULL;
41 static bitsetv firsts = NULL;
42
43 /* Retrieve the FDERIVES/FIRSTS sets of the nonterminals numbered Var. */
44 #define FDERIVES(Var) fderives[(Var) - ntokens]
45 #define FIRSTS(Var) firsts[(Var) - ntokens]
46 \f
47
48 /*-----------------.
49 | Debugging code. |
50 `-----------------*/
51
52 static void
53 print_closure (const char *title, item_number_t *array, size_t size)
54 {
55 size_t i;
56 fprintf (stderr, "Closure: %s\n", title);
57 for (i = 0; i < size; ++i)
58 {
59 item_number_t *rp;
60 fprintf (stderr, " %2d: .", array[i]);
61 for (rp = &ritem[array[i]]; *rp >= 0; ++rp)
62 fprintf (stderr, " %s", symbols[*rp]->tag);
63 fprintf (stderr, " (rule %d)\n", -*rp - 1);
64 }
65 fputs ("\n\n", stderr);
66 }
67
68
69 static void
70 print_firsts (void)
71 {
72 symbol_number_t i, j;
73
74 fprintf (stderr, "FIRSTS\n");
75 for (i = ntokens; i < nsyms; i++)
76 {
77 bitset_iterator iter;
78 fprintf (stderr, "\t%s firsts\n", symbols[i]->tag);
79 BITSET_FOR_EACH (iter, FIRSTS (i), j, 0)
80 {
81 fprintf (stderr, "\t\t%s\n",
82 symbols[j + ntokens]->tag);
83 }
84 }
85 fprintf (stderr, "\n\n");
86 }
87
88
89 static void
90 print_fderives (void)
91 {
92 int i;
93 rule_number_t r;
94
95 fprintf (stderr, "FDERIVES\n");
96 for (i = ntokens; i < nsyms; i++)
97 {
98 bitset_iterator iter;
99 fprintf (stderr, "\t%s derives\n", symbols[i]->tag);
100 BITSET_FOR_EACH (iter, FDERIVES (i), r, 0)
101 {
102 item_number_t *rhsp = NULL;
103 fprintf (stderr, "\t\t%d:", r - 1);
104 for (rhsp = rules[r].rhs; *rhsp >= 0; ++rhsp)
105 fprintf (stderr, " %s", symbols[*rhsp]->tag);
106 fputc ('\n', stderr);
107 }
108 }
109 fprintf (stderr, "\n\n");
110 }
111 \f
112 /*------------------------------------------------------------------.
113 | Set FIRSTS to be an NVARS array of NVARS bitsets 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] in first[5 - ntokens] (= FIRST |
120 | (5)) is set. |
121 `------------------------------------------------------------------*/
122
123 static void
124 set_firsts (void)
125 {
126 symbol_number_t i, j;
127
128 firsts = bitsetv_create (nvars, nvars, BITSET_FIXED);
129
130 for (i = ntokens; i < nsyms; i++)
131 for (j = 0; derives[i][j] >= 0; ++j)
132 {
133 int symbol = rules[derives[i][j]].rhs[0];
134 if (ISVAR (symbol))
135 bitset_set (FIRSTS (i), symbol - ntokens);
136 }
137
138 if (trace_flag)
139 bitsetv_matrix_dump (stderr, "RTC: Firsts Input", firsts);
140 bitsetv_reflexive_transitive_closure (firsts);
141 if (trace_flag)
142 bitsetv_matrix_dump (stderr, "RTC: Firsts Output", firsts);
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 symbol_number_t i, j;
162 rule_number_t k;
163
164 fderives = bitsetv_create (nvars, nrules + 1, BITSET_FIXED);
165
166 set_firsts ();
167
168 for (i = ntokens; i < nsyms; ++i)
169 for (j = ntokens; j < nsyms; ++j)
170 if (bitset_test (FIRSTS (i), j - ntokens))
171 for (k = 0; derives[j][k] > 0; ++k)
172 bitset_set (FDERIVES (i), derives[j][k]);
173
174 if (trace_flag)
175 print_fderives ();
176
177 bitsetv_free (firsts);
178 }
179
180 \f
181
182 void
183 new_closure (int n)
184 {
185 itemset = XCALLOC (item_number_t, n);
186
187 ruleset = bitset_create (nrules + 1, BITSET_FIXED);
188
189 set_fderives ();
190 }
191
192
193
194 void
195 closure (item_number_t *core, int n)
196 {
197 /* Index over CORE. */
198 int c;
199
200 /* A bit index over RULESET. */
201 rule_number_t ruleno;
202
203 bitset_iterator iter;
204
205 if (trace_flag)
206 print_closure ("input", core, n);
207
208 bitset_zero (ruleset);
209
210 for (c = 0; c < n; ++c)
211 if (ISVAR (ritem[core[c]]))
212 bitset_or (ruleset, ruleset, FDERIVES (ritem[core[c]]));
213
214 nritemset = 0;
215 c = 0;
216 BITSET_FOR_EACH (iter, ruleset, ruleno, 0)
217 {
218 item_number_t itemno = rules[ruleno].rhs - ritem;
219 while (c < n && core[c] < itemno)
220 {
221 itemset[nritemset] = core[c];
222 nritemset++;
223 c++;
224 }
225 itemset[nritemset] = itemno;
226 nritemset++;
227 };
228
229 while (c < n)
230 {
231 itemset[nritemset] = core[c];
232 nritemset++;
233 c++;
234 }
235
236 if (trace_flag)
237 print_closure ("output", itemset, nritemset);
238 }
239
240
241 void
242 free_closure (void)
243 {
244 XFREE (itemset);
245 bitset_free (ruleset);
246 bitsetv_free (fderives);
247 }