]>
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 | |
640748ee AD |
38 | #define TRANSITIONS_ALLOC(Num) \ |
39 | (transitions_t *) xcalloc ((sizeof (transitions_t) \ | |
40 | + (Num - 1) * sizeof (state_t *)), 1) | |
2cec70b9 | 41 | |
ccaf65bc | 42 | static transitions_t * |
640748ee | 43 | transitions_new (int num, state_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) | |
640748ee | 63 | return 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 | ||
8b752b00 AD |
77 | #define ERRS_ALLOC(Nerrs) \ |
78 | (errs_t *) xcalloc ((sizeof (errs_t) \ | |
640748ee | 79 | + (Nerrs - 1) * sizeof (symbol_t *)), 1) |
2cec70b9 AD |
80 | |
81 | ||
8a731ca8 | 82 | errs_t * |
640748ee | 83 | errs_new (int num, symbol_t **tokens) |
2cec70b9 | 84 | { |
8b752b00 AD |
85 | errs_t *res = ERRS_ALLOC (num); |
86 | res->num = num; | |
87 | memcpy (res->symbols, tokens, num * sizeof (tokens[0])); | |
2cec70b9 AD |
88 | return res; |
89 | } | |
80dac38c | 90 | |
df0e7316 AD |
91 | |
92 | ||
93 | ||
94 | /*-------------. | |
95 | | Reductions. | | |
96 | `-------------*/ | |
97 | ||
98 | ||
80dac38c AD |
99 | /*-------------------------------------. |
100 | | Create a new array of N reductions. | | |
101 | `-------------------------------------*/ | |
102 | ||
8b752b00 AD |
103 | #define REDUCTIONS_ALLOC(Nreductions) \ |
104 | (reductions_t *) xcalloc ((sizeof (reductions_t) \ | |
640748ee | 105 | + (Nreductions - 1) * sizeof (rule_t *)), 1) |
80dac38c | 106 | |
8a731ca8 | 107 | static reductions_t * |
640748ee | 108 | reductions_new (int num, rule_t **reductions) |
80dac38c | 109 | { |
8b752b00 AD |
110 | reductions_t *res = REDUCTIONS_ALLOC (num); |
111 | res->num = num; | |
112 | memcpy (res->rules, reductions, num * sizeof (reductions[0])); | |
80dac38c AD |
113 | return res; |
114 | } | |
10e5b8bd AD |
115 | |
116 | ||
df0e7316 AD |
117 | |
118 | /*---------. | |
119 | | States. | | |
120 | `---------*/ | |
121 | ||
122 | ||
123 | state_number_t nstates = 0; | |
124 | /* FINAL_STATE is properly set by new_state when it recognizes its | |
88bce5a2 | 125 | accessing symbol: $end. */ |
df0e7316 AD |
126 | state_t *final_state = NULL; |
127 | ||
c7ca99d4 | 128 | #define STATE_ALLOC(Nitems) \ |
640748ee AD |
129 | (state_t *) xcalloc ((sizeof (state_t) \ |
130 | + (Nitems - 1) * sizeof (item_number_t)), 1) | |
c7ca99d4 | 131 | |
8b752b00 AD |
132 | /*------------------------------------------------------------------. |
133 | | Create a new state with ACCESSING_SYMBOL, for those items. Store | | |
134 | | it in the state hash table. | | |
135 | `------------------------------------------------------------------*/ | |
df0e7316 AD |
136 | |
137 | state_t * | |
138 | state_new (symbol_number_t accessing_symbol, | |
139 | size_t core_size, item_number_t *core) | |
140 | { | |
141 | state_t *res; | |
142 | ||
143 | if (nstates >= STATE_NUMBER_MAX) | |
144 | fatal (_("too many states (max %d)"), STATE_NUMBER_MAX); | |
145 | ||
df0e7316 AD |
146 | res = STATE_ALLOC (core_size); |
147 | res->accessing_symbol = accessing_symbol; | |
148 | res->number = nstates; | |
149 | ++nstates; | |
150 | res->solved_conflicts = NULL; | |
151 | ||
152 | res->nitems = core_size; | |
153 | memcpy (res->items, core, core_size * sizeof (core[0])); | |
154 | ||
8b752b00 AD |
155 | state_hash_insert (res); |
156 | ||
df0e7316 AD |
157 | return res; |
158 | } | |
159 | ||
160 | ||
8b752b00 AD |
161 | /*-------------. |
162 | | Free STATE. | | |
163 | `-------------*/ | |
164 | ||
165 | static void | |
166 | state_free (state_t *state) | |
167 | { | |
168 | free (state->transitions); | |
169 | free (state->reductions); | |
170 | free (state->errs); | |
171 | free (state); | |
172 | } | |
173 | ||
174 | ||
175 | /*-------------------------------. | |
176 | | Set the transitions of STATE. | | |
177 | `-------------------------------*/ | |
32e1e0a4 AD |
178 | |
179 | void | |
640748ee | 180 | state_transitions_set (state_t *state, int num, state_t **transitions) |
32e1e0a4 | 181 | { |
8b752b00 AD |
182 | assert (!state->transitions); |
183 | state->transitions = transitions_new (num, transitions); | |
32e1e0a4 AD |
184 | } |
185 | ||
186 | ||
8a731ca8 AD |
187 | /*------------------------------. |
188 | | Set the reductions of STATE. | | |
189 | `------------------------------*/ | |
190 | ||
191 | void | |
640748ee | 192 | state_reductions_set (state_t *state, int num, rule_t **reductions) |
8a731ca8 | 193 | { |
8b752b00 AD |
194 | assert (!state->reductions); |
195 | state->reductions = reductions_new (num, reductions); | |
196 | } | |
197 | ||
198 | ||
199 | /*------------------------. | |
200 | | Set the errs of STATE. | | |
201 | `------------------------*/ | |
202 | ||
203 | void | |
640748ee | 204 | state_errs_set (state_t *state, int num, symbol_t **tokens) |
8b752b00 AD |
205 | { |
206 | assert (!state->errs); | |
207 | state->errs = errs_new (num, tokens); | |
8a731ca8 AD |
208 | } |
209 | ||
210 | ||
32e1e0a4 | 211 | |
10e5b8bd AD |
212 | /*--------------------------------------------------------------. |
213 | | Print on OUT all the lookaheads such that this STATE wants to | | |
214 | | reduce this RULE. | | |
215 | `--------------------------------------------------------------*/ | |
216 | ||
217 | void | |
218 | state_rule_lookaheads_print (state_t *state, rule_t *rule, FILE *out) | |
219 | { | |
220 | int j, k; | |
613f5e1a | 221 | bitset_iterator biter; |
10e5b8bd AD |
222 | int nlookaheads = 0; |
223 | /* Count the number of lookaheads corresponding to this rule. */ | |
224 | for (j = 0; j < state->nlookaheads; ++j) | |
613f5e1a | 225 | BITSET_FOR_EACH (biter, state->lookaheads[j], k, 0) |
bb0027a9 | 226 | if (state->lookaheads_rule[j] == rule) |
10e5b8bd AD |
227 | nlookaheads++; |
228 | ||
229 | /* Print them if there are. */ | |
230 | if (nlookaheads) | |
231 | { | |
232 | fprintf (out, " ["); | |
233 | for (j = 0; j < state->nlookaheads; ++j) | |
613f5e1a | 234 | BITSET_FOR_EACH (biter, state->lookaheads[j], k, 0) |
bb0027a9 | 235 | if (state->lookaheads_rule[j] == rule) |
10e5b8bd | 236 | fprintf (out, "%s%s", |
97650f4e | 237 | symbols[k]->tag, |
10e5b8bd AD |
238 | --nlookaheads ? ", " : ""); |
239 | fprintf (out, "]"); | |
240 | } | |
241 | } | |
c7ca99d4 AD |
242 | |
243 | ||
244 | /*----------------------. | |
245 | | A state hash table. | | |
246 | `----------------------*/ | |
247 | ||
248 | /* Initial capacity of states hash table. */ | |
249 | #define HT_INITIAL_CAPACITY 257 | |
250 | ||
251 | static struct hash_table *state_table = NULL; | |
252 | ||
253 | /* Two states are equal if they have the same core items. */ | |
254 | static bool | |
255 | state_compare (const state_t *s1, const state_t *s2) | |
256 | { | |
257 | int i; | |
258 | ||
259 | if (s1->nitems != s2->nitems) | |
260 | return FALSE; | |
261 | ||
262 | for (i = 0; i < s1->nitems; ++i) | |
263 | if (s1->items[i] != s2->items[i]) | |
264 | return FALSE; | |
265 | ||
266 | return TRUE; | |
267 | } | |
268 | ||
269 | static unsigned int | |
270 | state_hash (const state_t *state, unsigned int tablesize) | |
271 | { | |
272 | /* Add up the state's item numbers to get a hash key. */ | |
273 | int key = 0; | |
274 | int i; | |
275 | for (i = 0; i < state->nitems; ++i) | |
276 | key += state->items[i]; | |
277 | return key % tablesize; | |
278 | } | |
279 | ||
280 | ||
281 | /*-------------------------------. | |
282 | | Create the states hash table. | | |
283 | `-------------------------------*/ | |
284 | ||
285 | void | |
286 | state_hash_new (void) | |
287 | { | |
288 | state_table = hash_initialize (HT_INITIAL_CAPACITY, | |
289 | NULL, | |
290 | (Hash_hasher) state_hash, | |
291 | (Hash_comparator) state_compare, | |
292 | (Hash_data_freer) NULL); | |
293 | } | |
294 | ||
295 | ||
296 | /*---------------------------------------------. | |
297 | | Free the states hash table, not the states. | | |
298 | `---------------------------------------------*/ | |
299 | ||
300 | void | |
301 | state_hash_free (void) | |
302 | { | |
303 | hash_free (state_table); | |
304 | } | |
305 | ||
306 | ||
307 | /*---------------------------------------. | |
308 | | Insert STATE in the state hash table. | | |
309 | `---------------------------------------*/ | |
310 | ||
311 | void | |
312 | state_hash_insert (state_t *state) | |
313 | { | |
314 | hash_insert (state_table, state); | |
315 | } | |
316 | ||
317 | ||
318 | /*------------------------------------------------------------------. | |
319 | | Find the state associated to the CORE, and return it. If it does | | |
320 | | not exist yet, return NULL. | | |
321 | `------------------------------------------------------------------*/ | |
322 | ||
323 | state_t * | |
324 | state_hash_lookup (size_t core_size, item_number_t *core) | |
325 | { | |
326 | state_t *probe = STATE_ALLOC (core_size); | |
327 | state_t *entry; | |
328 | ||
329 | probe->nitems = core_size; | |
330 | memcpy (probe->items, core, core_size * sizeof (core[0])); | |
331 | entry = hash_lookup (state_table, probe); | |
332 | free (probe); | |
333 | return entry; | |
334 | } | |
335 | ||
336 | /* All the decorated states, indexed by the state number. */ | |
337 | state_t **states = NULL; | |
338 | ||
339 | ||
340 | /*----------------------. | |
341 | | Free all the states. | | |
342 | `----------------------*/ | |
343 | ||
344 | void | |
345 | states_free (void) | |
346 | { | |
347 | state_number_t i; | |
c7ca99d4 | 348 | for (i = 0; i < nstates; ++i) |
8b752b00 AD |
349 | state_free (states[i]); |
350 | free (states); | |
c7ca99d4 | 351 | } |