]>
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" |
9bfe901c | 29 | #include "getargs.h" |
c87d4863 | 30 | #include "reader.h" |
40675e7c DM |
31 | #include "gram.h" |
32 | #include "state.h" | |
a0f6b076 | 33 | #include "complain.h" |
2fa6973e | 34 | #include "closure.h" |
403b315b | 35 | #include "LR0.h" |
49701457 | 36 | #include "lalr.h" |
630e182b | 37 | #include "reduce.h" |
40675e7c | 38 | |
643a5994 | 39 | unsigned int nstates = 0; |
610ab194 AD |
40 | /* Initialize the final state to -1, otherwise, it might be set to 0 |
41 | by default, and since we don't compute the reductions of the final | |
42 | state, we end up not computing the reductions of the initial state, | |
43 | which is of course needed. | |
44 | ||
45 | FINAL_STATE is properly set by new_state when it recognizes the | |
46 | accessing symbol: EOF. */ | |
47 | int final_state = -1; | |
6a164e0c | 48 | static state_t *first_state = NULL; |
40675e7c | 49 | |
f693ad14 AD |
50 | static state_t *this_state = NULL; |
51 | static state_t *last_state = NULL; | |
40675e7c DM |
52 | |
53 | static int nshifts; | |
342b8b6e | 54 | static short *shift_symbol = NULL; |
40675e7c | 55 | |
342b8b6e AD |
56 | static short *redset = NULL; |
57 | static short *shiftset = NULL; | |
40675e7c | 58 | |
342b8b6e | 59 | static short **kernel_base = NULL; |
6255b435 | 60 | static int *kernel_size = NULL; |
342b8b6e | 61 | static short *kernel_items = NULL; |
40675e7c DM |
62 | |
63 | /* hash table for states, to recognize equivalent ones. */ | |
64 | ||
f693ad14 AD |
65 | #define STATE_HASH_SIZE 1009 |
66 | static state_t **state_hash = NULL; | |
40675e7c | 67 | |
2fa6973e | 68 | \f |
4a120d45 | 69 | static void |
d2729d44 | 70 | allocate_itemsets (void) |
40675e7c | 71 | { |
b4c4ccc2 AD |
72 | int i, r; |
73 | short *rhsp; | |
40675e7c | 74 | |
630e182b AD |
75 | /* Count the number of occurrences of all the symbols in RITEMS. |
76 | Note that useless productions (hence useless nonterminals) are | |
77 | browsed too, hence we need to allocate room for _all_ the | |
78 | symbols. */ | |
79 | int count = 0; | |
80 | short *symbol_count = XCALLOC (short, nsyms + nuseless_nonterminals); | |
40675e7c | 81 | |
b4c4ccc2 AD |
82 | for (r = 1; r < nrules + 1; ++r) |
83 | for (rhsp = rules[r].rhs; *rhsp >= 0; ++rhsp) | |
c87d4863 AD |
84 | { |
85 | count++; | |
b4c4ccc2 | 86 | symbol_count[*rhsp]++; |
c87d4863 | 87 | } |
40675e7c | 88 | |
2fa6973e AD |
89 | /* See comments before new_itemsets. All the vectors of items |
90 | live inside KERNEL_ITEMS. The number of active items after | |
40675e7c DM |
91 | some symbol cannot be more than the number of times that symbol |
92 | appears as an item, which is symbol_count[symbol]. | |
93 | We allocate that much space for each symbol. */ | |
94 | ||
d7913476 | 95 | kernel_base = XCALLOC (short *, nsyms); |
342b8b6e AD |
96 | if (count) |
97 | kernel_items = XCALLOC (short, count); | |
40675e7c DM |
98 | |
99 | count = 0; | |
100 | for (i = 0; i < nsyms; i++) | |
101 | { | |
102 | kernel_base[i] = kernel_items + count; | |
103 | count += symbol_count[i]; | |
104 | } | |
105 | ||
630e182b | 106 | free (symbol_count); |
0e41b407 | 107 | kernel_size = XCALLOC (int, nsyms); |
40675e7c DM |
108 | } |
109 | ||
110 | ||
4a120d45 | 111 | static void |
d2729d44 | 112 | allocate_storage (void) |
40675e7c | 113 | { |
2fa6973e | 114 | allocate_itemsets (); |
40675e7c | 115 | |
d7913476 AD |
116 | shiftset = XCALLOC (short, nsyms); |
117 | redset = XCALLOC (short, nrules + 1); | |
f693ad14 | 118 | state_hash = XCALLOC (state_t *, STATE_HASH_SIZE); |
375d5806 | 119 | shift_symbol = XCALLOC (short, nsyms); |
40675e7c DM |
120 | } |
121 | ||
122 | ||
4a120d45 | 123 | static void |
d2729d44 | 124 | free_storage (void) |
40675e7c | 125 | { |
630e182b AD |
126 | free (shift_symbol); |
127 | free (redset); | |
128 | free (shiftset); | |
129 | free (kernel_base); | |
130 | free (kernel_size); | |
d7913476 | 131 | XFREE (kernel_items); |
f693ad14 | 132 | free (state_hash); |
40675e7c DM |
133 | } |
134 | ||
135 | ||
136 | ||
40675e7c | 137 | |
2fa6973e AD |
138 | /*----------------------------------------------------------------. |
139 | | Find which symbols can be shifted in the current state, and for | | |
140 | | each one record which items would be active after that shift. | | |
141 | | Uses the contents of itemset. | | |
142 | | | | |
143 | | shift_symbol is set to a vector of the symbols that can be | | |
144 | | shifted. For each symbol in the grammar, kernel_base[symbol] | | |
145 | | points to a vector of item numbers activated if that symbol is | | |
125ecb56 | 146 | | shifted, and kernel_size[symbol] is their numbers. | |
2fa6973e | 147 | `----------------------------------------------------------------*/ |
40675e7c | 148 | |
4a120d45 | 149 | static void |
d2729d44 | 150 | new_itemsets (void) |
40675e7c | 151 | { |
2fa6973e | 152 | int i; |
2fa6973e | 153 | |
9bfe901c | 154 | if (trace_flag) |
c87d4863 AD |
155 | fprintf (stderr, "Entering new_itemsets, state = %d\n", |
156 | this_state->number); | |
40675e7c DM |
157 | |
158 | for (i = 0; i < nsyms; i++) | |
125ecb56 | 159 | kernel_size[i] = 0; |
40675e7c | 160 | |
b2872512 | 161 | nshifts = 0; |
40675e7c | 162 | |
b2872512 | 163 | for (i = 0; i < nitemset; ++i) |
40675e7c | 164 | { |
97db7bd4 | 165 | int symbol = ritem[itemset[i]]; |
75142d45 | 166 | if (symbol >= 0) |
40675e7c | 167 | { |
125ecb56 | 168 | if (!kernel_size[symbol]) |
40675e7c | 169 | { |
b2872512 AD |
170 | shift_symbol[nshifts] = symbol; |
171 | nshifts++; | |
40675e7c DM |
172 | } |
173 | ||
125ecb56 AD |
174 | kernel_base[symbol][kernel_size[symbol]] = itemset[i] + 1; |
175 | kernel_size[symbol]++; | |
40675e7c DM |
176 | } |
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 * |
2fa6973e | 188 | new_state (int symbol) |
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 | |
2fa6973e AD |
197 | if (nstates >= MAXSHORT) |
198 | fatal (_("too many states (max %d)"), MAXSHORT); | |
40675e7c | 199 | |
f693ad14 | 200 | p = STATE_ALLOC (kernel_size[symbol]); |
2fa6973e AD |
201 | p->accessing_symbol = symbol; |
202 | p->number = nstates; | |
125ecb56 | 203 | p->nitems = kernel_size[symbol]; |
2fa6973e | 204 | |
125ecb56 | 205 | shortcpy (p->items, kernel_base[symbol], kernel_size[symbol]); |
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 |
d2729d44 | 231 | get_state (int symbol) |
40675e7c | 232 | { |
2fa6973e | 233 | int key; |
97db7bd4 | 234 | int 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; |
125ecb56 | 245 | for (i = 0; i < kernel_size[symbol]; ++i) |
97db7bd4 | 246 | key += kernel_base[symbol][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 | { | |
125ecb56 | 255 | if (sp->nitems == kernel_size[symbol]) |
40675e7c DM |
256 | { |
257 | found = 1; | |
125ecb56 | 258 | for (i = 0; i < kernel_size[symbol]; ++i) |
97db7bd4 AD |
259 | if (kernel_base[symbol][i] != sp->items[i]) |
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 | { |
2fa6973e | 271 | sp = sp->link = new_state (symbol); |
40675e7c DM |
272 | found = 1; |
273 | } | |
274 | } | |
275 | } | |
276 | } | |
2fa6973e | 277 | else /* bucket is empty */ |
40675e7c | 278 | { |
f693ad14 | 279 | state_hash[key] = sp = new_state (symbol); |
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; | |
300 | int 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++) |
97db7bd4 | 321 | shiftset[i] = get_state (shift_symbol[i]); |
40675e7c DM |
322 | } |
323 | ||
324 | ||
4a120d45 | 325 | static void |
2fa6973e | 326 | new_states (void) |
40675e7c | 327 | { |
643a5994 AD |
328 | /* The 0 at the lhs is the index of the item of this initial rule. */ |
329 | kernel_base[0][0] = 0; | |
330 | kernel_size[0] = 1; | |
331 | this_state = new_state (0); | |
40675e7c DM |
332 | } |
333 | ||
334 | ||
4a38e613 AD |
335 | /*------------------------------------------------------------. |
336 | | Save the NSHIFTS of SHIFTSET into the current linked list. | | |
337 | `------------------------------------------------------------*/ | |
338 | ||
4a120d45 | 339 | static void |
d2729d44 | 340 | save_shifts (void) |
40675e7c | 341 | { |
4a38e613 | 342 | shifts *p = shifts_new (nshifts); |
300f275f | 343 | shortcpy (p->shifts, shiftset, nshifts); |
80e25d4d | 344 | this_state->shifts = p; |
40675e7c DM |
345 | } |
346 | ||
347 | ||
2fa6973e AD |
348 | /*----------------------------------------------------------------. |
349 | | Find which rules can be used for reduction transitions from the | | |
350 | | current state and make a reductions structure for the state to | | |
351 | | record their rule numbers. | | |
352 | `----------------------------------------------------------------*/ | |
353 | ||
4a120d45 | 354 | static void |
2fa6973e | 355 | save_reductions (void) |
40675e7c | 356 | { |
30171f79 | 357 | int count = 0; |
fb908786 | 358 | int i; |
40675e7c | 359 | |
30171f79 AD |
360 | /* If this is the final state, we want it to have no reductions at |
361 | all, although it has one for `START_SYMBOL EOF .'. */ | |
362 | if (this_state->number == final_state) | |
363 | return; | |
40675e7c | 364 | |
30171f79 | 365 | /* Find and count the active items that represent ends of rules. */ |
b2872512 | 366 | for (i = 0; i < nitemset; ++i) |
2fa6973e | 367 | { |
fb908786 | 368 | int item = ritem[itemset[i]]; |
2fa6973e AD |
369 | if (item < 0) |
370 | redset[count++] = -item; | |
371 | } | |
40675e7c | 372 | |
2fa6973e | 373 | /* Make a reductions structure and copy the data into it. */ |
80dac38c AD |
374 | this_state->reductions = reductions_new (count); |
375 | shortcpy (this_state->reductions->rules, redset, count); | |
2fa6973e AD |
376 | } |
377 | ||
378 | \f | |
82841af7 | 379 | /*---------------. |
29e88316 | 380 | | Build STATES. | |
82841af7 | 381 | `---------------*/ |
6a164e0c AD |
382 | |
383 | static void | |
29e88316 | 384 | set_states (void) |
6a164e0c | 385 | { |
2cec70b9 | 386 | state_t *sp; |
29e88316 | 387 | states = XCALLOC (state_t *, nstates); |
6a164e0c | 388 | |
2cec70b9 AD |
389 | for (sp = first_state; sp; sp = sp->next) |
390 | { | |
391 | /* Pessimization, but simplification of the code: make sure all | |
80dac38c AD |
392 | the states have a shifts, errs, and reductions, even if |
393 | reduced to 0. */ | |
2cec70b9 AD |
394 | if (!sp->shifts) |
395 | sp->shifts = shifts_new (0); | |
396 | if (!sp->errs) | |
397 | sp->errs = errs_new (0); | |
80dac38c AD |
398 | if (!sp->reductions) |
399 | sp->reductions = reductions_new (0); | |
2cec70b9 | 400 | |
29e88316 | 401 | states[sp->number] = sp; |
2cec70b9 | 402 | } |
6a164e0c AD |
403 | } |
404 | ||
2fa6973e AD |
405 | /*-------------------------------------------------------------------. |
406 | | Compute the nondeterministic finite state machine (see state.h for | | |
407 | | details) from the grammar. | | |
408 | `-------------------------------------------------------------------*/ | |
409 | ||
410 | void | |
411 | generate_states (void) | |
412 | { | |
413 | allocate_storage (); | |
9e7f6bbd | 414 | new_closure (nritems); |
2fa6973e AD |
415 | new_states (); |
416 | ||
417 | while (this_state) | |
418 | { | |
23cbcc6c AD |
419 | if (trace_flag) |
420 | fprintf (stderr, "Processing state %d (reached by %s)\n", | |
ad949da9 | 421 | this_state->number, |
8b3df748 AD |
422 | quotearg_style (escape_quoting_style, |
423 | symbols[this_state->accessing_symbol]->tag)); | |
2fa6973e AD |
424 | /* Set up ruleset and itemset for the transitions out of this |
425 | state. ruleset gets a 1 bit for each rule that could reduce | |
426 | now. itemset gets a vector of all the items that could be | |
427 | accepted next. */ | |
428 | closure (this_state->items, this_state->nitems); | |
429 | /* record the reductions allowed out of this state */ | |
430 | save_reductions (); | |
431 | /* find the itemsets of the states that shifts can reach */ | |
432 | new_itemsets (); | |
433 | /* find or create the core structures for those states */ | |
434 | append_states (); | |
435 | ||
436 | /* create the shifts structures for the shifts to those states, | |
437 | now that the state numbers transitioning to are known */ | |
d954473d | 438 | save_shifts (); |
2fa6973e AD |
439 | |
440 | /* states are queued when they are created; process them all */ | |
441 | this_state = this_state->next; | |
442 | } | |
443 | ||
444 | /* discard various storage */ | |
445 | free_closure (); | |
446 | free_storage (); | |
447 | ||
29e88316 AD |
448 | /* Set up STATES. */ |
449 | set_states (); | |
40675e7c | 450 | } |