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