]>
Commit | Line | Data |
---|---|---|
40675e7c | 1 | /* Generate the nondeterministic finite state machine for bison, |
602bbf31 | 2 | Copyright 1984, 1986, 1989, 2000, 2001, 2002 Free Software Foundation, Inc. |
40675e7c | 3 | |
2fa6973e | 4 | This file is part of Bison, the GNU Compiler Compiler. |
40675e7c | 5 | |
2fa6973e AD |
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. | |
40675e7c | 10 | |
2fa6973e AD |
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. | |
40675e7c | 15 | |
2fa6973e AD |
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. */ | |
40675e7c DM |
20 | |
21 | ||
22 | /* See comments in state.h for the data structures that represent it. | |
23 | The entry point is generate_states. */ | |
24 | ||
40675e7c | 25 | #include "system.h" |
602bbf31 | 26 | #include "bitset.h" |
8b3df748 | 27 | #include "quotearg.h" |
0e78e603 | 28 | #include "symtab.h" |
5fbb0954 | 29 | #include "gram.h" |
9bfe901c | 30 | #include "getargs.h" |
c87d4863 | 31 | #include "reader.h" |
40675e7c DM |
32 | #include "gram.h" |
33 | #include "state.h" | |
a0f6b076 | 34 | #include "complain.h" |
2fa6973e | 35 | #include "closure.h" |
403b315b | 36 | #include "LR0.h" |
49701457 | 37 | #include "lalr.h" |
630e182b | 38 | #include "reduce.h" |
40675e7c | 39 | |
d57650a5 AD |
40 | state_number_t nstates = 0; |
41 | /* FINAL_STATE is properly set by new_state when it recognizes its | |
610ab194 | 42 | accessing symbol: EOF. */ |
d57650a5 | 43 | state_t *final_state = NULL; |
6a164e0c | 44 | static state_t *first_state = NULL; |
40675e7c | 45 | |
f693ad14 AD |
46 | static state_t *this_state = NULL; |
47 | static state_t *last_state = NULL; | |
40675e7c DM |
48 | |
49 | static int nshifts; | |
a49aecd5 | 50 | static symbol_number_t *shift_symbol = NULL; |
40675e7c | 51 | |
342b8b6e | 52 | static short *redset = NULL; |
d57650a5 | 53 | static state_number_t *shiftset = NULL; |
40675e7c | 54 | |
62a3e4f0 | 55 | static item_number_t **kernel_base = NULL; |
6255b435 | 56 | static int *kernel_size = NULL; |
62a3e4f0 | 57 | static item_number_t *kernel_items = NULL; |
40675e7c DM |
58 | |
59 | /* hash table for states, to recognize equivalent ones. */ | |
60 | ||
f693ad14 AD |
61 | #define STATE_HASH_SIZE 1009 |
62 | static state_t **state_hash = NULL; | |
40675e7c | 63 | |
2fa6973e | 64 | \f |
4a120d45 | 65 | static void |
d2729d44 | 66 | allocate_itemsets (void) |
40675e7c | 67 | { |
b4c4ccc2 | 68 | int i, r; |
62a3e4f0 | 69 | item_number_t *rhsp; |
40675e7c | 70 | |
630e182b AD |
71 | /* Count the number of occurrences of all the symbols in RITEMS. |
72 | Note that useless productions (hence useless nonterminals) are | |
73 | browsed too, hence we need to allocate room for _all_ the | |
74 | symbols. */ | |
75 | int count = 0; | |
76 | short *symbol_count = XCALLOC (short, nsyms + nuseless_nonterminals); | |
40675e7c | 77 | |
b4c4ccc2 AD |
78 | for (r = 1; r < nrules + 1; ++r) |
79 | for (rhsp = rules[r].rhs; *rhsp >= 0; ++rhsp) | |
c87d4863 AD |
80 | { |
81 | count++; | |
b4c4ccc2 | 82 | symbol_count[*rhsp]++; |
c87d4863 | 83 | } |
40675e7c | 84 | |
2fa6973e AD |
85 | /* See comments before new_itemsets. All the vectors of items |
86 | live inside KERNEL_ITEMS. The number of active items after | |
40675e7c DM |
87 | some symbol cannot be more than the number of times that symbol |
88 | appears as an item, which is symbol_count[symbol]. | |
89 | We allocate that much space for each symbol. */ | |
90 | ||
62a3e4f0 | 91 | kernel_base = XCALLOC (item_number_t *, nsyms); |
342b8b6e | 92 | if (count) |
62a3e4f0 | 93 | kernel_items = XCALLOC (item_number_t, count); |
40675e7c DM |
94 | |
95 | count = 0; | |
96 | for (i = 0; i < nsyms; i++) | |
97 | { | |
98 | kernel_base[i] = kernel_items + count; | |
99 | count += symbol_count[i]; | |
100 | } | |
101 | ||
630e182b | 102 | free (symbol_count); |
0e41b407 | 103 | kernel_size = XCALLOC (int, nsyms); |
40675e7c DM |
104 | } |
105 | ||
106 | ||
4a120d45 | 107 | static void |
d2729d44 | 108 | allocate_storage (void) |
40675e7c | 109 | { |
2fa6973e | 110 | allocate_itemsets (); |
40675e7c | 111 | |
d57650a5 | 112 | shiftset = XCALLOC (state_number_t, nsyms); |
d7913476 | 113 | redset = XCALLOC (short, nrules + 1); |
f693ad14 | 114 | state_hash = XCALLOC (state_t *, STATE_HASH_SIZE); |
a49aecd5 | 115 | shift_symbol = XCALLOC (symbol_number_t, nsyms); |
40675e7c DM |
116 | } |
117 | ||
118 | ||
4a120d45 | 119 | static void |
d2729d44 | 120 | free_storage (void) |
40675e7c | 121 | { |
630e182b AD |
122 | free (shift_symbol); |
123 | free (redset); | |
124 | free (shiftset); | |
125 | free (kernel_base); | |
126 | free (kernel_size); | |
d7913476 | 127 | XFREE (kernel_items); |
f693ad14 | 128 | free (state_hash); |
40675e7c DM |
129 | } |
130 | ||
131 | ||
132 | ||
40675e7c | 133 | |
2fa6973e AD |
134 | /*----------------------------------------------------------------. |
135 | | Find which symbols can be shifted in the current state, and for | | |
136 | | each one record which items would be active after that shift. | | |
137 | | Uses the contents of itemset. | | |
138 | | | | |
139 | | shift_symbol is set to a vector of the symbols that can be | | |
140 | | shifted. For each symbol in the grammar, kernel_base[symbol] | | |
141 | | points to a vector of item numbers activated if that symbol is | | |
125ecb56 | 142 | | shifted, and kernel_size[symbol] is their numbers. | |
2fa6973e | 143 | `----------------------------------------------------------------*/ |
40675e7c | 144 | |
4a120d45 | 145 | static void |
d2729d44 | 146 | new_itemsets (void) |
40675e7c | 147 | { |
2fa6973e | 148 | int i; |
2fa6973e | 149 | |
9bfe901c | 150 | if (trace_flag) |
c87d4863 AD |
151 | fprintf (stderr, "Entering new_itemsets, state = %d\n", |
152 | this_state->number); | |
40675e7c DM |
153 | |
154 | for (i = 0; i < nsyms; i++) | |
125ecb56 | 155 | kernel_size[i] = 0; |
40675e7c | 156 | |
b2872512 | 157 | nshifts = 0; |
40675e7c | 158 | |
5123689b | 159 | for (i = 0; i < nritemset; ++i) |
5fbb0954 AD |
160 | if (ritem[itemset[i]] >= 0) |
161 | { | |
a49aecd5 AD |
162 | symbol_number_t symbol |
163 | = item_number_as_symbol_number (ritem[itemset[i]]); | |
5fbb0954 AD |
164 | if (!kernel_size[symbol]) |
165 | { | |
166 | shift_symbol[nshifts] = symbol; | |
167 | nshifts++; | |
168 | } | |
169 | ||
170 | kernel_base[symbol][kernel_size[symbol]] = itemset[i] + 1; | |
171 | kernel_size[symbol]++; | |
172 | } | |
40675e7c DM |
173 | } |
174 | ||
175 | ||
176 | ||
2fa6973e AD |
177 | /*-----------------------------------------------------------------. |
178 | | Subroutine of get_state. Create a new state for those items, if | | |
179 | | necessary. | | |
180 | `-----------------------------------------------------------------*/ | |
40675e7c | 181 | |
f693ad14 | 182 | static state_t * |
a49aecd5 | 183 | new_state (symbol_number_t symbol, size_t core_size, item_number_t *core) |
40675e7c | 184 | { |
f693ad14 | 185 | state_t *p; |
40675e7c | 186 | |
9bfe901c | 187 | if (trace_flag) |
c87d4863 | 188 | fprintf (stderr, "Entering new_state, state = %d, symbol = %d (%s)\n", |
6b98e4b5 | 189 | nstates, symbol, symbol_tag_get (symbols[symbol])); |
40675e7c | 190 | |
d57650a5 AD |
191 | if (nstates >= STATE_NUMBER_MAX) |
192 | fatal (_("too many states (max %d)"), STATE_NUMBER_MAX); | |
40675e7c | 193 | |
458be8e0 | 194 | p = STATE_ALLOC (core_size); |
2fa6973e AD |
195 | p->accessing_symbol = symbol; |
196 | p->number = nstates; | |
b408954b | 197 | p->solved_conflicts = NULL; |
2fa6973e | 198 | |
b408954b | 199 | p->nitems = core_size; |
458be8e0 | 200 | memcpy (p->items, core, core_size * sizeof (core[0])); |
2fa6973e | 201 | |
643a5994 AD |
202 | /* If this is the eoftoken, and this is not the initial state, then |
203 | this is the final state. */ | |
204 | if (symbol == 0 && first_state) | |
d57650a5 | 205 | final_state = p; |
643a5994 AD |
206 | |
207 | if (!first_state) | |
208 | first_state = p; | |
209 | if (last_state) | |
210 | last_state->next = p; | |
2fa6973e | 211 | last_state = p; |
40675e7c | 212 | |
643a5994 | 213 | nstates++; |
30171f79 | 214 | |
2fa6973e AD |
215 | return p; |
216 | } | |
40675e7c | 217 | |
2fa6973e AD |
218 | |
219 | /*--------------------------------------------------------------. | |
220 | | Find the state number for the state we would get to (from the | | |
221 | | current state) by shifting symbol. Create a new state if no | | |
97db7bd4 | 222 | | equivalent one exists already. Used by append_states. | |
2fa6973e | 223 | `--------------------------------------------------------------*/ |
40675e7c | 224 | |
d57650a5 | 225 | static state_number_t |
a49aecd5 | 226 | get_state (symbol_number_t symbol, size_t core_size, item_number_t *core) |
40675e7c | 227 | { |
2fa6973e | 228 | int key; |
0c2d3f4c | 229 | size_t i; |
f693ad14 | 230 | state_t *sp; |
40675e7c | 231 | |
9bfe901c | 232 | if (trace_flag) |
c87d4863 | 233 | fprintf (stderr, "Entering get_state, state = %d, symbol = %d (%s)\n", |
6b98e4b5 AD |
234 | this_state->number, symbol, |
235 | symbol_tag_get (symbols[symbol])); | |
40675e7c | 236 | |
97db7bd4 AD |
237 | /* Add up the target state's active item numbers to get a hash key. |
238 | */ | |
40675e7c | 239 | key = 0; |
458be8e0 AD |
240 | for (i = 0; i < core_size; ++i) |
241 | key += core[i]; | |
f693ad14 AD |
242 | key = key % STATE_HASH_SIZE; |
243 | sp = state_hash[key]; | |
40675e7c DM |
244 | |
245 | if (sp) | |
246 | { | |
97db7bd4 | 247 | int found = 0; |
40675e7c DM |
248 | while (!found) |
249 | { | |
458be8e0 | 250 | if (sp->nitems == core_size) |
40675e7c DM |
251 | { |
252 | found = 1; | |
458be8e0 AD |
253 | for (i = 0; i < core_size; ++i) |
254 | if (core[i] != sp->items[i]) | |
97db7bd4 | 255 | found = 0; |
40675e7c DM |
256 | } |
257 | ||
258 | if (!found) | |
259 | { | |
260 | if (sp->link) | |
261 | { | |
262 | sp = sp->link; | |
263 | } | |
2fa6973e | 264 | else /* bucket exhausted and no match */ |
40675e7c | 265 | { |
458be8e0 | 266 | sp = sp->link = new_state (symbol, core_size, core); |
40675e7c DM |
267 | found = 1; |
268 | } | |
269 | } | |
270 | } | |
271 | } | |
2fa6973e | 272 | else /* bucket is empty */ |
40675e7c | 273 | { |
458be8e0 | 274 | state_hash[key] = sp = new_state (symbol, core_size, core); |
40675e7c DM |
275 | } |
276 | ||
c87d4863 AD |
277 | if (trace_flag) |
278 | fprintf (stderr, "Exiting get_state => %d\n", sp->number); | |
279 | ||
36281465 | 280 | return sp->number; |
40675e7c DM |
281 | } |
282 | ||
2fa6973e AD |
283 | /*------------------------------------------------------------------. |
284 | | Use the information computed by new_itemsets to find the state | | |
285 | | numbers reached by each shift transition from the current state. | | |
286 | | | | |
287 | | shiftset is set up as a vector of state numbers of those states. | | |
288 | `------------------------------------------------------------------*/ | |
40675e7c | 289 | |
2fa6973e AD |
290 | static void |
291 | append_states (void) | |
40675e7c | 292 | { |
2fa6973e AD |
293 | int i; |
294 | int j; | |
a49aecd5 | 295 | symbol_number_t symbol; |
40675e7c | 296 | |
9bfe901c | 297 | if (trace_flag) |
c87d4863 AD |
298 | fprintf (stderr, "Entering append_states, state = %d\n", |
299 | this_state->number); | |
40675e7c | 300 | |
2fa6973e | 301 | /* first sort shift_symbol into increasing order */ |
40675e7c | 302 | |
2fa6973e AD |
303 | for (i = 1; i < nshifts; i++) |
304 | { | |
305 | symbol = shift_symbol[i]; | |
306 | j = i; | |
307 | while (j > 0 && shift_symbol[j - 1] > symbol) | |
308 | { | |
309 | shift_symbol[j] = shift_symbol[j - 1]; | |
310 | j--; | |
311 | } | |
312 | shift_symbol[j] = symbol; | |
313 | } | |
40675e7c | 314 | |
2fa6973e | 315 | for (i = 0; i < nshifts; i++) |
458be8e0 AD |
316 | { |
317 | symbol = shift_symbol[i]; | |
318 | shiftset[i] = get_state (symbol, | |
319 | kernel_size[symbol], kernel_base[symbol]); | |
320 | } | |
40675e7c DM |
321 | } |
322 | ||
323 | ||
4a120d45 | 324 | static void |
2fa6973e | 325 | new_states (void) |
40675e7c | 326 | { |
643a5994 AD |
327 | /* The 0 at the lhs is the index of the item of this initial rule. */ |
328 | kernel_base[0][0] = 0; | |
329 | kernel_size[0] = 1; | |
458be8e0 | 330 | this_state = new_state (0, kernel_size[0], kernel_base[0]); |
40675e7c DM |
331 | } |
332 | ||
333 | ||
4a38e613 AD |
334 | /*------------------------------------------------------------. |
335 | | Save the NSHIFTS of SHIFTSET into the current linked list. | | |
336 | `------------------------------------------------------------*/ | |
337 | ||
4a120d45 | 338 | static void |
d2729d44 | 339 | save_shifts (void) |
40675e7c | 340 | { |
4a38e613 | 341 | shifts *p = shifts_new (nshifts); |
62a3e4f0 | 342 | memcpy (p->shifts, shiftset, nshifts * sizeof (shiftset[0])); |
80e25d4d | 343 | this_state->shifts = p; |
40675e7c DM |
344 | } |
345 | ||
346 | ||
2fa6973e AD |
347 | /*----------------------------------------------------------------. |
348 | | Find which rules can be used for reduction transitions from the | | |
349 | | current state and make a reductions structure for the state to | | |
350 | | record their rule numbers. | | |
351 | `----------------------------------------------------------------*/ | |
352 | ||
4a120d45 | 353 | static void |
2fa6973e | 354 | save_reductions (void) |
40675e7c | 355 | { |
30171f79 | 356 | int count = 0; |
fb908786 | 357 | int i; |
40675e7c | 358 | |
30171f79 AD |
359 | /* If this is the final state, we want it to have no reductions at |
360 | all, although it has one for `START_SYMBOL EOF .'. */ | |
d57650a5 | 361 | if (final_state && this_state->number == final_state->number) |
30171f79 | 362 | return; |
40675e7c | 363 | |
30171f79 | 364 | /* Find and count the active items that represent ends of rules. */ |
5123689b | 365 | for (i = 0; i < nritemset; ++i) |
2fa6973e | 366 | { |
fb908786 | 367 | int item = ritem[itemset[i]]; |
2fa6973e AD |
368 | if (item < 0) |
369 | redset[count++] = -item; | |
370 | } | |
40675e7c | 371 | |
2fa6973e | 372 | /* Make a reductions structure and copy the data into it. */ |
80dac38c | 373 | this_state->reductions = reductions_new (count); |
62a3e4f0 | 374 | memcpy (this_state->reductions->rules, redset, count * sizeof (redset[0])); |
2fa6973e AD |
375 | } |
376 | ||
377 | \f | |
82841af7 | 378 | /*---------------. |
29e88316 | 379 | | Build STATES. | |
82841af7 | 380 | `---------------*/ |
6a164e0c AD |
381 | |
382 | static void | |
29e88316 | 383 | set_states (void) |
6a164e0c | 384 | { |
2cec70b9 | 385 | state_t *sp; |
29e88316 | 386 | states = XCALLOC (state_t *, nstates); |
6a164e0c | 387 | |
2cec70b9 AD |
388 | for (sp = first_state; sp; sp = sp->next) |
389 | { | |
390 | /* Pessimization, but simplification of the code: make sure all | |
80dac38c AD |
391 | the states have a shifts, errs, and reductions, even if |
392 | reduced to 0. */ | |
2cec70b9 AD |
393 | if (!sp->shifts) |
394 | sp->shifts = shifts_new (0); | |
395 | if (!sp->errs) | |
396 | sp->errs = errs_new (0); | |
80dac38c AD |
397 | if (!sp->reductions) |
398 | sp->reductions = reductions_new (0); | |
2cec70b9 | 399 | |
29e88316 | 400 | states[sp->number] = sp; |
2cec70b9 | 401 | } |
6a164e0c AD |
402 | } |
403 | ||
2fa6973e AD |
404 | /*-------------------------------------------------------------------. |
405 | | Compute the nondeterministic finite state machine (see state.h for | | |
406 | | details) from the grammar. | | |
407 | `-------------------------------------------------------------------*/ | |
408 | ||
409 | void | |
410 | generate_states (void) | |
411 | { | |
412 | allocate_storage (); | |
9e7f6bbd | 413 | new_closure (nritems); |
2fa6973e AD |
414 | new_states (); |
415 | ||
416 | while (this_state) | |
417 | { | |
23cbcc6c AD |
418 | if (trace_flag) |
419 | fprintf (stderr, "Processing state %d (reached by %s)\n", | |
ad949da9 | 420 | this_state->number, |
6b98e4b5 | 421 | symbol_tag_get (symbols[this_state->accessing_symbol])); |
2fa6973e AD |
422 | /* Set up ruleset and itemset for the transitions out of this |
423 | state. ruleset gets a 1 bit for each rule that could reduce | |
424 | now. itemset gets a vector of all the items that could be | |
425 | accepted next. */ | |
426 | closure (this_state->items, this_state->nitems); | |
427 | /* record the reductions allowed out of this state */ | |
428 | save_reductions (); | |
429 | /* find the itemsets of the states that shifts can reach */ | |
430 | new_itemsets (); | |
431 | /* find or create the core structures for those states */ | |
432 | append_states (); | |
433 | ||
434 | /* create the shifts structures for the shifts to those states, | |
435 | now that the state numbers transitioning to are known */ | |
d954473d | 436 | save_shifts (); |
2fa6973e AD |
437 | |
438 | /* states are queued when they are created; process them all */ | |
439 | this_state = this_state->next; | |
440 | } | |
441 | ||
442 | /* discard various storage */ | |
443 | free_closure (); | |
444 | free_storage (); | |
445 | ||
29e88316 AD |
446 | /* Set up STATES. */ |
447 | set_states (); | |
40675e7c | 448 | } |