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