]>
Commit | Line | Data |
---|---|---|
1 | /* Closures for Bison | |
2 | ||
3 | Copyright (C) 1984, 1989, 2000-2002, 2004-2005, 2007, 2009-2013 Free | |
4 | Software Foundation, Inc. | |
5 | ||
6 | This file is part of Bison, the GNU Compiler Compiler. | |
7 | ||
8 | This program is free software: you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation, either version 3 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
20 | ||
21 | #include <config.h> | |
22 | #include "system.h" | |
23 | ||
24 | #include <bitset.h> | |
25 | #include <bitsetv-print.h> | |
26 | #include <bitsetv.h> | |
27 | ||
28 | #include "closure.h" | |
29 | #include "derives.h" | |
30 | #include "getargs.h" | |
31 | #include "gram.h" | |
32 | #include "reader.h" | |
33 | #include "symtab.h" | |
34 | ||
35 | /* NITEMSET is the size of the array ITEMSET. */ | |
36 | item_number *itemset; | |
37 | size_t nitemset; | |
38 | ||
39 | static bitset ruleset; | |
40 | ||
41 | /* internal data. See comments before set_fderives and set_firsts. */ | |
42 | static bitsetv fderives = NULL; | |
43 | static bitsetv firsts = NULL; | |
44 | ||
45 | /* Retrieve the FDERIVES/FIRSTS sets of the nonterminals numbered Var. */ | |
46 | #define FDERIVES(Var) fderives[(Var) - ntokens] | |
47 | #define FIRSTS(Var) firsts[(Var) - ntokens] | |
48 | \f | |
49 | ||
50 | /*-----------------. | |
51 | | Debugging code. | | |
52 | `-----------------*/ | |
53 | ||
54 | static void | |
55 | print_closure (char const *title, item_number const *array, size_t size) | |
56 | { | |
57 | size_t i; | |
58 | fprintf (stderr, "Closure: %s\n", title); | |
59 | for (i = 0; i < size; ++i) | |
60 | { | |
61 | item_number *rp; | |
62 | fprintf (stderr, " %2d: .", array[i]); | |
63 | for (rp = &ritem[array[i]]; *rp >= 0; ++rp) | |
64 | fprintf (stderr, " %s", symbols[*rp]->tag); | |
65 | fprintf (stderr, " (rule %d)\n", -*rp - 1); | |
66 | } | |
67 | fputs ("\n\n", stderr); | |
68 | } | |
69 | ||
70 | ||
71 | static void | |
72 | print_firsts (void) | |
73 | { | |
74 | symbol_number i, j; | |
75 | ||
76 | fprintf (stderr, "FIRSTS\n"); | |
77 | for (i = ntokens; i < nsyms; i++) | |
78 | { | |
79 | bitset_iterator iter; | |
80 | fprintf (stderr, " %s firsts\n", symbols[i]->tag); | |
81 | BITSET_FOR_EACH (iter, FIRSTS (i), j, 0) | |
82 | fprintf (stderr, " %s\n", symbols[j + ntokens]->tag); | |
83 | } | |
84 | fprintf (stderr, "\n\n"); | |
85 | } | |
86 | ||
87 | ||
88 | static void | |
89 | print_fderives (void) | |
90 | { | |
91 | int i; | |
92 | rule_number r; | |
93 | ||
94 | fprintf (stderr, "FDERIVES\n"); | |
95 | for (i = ntokens; i < nsyms; i++) | |
96 | { | |
97 | bitset_iterator iter; | |
98 | fprintf (stderr, " %s derives\n", symbols[i]->tag); | |
99 | BITSET_FOR_EACH (iter, FDERIVES (i), r, 0) | |
100 | { | |
101 | fprintf (stderr, " %3d ", r); | |
102 | rule_rhs_print (&rules[r], stderr); | |
103 | fprintf (stderr, "\n"); | |
104 | } | |
105 | } | |
106 | fprintf (stderr, "\n\n"); | |
107 | } | |
108 | \f | |
109 | /*------------------------------------------------------------------. | |
110 | | Set FIRSTS to be an NVARS array of NVARS bitsets indicating which | | |
111 | | items can represent the beginning of the input corresponding to | | |
112 | | which other items. | | |
113 | | | | |
114 | | For example, if some rule expands symbol 5 into the sequence of | | |
115 | | symbols 8 3 20, the symbol 8 can be the beginning of the data for | | |
116 | | symbol 5, so the bit [8 - ntokens] in first[5 - ntokens] (= FIRST | | |
117 | | (5)) is set. | | |
118 | `------------------------------------------------------------------*/ | |
119 | ||
120 | static void | |
121 | set_firsts (void) | |
122 | { | |
123 | symbol_number i, j; | |
124 | ||
125 | firsts = bitsetv_create (nvars, nvars, BITSET_FIXED); | |
126 | ||
127 | for (i = ntokens; i < nsyms; i++) | |
128 | for (j = 0; derives[i - ntokens][j]; ++j) | |
129 | { | |
130 | item_number sym = derives[i - ntokens][j]->rhs[0]; | |
131 | if (ISVAR (sym)) | |
132 | bitset_set (FIRSTS (i), sym - ntokens); | |
133 | } | |
134 | ||
135 | if (trace_flag & trace_sets) | |
136 | bitsetv_matrix_dump (stderr, "RTC: Firsts Input", firsts); | |
137 | bitsetv_reflexive_transitive_closure (firsts); | |
138 | if (trace_flag & trace_sets) | |
139 | bitsetv_matrix_dump (stderr, "RTC: Firsts Output", firsts); | |
140 | ||
141 | if (trace_flag & trace_sets) | |
142 | print_firsts (); | |
143 | } | |
144 | ||
145 | /*-------------------------------------------------------------------. | |
146 | | Set FDERIVES to an NVARS by NRULES matrix of bits indicating which | | |
147 | | rules can help derive the beginning of the data for each | | |
148 | | nonterminal. | | |
149 | | | | |
150 | | For example, if symbol 5 can be derived as the sequence of symbols | | |
151 | | 8 3 20, and one of the rules for deriving symbol 8 is rule 4, then | | |
152 | | the [5 - NTOKENS, 4] bit in FDERIVES is set. | | |
153 | `-------------------------------------------------------------------*/ | |
154 | ||
155 | static void | |
156 | set_fderives (void) | |
157 | { | |
158 | symbol_number i, j; | |
159 | rule_number k; | |
160 | ||
161 | fderives = bitsetv_create (nvars, nrules, BITSET_FIXED); | |
162 | ||
163 | set_firsts (); | |
164 | ||
165 | for (i = ntokens; i < nsyms; ++i) | |
166 | for (j = ntokens; j < nsyms; ++j) | |
167 | if (bitset_test (FIRSTS (i), j - ntokens)) | |
168 | for (k = 0; derives[j - ntokens][k]; ++k) | |
169 | bitset_set (FDERIVES (i), derives[j - ntokens][k]->number); | |
170 | ||
171 | if (trace_flag & trace_sets) | |
172 | print_fderives (); | |
173 | ||
174 | bitsetv_free (firsts); | |
175 | } | |
176 | ||
177 | \f | |
178 | ||
179 | void | |
180 | new_closure (unsigned int n) | |
181 | { | |
182 | itemset = xnmalloc (n, sizeof *itemset); | |
183 | ||
184 | ruleset = bitset_create (nrules, BITSET_FIXED); | |
185 | ||
186 | set_fderives (); | |
187 | } | |
188 | ||
189 | ||
190 | ||
191 | void | |
192 | closure (item_number const *core, size_t n) | |
193 | { | |
194 | /* Index over CORE. */ | |
195 | size_t c; | |
196 | ||
197 | /* A bit index over RULESET. */ | |
198 | rule_number ruleno; | |
199 | ||
200 | bitset_iterator iter; | |
201 | ||
202 | if (trace_flag & trace_sets) | |
203 | print_closure ("input", core, n); | |
204 | ||
205 | bitset_zero (ruleset); | |
206 | ||
207 | for (c = 0; c < n; ++c) | |
208 | if (ISVAR (ritem[core[c]])) | |
209 | bitset_or (ruleset, ruleset, FDERIVES (ritem[core[c]])); | |
210 | ||
211 | /* core is sorted on item index in ritem, which is sorted on rule number. | |
212 | Compute itemset with the same sort. */ | |
213 | nitemset = 0; | |
214 | c = 0; | |
215 | BITSET_FOR_EACH (iter, ruleset, ruleno, 0) | |
216 | { | |
217 | item_number itemno = rules[ruleno].rhs - ritem; | |
218 | while (c < n && core[c] < itemno) | |
219 | { | |
220 | itemset[nitemset] = core[c]; | |
221 | nitemset++; | |
222 | c++; | |
223 | } | |
224 | itemset[nitemset] = itemno; | |
225 | nitemset++; | |
226 | }; | |
227 | ||
228 | while (c < n) | |
229 | { | |
230 | itemset[nitemset] = core[c]; | |
231 | nitemset++; | |
232 | c++; | |
233 | } | |
234 | ||
235 | if (trace_flag & trace_sets) | |
236 | print_closure ("output", itemset, nitemset); | |
237 | } | |
238 | ||
239 | ||
240 | void | |
241 | free_closure (void) | |
242 | { | |
243 | free (itemset); | |
244 | bitset_free (ruleset); | |
245 | bitsetv_free (fderives); | |
246 | } |