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