]>
Commit | Line | Data |
---|---|---|
ec14f0c8 | 1 | /* Type definitions for nondeterministic finite state machine for Bison. |
03b9e273 | 2 | |
75ad86ee | 3 | Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software |
c21493b8 | 4 | Foundation, Inc. |
5e893e1c AD |
5 | |
6 | This file is part of Bison, the GNU Compiler Compiler. | |
7 | ||
8 | Bison 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 2, or (at your option) | |
11 | any later version. | |
12 | ||
13 | Bison 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 Bison; see the file COPYING. If not, write to | |
0fb669f9 PE |
20 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
21 | Boston, MA 02110-1301, USA. */ | |
5e893e1c | 22 | |
2cec9080 | 23 | #include <config.h> |
5e893e1c | 24 | #include "system.h" |
17ee7397 PE |
25 | |
26 | #include <hash.h> | |
27 | ||
df0e7316 | 28 | #include "complain.h" |
62a3e4f0 | 29 | #include "gram.h" |
5e893e1c AD |
30 | #include "state.h" |
31 | ||
df0e7316 AD |
32 | |
33 | /*-------------------. | |
34 | | Shifts and Gotos. | | |
35 | `-------------------*/ | |
36 | ||
37 | ||
03b9e273 PE |
38 | /*-----------------------------------------. |
39 | | Create a new array of NUM shifts/gotos. | | |
40 | `-----------------------------------------*/ | |
2cec70b9 | 41 | |
17ee7397 PE |
42 | static transitions * |
43 | transitions_new (int num, state **the_states) | |
5e893e1c | 44 | { |
03b9e273 PE |
45 | size_t states_size = num * sizeof *the_states; |
46 | transitions *res = xmalloc (offsetof (transitions, states) + states_size); | |
ccaf65bc | 47 | res->num = num; |
03b9e273 | 48 | memcpy (res->states, the_states, states_size); |
5e893e1c AD |
49 | return res; |
50 | } | |
2cec70b9 AD |
51 | |
52 | ||
a737b216 PE |
53 | /*-------------------------------------------------------. |
54 | | Return the state such that SHIFTS contain a shift/goto | | |
55 | | to it on SYM. Abort if none found. | | |
56 | `-------------------------------------------------------*/ | |
24c7d800 | 57 | |
17ee7397 | 58 | state * |
a737b216 | 59 | transitions_to (transitions *shifts, symbol_number sym) |
24c7d800 AD |
60 | { |
61 | int j; | |
c21493b8 PE |
62 | for (j = 0; ; j++) |
63 | { | |
4f82b42a | 64 | aver (j < shifts->num); |
c21493b8 PE |
65 | if (TRANSITION_SYMBOL (shifts, j) == sym) |
66 | return shifts->states[j]; | |
67 | } | |
24c7d800 | 68 | } |
df0e7316 AD |
69 | |
70 | ||
71 | /*--------------------. | |
72 | | Error transitions. | | |
73 | `--------------------*/ | |
74 | ||
75 | ||
03b9e273 PE |
76 | /*---------------------------------. |
77 | | Create a new array of NUM errs. | | |
78 | `---------------------------------*/ | |
2cec70b9 | 79 | |
17ee7397 PE |
80 | errs * |
81 | errs_new (int num, symbol **tokens) | |
2cec70b9 | 82 | { |
03b9e273 PE |
83 | size_t symbols_size = num * sizeof *tokens; |
84 | errs *res = xmalloc (offsetof (errs, symbols) + symbols_size); | |
8b752b00 | 85 | res->num = num; |
03b9e273 | 86 | memcpy (res->symbols, tokens, symbols_size); |
2cec70b9 AD |
87 | return res; |
88 | } | |
80dac38c | 89 | |
df0e7316 AD |
90 | |
91 | ||
92 | ||
93 | /*-------------. | |
94 | | Reductions. | | |
95 | `-------------*/ | |
96 | ||
97 | ||
03b9e273 PE |
98 | /*---------------------------------------. |
99 | | Create a new array of NUM reductions. | | |
100 | `---------------------------------------*/ | |
80dac38c | 101 | |
17ee7397 PE |
102 | static reductions * |
103 | reductions_new (int num, rule **reds) | |
80dac38c | 104 | { |
03b9e273 PE |
105 | size_t rules_size = num * sizeof *reds; |
106 | reductions *res = xmalloc (offsetof (reductions, rules) + rules_size); | |
8b752b00 | 107 | res->num = num; |
742e4900 | 108 | res->lookahead_tokens = NULL; |
03b9e273 | 109 | memcpy (res->rules, reds, rules_size); |
80dac38c AD |
110 | return res; |
111 | } | |
10e5b8bd AD |
112 | |
113 | ||
df0e7316 AD |
114 | |
115 | /*---------. | |
116 | | States. | | |
117 | `---------*/ | |
118 | ||
119 | ||
17ee7397 | 120 | state_number nstates = 0; |
df0e7316 | 121 | /* FINAL_STATE is properly set by new_state when it recognizes its |
88bce5a2 | 122 | accessing symbol: $end. */ |
17ee7397 | 123 | state *final_state = NULL; |
df0e7316 | 124 | |
c7ca99d4 | 125 | |
8b752b00 AD |
126 | /*------------------------------------------------------------------. |
127 | | Create a new state with ACCESSING_SYMBOL, for those items. Store | | |
128 | | it in the state hash table. | | |
129 | `------------------------------------------------------------------*/ | |
df0e7316 | 130 | |
17ee7397 PE |
131 | state * |
132 | state_new (symbol_number accessing_symbol, | |
03b9e273 | 133 | size_t nitems, item_number *core) |
df0e7316 | 134 | { |
17ee7397 | 135 | state *res; |
03b9e273 | 136 | size_t items_size = nitems * sizeof *core; |
df0e7316 | 137 | |
4f82b42a | 138 | aver (nstates < STATE_NUMBER_MAXIMUM); |
df0e7316 | 139 | |
03b9e273 PE |
140 | res = xmalloc (offsetof (state, items) + items_size); |
141 | res->number = nstates++; | |
df0e7316 | 142 | res->accessing_symbol = accessing_symbol; |
03b9e273 PE |
143 | res->transitions = NULL; |
144 | res->reductions = NULL; | |
145 | res->errs = NULL; | |
146 | res->consistent = 0; | |
df0e7316 | 147 | res->solved_conflicts = NULL; |
5967f0cf | 148 | res->reachable = false; |
df0e7316 | 149 | |
03b9e273 PE |
150 | res->nitems = nitems; |
151 | memcpy (res->items, core, items_size); | |
df0e7316 | 152 | |
8b752b00 AD |
153 | state_hash_insert (res); |
154 | ||
df0e7316 AD |
155 | return res; |
156 | } | |
157 | ||
158 | ||
17ee7397 PE |
159 | /*---------. |
160 | | Free S. | | |
161 | `---------*/ | |
8b752b00 AD |
162 | |
163 | static void | |
17ee7397 | 164 | state_free (state *s) |
8b752b00 | 165 | { |
17ee7397 PE |
166 | free (s->transitions); |
167 | free (s->reductions); | |
168 | free (s->errs); | |
169 | free (s); | |
8b752b00 AD |
170 | } |
171 | ||
172 | ||
17ee7397 PE |
173 | /*---------------------------. |
174 | | Set the transitions of S. | | |
175 | `---------------------------*/ | |
32e1e0a4 AD |
176 | |
177 | void | |
17ee7397 | 178 | state_transitions_set (state *s, int num, state **trans) |
32e1e0a4 | 179 | { |
4f82b42a | 180 | aver (!s->transitions); |
17ee7397 | 181 | s->transitions = transitions_new (num, trans); |
32e1e0a4 AD |
182 | } |
183 | ||
184 | ||
17ee7397 PE |
185 | /*--------------------------. |
186 | | Set the reductions of S. | | |
187 | `--------------------------*/ | |
8a731ca8 AD |
188 | |
189 | void | |
17ee7397 | 190 | state_reductions_set (state *s, int num, rule **reds) |
8a731ca8 | 191 | { |
4f82b42a | 192 | aver (!s->reductions); |
17ee7397 | 193 | s->reductions = reductions_new (num, reds); |
8b752b00 AD |
194 | } |
195 | ||
196 | ||
cd08e51e | 197 | int |
17ee7397 | 198 | state_reduction_find (state *s, rule *r) |
cd08e51e AD |
199 | { |
200 | int i; | |
17ee7397 | 201 | reductions *reds = s->reductions; |
cd08e51e | 202 | for (i = 0; i < reds->num; ++i) |
17ee7397 | 203 | if (reds->rules[i] == r) |
cd08e51e AD |
204 | return i; |
205 | return -1; | |
206 | } | |
207 | ||
208 | ||
17ee7397 PE |
209 | /*--------------------. |
210 | | Set the errs of S. | | |
211 | `--------------------*/ | |
8b752b00 AD |
212 | |
213 | void | |
17ee7397 | 214 | state_errs_set (state *s, int num, symbol **tokens) |
8b752b00 | 215 | { |
4f82b42a | 216 | aver (!s->errs); |
17ee7397 | 217 | s->errs = errs_new (num, tokens); |
8a731ca8 AD |
218 | } |
219 | ||
220 | ||
32e1e0a4 | 221 | |
742e4900 JD |
222 | /*--------------------------------------------------. |
223 | | Print on OUT all the lookahead tokens such that S | | |
224 | | wants to reduce R. | | |
225 | `--------------------------------------------------*/ | |
10e5b8bd AD |
226 | |
227 | void | |
742e4900 | 228 | state_rule_lookahead_tokens_print (state *s, rule *r, FILE *out) |
10e5b8bd | 229 | { |
cd08e51e | 230 | /* Find the reduction we are handling. */ |
17ee7397 PE |
231 | reductions *reds = s->reductions; |
232 | int red = state_reduction_find (s, r); | |
10e5b8bd AD |
233 | |
234 | /* Print them if there are. */ | |
742e4900 | 235 | if (reds->lookahead_tokens && red != -1) |
10e5b8bd | 236 | { |
cd08e51e AD |
237 | bitset_iterator biter; |
238 | int k; | |
d0829076 | 239 | char const *sep = ""; |
2f4f028d | 240 | fprintf (out, " ["); |
742e4900 | 241 | BITSET_FOR_EACH (biter, reds->lookahead_tokens[red], k, 0) |
d0829076 PE |
242 | { |
243 | fprintf (out, "%s%s", sep, symbols[k]->tag); | |
244 | sep = ", "; | |
245 | } | |
2f4f028d | 246 | fprintf (out, "]"); |
10e5b8bd AD |
247 | } |
248 | } | |
c7ca99d4 AD |
249 | |
250 | ||
36b5e963 | 251 | /*---------------------. |
c7ca99d4 | 252 | | A state hash table. | |
36b5e963 | 253 | `---------------------*/ |
c7ca99d4 AD |
254 | |
255 | /* Initial capacity of states hash table. */ | |
256 | #define HT_INITIAL_CAPACITY 257 | |
257 | ||
258 | static struct hash_table *state_table = NULL; | |
259 | ||
260 | /* Two states are equal if they have the same core items. */ | |
03b9e273 | 261 | static inline bool |
17ee7397 | 262 | state_compare (state const *s1, state const *s2) |
c7ca99d4 | 263 | { |
f6fbd3da | 264 | size_t i; |
c7ca99d4 AD |
265 | |
266 | if (s1->nitems != s2->nitems) | |
8307162d | 267 | return false; |
c7ca99d4 AD |
268 | |
269 | for (i = 0; i < s1->nitems; ++i) | |
270 | if (s1->items[i] != s2->items[i]) | |
8307162d | 271 | return false; |
c7ca99d4 | 272 | |
8307162d | 273 | return true; |
c7ca99d4 AD |
274 | } |
275 | ||
03b9e273 PE |
276 | static bool |
277 | state_comparator (void const *s1, void const *s2) | |
278 | { | |
279 | return state_compare (s1, s2); | |
280 | } | |
281 | ||
233a88ad PE |
282 | static inline size_t |
283 | state_hash (state const *s, size_t tablesize) | |
c7ca99d4 AD |
284 | { |
285 | /* Add up the state's item numbers to get a hash key. */ | |
233a88ad | 286 | size_t key = 0; |
f6fbd3da | 287 | size_t i; |
17ee7397 PE |
288 | for (i = 0; i < s->nitems; ++i) |
289 | key += s->items[i]; | |
c7ca99d4 AD |
290 | return key % tablesize; |
291 | } | |
292 | ||
233a88ad PE |
293 | static size_t |
294 | state_hasher (void const *s, size_t tablesize) | |
03b9e273 PE |
295 | { |
296 | return state_hash (s, tablesize); | |
297 | } | |
298 | ||
c7ca99d4 AD |
299 | |
300 | /*-------------------------------. | |
301 | | Create the states hash table. | | |
302 | `-------------------------------*/ | |
303 | ||
304 | void | |
305 | state_hash_new (void) | |
306 | { | |
307 | state_table = hash_initialize (HT_INITIAL_CAPACITY, | |
308 | NULL, | |
03b9e273 PE |
309 | state_hasher, |
310 | state_comparator, | |
311 | NULL); | |
c7ca99d4 AD |
312 | } |
313 | ||
314 | ||
315 | /*---------------------------------------------. | |
316 | | Free the states hash table, not the states. | | |
317 | `---------------------------------------------*/ | |
318 | ||
319 | void | |
320 | state_hash_free (void) | |
321 | { | |
322 | hash_free (state_table); | |
323 | } | |
324 | ||
325 | ||
17ee7397 PE |
326 | /*-----------------------------------. |
327 | | Insert S in the state hash table. | | |
328 | `-----------------------------------*/ | |
c7ca99d4 AD |
329 | |
330 | void | |
17ee7397 | 331 | state_hash_insert (state *s) |
c7ca99d4 | 332 | { |
17ee7397 | 333 | hash_insert (state_table, s); |
c7ca99d4 AD |
334 | } |
335 | ||
336 | ||
337 | /*------------------------------------------------------------------. | |
338 | | Find the state associated to the CORE, and return it. If it does | | |
339 | | not exist yet, return NULL. | | |
340 | `------------------------------------------------------------------*/ | |
341 | ||
17ee7397 | 342 | state * |
03b9e273 | 343 | state_hash_lookup (size_t nitems, item_number *core) |
c7ca99d4 | 344 | { |
03b9e273 PE |
345 | size_t items_size = nitems * sizeof *core; |
346 | state *probe = xmalloc (offsetof (state, items) + items_size); | |
17ee7397 | 347 | state *entry; |
c7ca99d4 | 348 | |
03b9e273 PE |
349 | probe->nitems = nitems; |
350 | memcpy (probe->items, core, items_size); | |
c7ca99d4 AD |
351 | entry = hash_lookup (state_table, probe); |
352 | free (probe); | |
353 | return entry; | |
354 | } | |
355 | ||
5967f0cf JD |
356 | |
357 | /*------------------------------------------------------. | |
358 | | Mark S and all states reachable from S as reachable. | | |
359 | `------------------------------------------------------*/ | |
360 | ||
361 | static void | |
362 | state_mark_reachable_states (state *s) | |
363 | { | |
364 | if (s->reachable) | |
365 | return; | |
366 | s->reachable = true; | |
367 | for (int i = 0; i < s->transitions->num; ++i) | |
368 | if (!TRANSITION_IS_DISABLED (s->transitions, i)) | |
369 | state_mark_reachable_states (s->transitions->states[i]); | |
370 | } | |
371 | ||
372 | void | |
373 | state_remove_unreachable_states (state_number old_to_new[]) | |
374 | { | |
375 | state_number nstates_reachable = 0; | |
376 | state_mark_reachable_states (states[0]); | |
377 | for (state_number i = 0; i < nstates; ++i) | |
378 | { | |
379 | if (states[i]->reachable) | |
380 | { | |
381 | states[nstates_reachable] = states[i]; | |
382 | states[nstates_reachable]->number = nstates_reachable; | |
383 | old_to_new[i] = nstates_reachable++; | |
384 | } | |
385 | else | |
386 | { | |
387 | state_free (states[i]); | |
388 | old_to_new[i] = nstates; | |
389 | } | |
390 | } | |
391 | nstates = nstates_reachable; | |
392 | } | |
393 | ||
c7ca99d4 | 394 | /* All the decorated states, indexed by the state number. */ |
17ee7397 | 395 | state **states = NULL; |
c7ca99d4 AD |
396 | |
397 | ||
398 | /*----------------------. | |
399 | | Free all the states. | | |
400 | `----------------------*/ | |
401 | ||
402 | void | |
403 | states_free (void) | |
404 | { | |
17ee7397 | 405 | state_number i; |
c7ca99d4 | 406 | for (i = 0; i < nstates; ++i) |
8b752b00 AD |
407 | state_free (states[i]); |
408 | free (states); | |
c7ca99d4 | 409 | } |