]>
Commit | Line | Data |
---|---|---|
5e893e1c | 1 | /* Type definitions for nondeterministic finite state machine for bison, |
62a3e4f0 | 2 | Copyright (C) 2001, 2002 Free Software Foundation, Inc. |
5e893e1c AD |
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 | |
7 | it 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, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU 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 | |
18 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
19 | Boston, MA 02111-1307, USA. */ | |
20 | ||
21 | ||
22 | #include "system.h" | |
c7ca99d4 | 23 | #include "hash.h" |
df0e7316 | 24 | #include "complain.h" |
62a3e4f0 | 25 | #include "gram.h" |
5e893e1c AD |
26 | #include "state.h" |
27 | ||
df0e7316 AD |
28 | |
29 | /*-------------------. | |
30 | | Shifts and Gotos. | | |
31 | `-------------------*/ | |
32 | ||
33 | ||
c7ca99d4 AD |
34 | /*---------------------------------------. |
35 | | Create a new array of N shifts/gotos. | | |
36 | `---------------------------------------*/ | |
5e893e1c | 37 | |
ccaf65bc AD |
38 | #define TRANSITIONS_ALLOC(Nshifts) \ |
39 | (transitions_t *) xcalloc ((unsigned) (sizeof (transitions_t) \ | |
32e1e0a4 | 40 | + (Nshifts - 1) * sizeof (state_number_t)), 1) |
2cec70b9 | 41 | |
ccaf65bc AD |
42 | static transitions_t * |
43 | transitions_new (int num, state_number_t *the_states) | |
5e893e1c | 44 | { |
ccaf65bc AD |
45 | transitions_t *res = TRANSITIONS_ALLOC (num); |
46 | res->num = num; | |
47 | memcpy (res->states, the_states, num * sizeof (the_states[0])); | |
5e893e1c AD |
48 | return res; |
49 | } | |
2cec70b9 AD |
50 | |
51 | ||
ccaf65bc AD |
52 | /*-------------------------------------------------------------------. |
53 | | Return the state such these TRANSITIONS contain a shift/goto to it | | |
54 | | on SYMBOL. Aborts if none found. | | |
55 | `-------------------------------------------------------------------*/ | |
24c7d800 AD |
56 | |
57 | state_t * | |
ccaf65bc | 58 | transitions_to (transitions_t *shifts, symbol_number_t s) |
24c7d800 AD |
59 | { |
60 | int j; | |
ccaf65bc AD |
61 | for (j = 0; j < shifts->num; j++) |
62 | if (TRANSITION_SYMBOL (shifts, j) == s) | |
63 | return states[shifts->states[j]]; | |
24c7d800 AD |
64 | abort (); |
65 | } | |
df0e7316 AD |
66 | |
67 | ||
68 | /*--------------------. | |
69 | | Error transitions. | | |
70 | `--------------------*/ | |
71 | ||
72 | ||
2cec70b9 AD |
73 | /*-------------------------------. |
74 | | Create a new array of N errs. | | |
75 | `-------------------------------*/ | |
76 | ||
77 | #define ERRS_ALLOC(Nerrs) \ | |
8a731ca8 | 78 | (errs_t *) xcalloc ((unsigned) (sizeof (errs_t) \ |
2cec70b9 AD |
79 | + (Nerrs - 1) * sizeof (short)), 1) |
80 | ||
81 | ||
8a731ca8 | 82 | errs_t * |
2cec70b9 AD |
83 | errs_new (int n) |
84 | { | |
8a731ca8 | 85 | errs_t *res = ERRS_ALLOC (n); |
2cec70b9 AD |
86 | res->nerrs = n; |
87 | return res; | |
88 | } | |
89 | ||
90 | ||
8a731ca8 AD |
91 | errs_t * |
92 | errs_dup (errs_t *src) | |
2cec70b9 | 93 | { |
8a731ca8 | 94 | errs_t *res = errs_new (src->nerrs); |
82841af7 | 95 | memcpy (res->errs, src->errs, src->nerrs * sizeof (src->errs[0])); |
2cec70b9 AD |
96 | return res; |
97 | } | |
80dac38c | 98 | |
df0e7316 AD |
99 | |
100 | ||
101 | ||
102 | /*-------------. | |
103 | | Reductions. | | |
104 | `-------------*/ | |
105 | ||
106 | ||
80dac38c AD |
107 | /*-------------------------------------. |
108 | | Create a new array of N reductions. | | |
109 | `-------------------------------------*/ | |
110 | ||
111 | #define REDUCTIONS_ALLOC(Nreductions) \ | |
8a731ca8 | 112 | (reductions_t *) xcalloc ((unsigned) (sizeof (reductions_t) \ |
80dac38c AD |
113 | + (Nreductions - 1) * sizeof (short)), 1) |
114 | ||
8a731ca8 AD |
115 | static reductions_t * |
116 | reductions_new (int nreductions, short *reductions) | |
80dac38c | 117 | { |
8a731ca8 AD |
118 | reductions_t *res = REDUCTIONS_ALLOC (nreductions); |
119 | res->nreds = nreductions; | |
120 | memcpy (res->rules, reductions, nreductions * sizeof (reductions[0])); | |
80dac38c AD |
121 | return res; |
122 | } | |
10e5b8bd AD |
123 | |
124 | ||
df0e7316 AD |
125 | |
126 | /*---------. | |
127 | | States. | | |
128 | `---------*/ | |
129 | ||
130 | ||
131 | state_number_t nstates = 0; | |
132 | /* FINAL_STATE is properly set by new_state when it recognizes its | |
133 | accessing symbol: EOF. */ | |
134 | state_t *final_state = NULL; | |
135 | ||
c7ca99d4 AD |
136 | #define STATE_ALLOC(Nitems) \ |
137 | (state_t *) xcalloc ((unsigned) (sizeof (state_t) \ | |
138 | + (Nitems - 1) * sizeof (item_number_t)), 1) | |
139 | ||
df0e7316 AD |
140 | /*------------------------------------------------------------. |
141 | | Create a new state with ACCESSING_SYMBOL, for those items. | | |
142 | `------------------------------------------------------------*/ | |
143 | ||
144 | state_t * | |
145 | state_new (symbol_number_t accessing_symbol, | |
146 | size_t core_size, item_number_t *core) | |
147 | { | |
148 | state_t *res; | |
149 | ||
150 | if (nstates >= STATE_NUMBER_MAX) | |
151 | fatal (_("too many states (max %d)"), STATE_NUMBER_MAX); | |
152 | ||
df0e7316 AD |
153 | res = STATE_ALLOC (core_size); |
154 | res->accessing_symbol = accessing_symbol; | |
155 | res->number = nstates; | |
156 | ++nstates; | |
157 | res->solved_conflicts = NULL; | |
158 | ||
159 | res->nitems = core_size; | |
160 | memcpy (res->items, core, core_size * sizeof (core[0])); | |
161 | ||
162 | return res; | |
163 | } | |
164 | ||
165 | ||
32e1e0a4 AD |
166 | /*--------------------------. |
167 | | Set the shifts of STATE. | | |
168 | `--------------------------*/ | |
169 | ||
170 | void | |
ccaf65bc | 171 | state_transitions_set (state_t *state, int nshifts, state_number_t *shifts) |
32e1e0a4 | 172 | { |
ccaf65bc | 173 | state->shifts = transitions_new (nshifts, shifts); |
32e1e0a4 AD |
174 | } |
175 | ||
176 | ||
8a731ca8 AD |
177 | /*------------------------------. |
178 | | Set the reductions of STATE. | | |
179 | `------------------------------*/ | |
180 | ||
181 | void | |
182 | state_reductions_set (state_t *state, int nreductions, short *reductions) | |
183 | { | |
184 | state->reductions = reductions_new (nreductions, reductions); | |
185 | } | |
186 | ||
187 | ||
32e1e0a4 | 188 | |
10e5b8bd AD |
189 | /*--------------------------------------------------------------. |
190 | | Print on OUT all the lookaheads such that this STATE wants to | | |
191 | | reduce this RULE. | | |
192 | `--------------------------------------------------------------*/ | |
193 | ||
194 | void | |
195 | state_rule_lookaheads_print (state_t *state, rule_t *rule, FILE *out) | |
196 | { | |
197 | int j, k; | |
198 | int nlookaheads = 0; | |
199 | /* Count the number of lookaheads corresponding to this rule. */ | |
200 | for (j = 0; j < state->nlookaheads; ++j) | |
574fb2d5 AD |
201 | BITSET_EXECUTE (state->lookaheads[j], 0, k, |
202 | { | |
203 | if (state->lookaheads_rule[j]->number == rule->number) | |
10e5b8bd | 204 | nlookaheads++; |
574fb2d5 | 205 | }); |
10e5b8bd AD |
206 | |
207 | /* Print them if there are. */ | |
208 | if (nlookaheads) | |
209 | { | |
210 | fprintf (out, " ["); | |
211 | for (j = 0; j < state->nlookaheads; ++j) | |
574fb2d5 AD |
212 | BITSET_EXECUTE (state->lookaheads[j], 0, k, |
213 | { | |
214 | if (state->lookaheads_rule[j]->number == rule->number) | |
10e5b8bd AD |
215 | fprintf (out, "%s%s", |
216 | symbol_tag_get (symbols[k]), | |
217 | --nlookaheads ? ", " : ""); | |
574fb2d5 | 218 | }); |
10e5b8bd AD |
219 | fprintf (out, "]"); |
220 | } | |
221 | } | |
c7ca99d4 AD |
222 | |
223 | ||
224 | /*----------------------. | |
225 | | A state hash table. | | |
226 | `----------------------*/ | |
227 | ||
228 | /* Initial capacity of states hash table. */ | |
229 | #define HT_INITIAL_CAPACITY 257 | |
230 | ||
231 | static struct hash_table *state_table = NULL; | |
232 | ||
233 | /* Two states are equal if they have the same core items. */ | |
234 | static bool | |
235 | state_compare (const state_t *s1, const state_t *s2) | |
236 | { | |
237 | int i; | |
238 | ||
239 | if (s1->nitems != s2->nitems) | |
240 | return FALSE; | |
241 | ||
242 | for (i = 0; i < s1->nitems; ++i) | |
243 | if (s1->items[i] != s2->items[i]) | |
244 | return FALSE; | |
245 | ||
246 | return TRUE; | |
247 | } | |
248 | ||
249 | static unsigned int | |
250 | state_hash (const state_t *state, unsigned int tablesize) | |
251 | { | |
252 | /* Add up the state's item numbers to get a hash key. */ | |
253 | int key = 0; | |
254 | int i; | |
255 | for (i = 0; i < state->nitems; ++i) | |
256 | key += state->items[i]; | |
257 | return key % tablesize; | |
258 | } | |
259 | ||
260 | ||
261 | /*-------------------------------. | |
262 | | Create the states hash table. | | |
263 | `-------------------------------*/ | |
264 | ||
265 | void | |
266 | state_hash_new (void) | |
267 | { | |
268 | state_table = hash_initialize (HT_INITIAL_CAPACITY, | |
269 | NULL, | |
270 | (Hash_hasher) state_hash, | |
271 | (Hash_comparator) state_compare, | |
272 | (Hash_data_freer) NULL); | |
273 | } | |
274 | ||
275 | ||
276 | /*---------------------------------------------. | |
277 | | Free the states hash table, not the states. | | |
278 | `---------------------------------------------*/ | |
279 | ||
280 | void | |
281 | state_hash_free (void) | |
282 | { | |
283 | hash_free (state_table); | |
284 | } | |
285 | ||
286 | ||
287 | /*---------------------------------------. | |
288 | | Insert STATE in the state hash table. | | |
289 | `---------------------------------------*/ | |
290 | ||
291 | void | |
292 | state_hash_insert (state_t *state) | |
293 | { | |
294 | hash_insert (state_table, state); | |
295 | } | |
296 | ||
297 | ||
298 | /*------------------------------------------------------------------. | |
299 | | Find the state associated to the CORE, and return it. If it does | | |
300 | | not exist yet, return NULL. | | |
301 | `------------------------------------------------------------------*/ | |
302 | ||
303 | state_t * | |
304 | state_hash_lookup (size_t core_size, item_number_t *core) | |
305 | { | |
306 | state_t *probe = STATE_ALLOC (core_size); | |
307 | state_t *entry; | |
308 | ||
309 | probe->nitems = core_size; | |
310 | memcpy (probe->items, core, core_size * sizeof (core[0])); | |
311 | entry = hash_lookup (state_table, probe); | |
312 | free (probe); | |
313 | return entry; | |
314 | } | |
315 | ||
316 | /* All the decorated states, indexed by the state number. */ | |
317 | state_t **states = NULL; | |
318 | ||
319 | ||
320 | /*----------------------. | |
321 | | Free all the states. | | |
322 | `----------------------*/ | |
323 | ||
324 | void | |
325 | states_free (void) | |
326 | { | |
327 | state_number_t i; | |
328 | ||
329 | for (i = 0; i < nstates; ++i) | |
330 | { | |
331 | free (states[i]->shifts); | |
332 | XFREE (states[i]->reductions); | |
333 | free (states[i]->errs); | |
334 | free (states[i]); | |
335 | } | |
336 | XFREE (states); | |
337 | } |