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