]> git.saurik.com Git - bison.git/blame - src/LR0.c
Now that states have a complete set of members, the linked list of
[bison.git] / src / LR0.c
CommitLineData
40675e7c 1/* Generate the nondeterministic finite state machine for bison,
97db7bd4 2 Copyright 1984, 1986, 1989, 2000, 2001 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"
9bfe901c 26#include "getargs.h"
c87d4863 27#include "reader.h"
40675e7c
DM
28#include "gram.h"
29#include "state.h"
a0f6b076 30#include "complain.h"
2fa6973e 31#include "closure.h"
403b315b 32#include "LR0.h"
49701457 33#include "lalr.h"
630e182b 34#include "reduce.h"
40675e7c 35
40675e7c
DM
36int nstates;
37int final_state;
6a164e0c 38static state_t *first_state = NULL;
40675e7c 39
f693ad14
AD
40static state_t *this_state = NULL;
41static state_t *last_state = NULL;
40675e7c
DM
42
43static int nshifts;
342b8b6e 44static short *shift_symbol = NULL;
40675e7c 45
342b8b6e
AD
46static short *redset = NULL;
47static short *shiftset = NULL;
40675e7c 48
342b8b6e 49static short **kernel_base = NULL;
6255b435 50static int *kernel_size = NULL;
342b8b6e 51static short *kernel_items = NULL;
40675e7c
DM
52
53/* hash table for states, to recognize equivalent ones. */
54
f693ad14
AD
55#define STATE_HASH_SIZE 1009
56static state_t **state_hash = NULL;
40675e7c 57
2fa6973e 58\f
4a120d45 59static void
d2729d44 60allocate_itemsets (void)
40675e7c 61{
2fa6973e 62 int i;
40675e7c 63
630e182b
AD
64 /* Count the number of occurrences of all the symbols in RITEMS.
65 Note that useless productions (hence useless nonterminals) are
66 browsed too, hence we need to allocate room for _all_ the
67 symbols. */
68 int count = 0;
69 short *symbol_count = XCALLOC (short, nsyms + nuseless_nonterminals);
40675e7c 70
c87d4863
AD
71 for (i = 0; ritem[i]; ++i)
72 if (ritem[i] > 0)
73 {
74 count++;
75 symbol_count[ritem[i]]++;
76 }
40675e7c 77
2fa6973e
AD
78 /* See comments before new_itemsets. All the vectors of items
79 live inside KERNEL_ITEMS. The number of active items after
40675e7c
DM
80 some symbol cannot be more than the number of times that symbol
81 appears as an item, which is symbol_count[symbol].
82 We allocate that much space for each symbol. */
83
d7913476 84 kernel_base = XCALLOC (short *, nsyms);
342b8b6e
AD
85 if (count)
86 kernel_items = XCALLOC (short, count);
40675e7c
DM
87
88 count = 0;
89 for (i = 0; i < nsyms; i++)
90 {
91 kernel_base[i] = kernel_items + count;
92 count += symbol_count[i];
93 }
94
630e182b 95 free (symbol_count);
0e41b407 96 kernel_size = XCALLOC (int, nsyms);
40675e7c
DM
97}
98
99
4a120d45 100static void
d2729d44 101allocate_storage (void)
40675e7c 102{
2fa6973e 103 allocate_itemsets ();
40675e7c 104
d7913476
AD
105 shiftset = XCALLOC (short, nsyms);
106 redset = XCALLOC (short, nrules + 1);
f693ad14 107 state_hash = XCALLOC (state_t *, STATE_HASH_SIZE);
40675e7c
DM
108}
109
110
4a120d45 111static void
d2729d44 112free_storage (void)
40675e7c 113{
630e182b
AD
114 free (shift_symbol);
115 free (redset);
116 free (shiftset);
117 free (kernel_base);
118 free (kernel_size);
d7913476 119 XFREE (kernel_items);
f693ad14 120 free (state_hash);
40675e7c
DM
121}
122
123
124
40675e7c 125
2fa6973e
AD
126/*----------------------------------------------------------------.
127| Find which symbols can be shifted in the current state, and for |
128| each one record which items would be active after that shift. |
129| Uses the contents of itemset. |
130| |
131| shift_symbol is set to a vector of the symbols that can be |
132| shifted. For each symbol in the grammar, kernel_base[symbol] |
133| points to a vector of item numbers activated if that symbol is |
125ecb56 134| shifted, and kernel_size[symbol] is their numbers. |
2fa6973e 135`----------------------------------------------------------------*/
40675e7c 136
4a120d45 137static void
d2729d44 138new_itemsets (void)
40675e7c 139{
2fa6973e 140 int i;
2fa6973e 141
9bfe901c 142 if (trace_flag)
c87d4863
AD
143 fprintf (stderr, "Entering new_itemsets, state = %d\n",
144 this_state->number);
40675e7c
DM
145
146 for (i = 0; i < nsyms; i++)
125ecb56 147 kernel_size[i] = 0;
40675e7c 148
630e182b 149 shift_symbol = XCALLOC (short, nsyms);
b2872512 150 nshifts = 0;
40675e7c 151
b2872512 152 for (i = 0; i < nitemset; ++i)
40675e7c 153 {
97db7bd4 154 int symbol = ritem[itemset[i]];
40675e7c
DM
155 if (symbol > 0)
156 {
125ecb56 157 if (!kernel_size[symbol])
40675e7c 158 {
b2872512
AD
159 shift_symbol[nshifts] = symbol;
160 nshifts++;
40675e7c
DM
161 }
162
125ecb56
AD
163 kernel_base[symbol][kernel_size[symbol]] = itemset[i] + 1;
164 kernel_size[symbol]++;
40675e7c
DM
165 }
166 }
40675e7c
DM
167}
168
169
170
2fa6973e
AD
171/*-----------------------------------------------------------------.
172| Subroutine of get_state. Create a new state for those items, if |
173| necessary. |
174`-----------------------------------------------------------------*/
40675e7c 175
f693ad14 176static state_t *
2fa6973e 177new_state (int symbol)
40675e7c 178{
f693ad14 179 state_t *p;
40675e7c 180
9bfe901c 181 if (trace_flag)
c87d4863
AD
182 fprintf (stderr, "Entering new_state, state = %d, symbol = %d (%s)\n",
183 this_state->number, symbol, tags[symbol]);
40675e7c 184
2fa6973e
AD
185 if (nstates >= MAXSHORT)
186 fatal (_("too many states (max %d)"), MAXSHORT);
40675e7c 187
f693ad14 188 p = STATE_ALLOC (kernel_size[symbol]);
2fa6973e
AD
189 p->accessing_symbol = symbol;
190 p->number = nstates;
125ecb56 191 p->nitems = kernel_size[symbol];
2fa6973e 192
125ecb56 193 shortcpy (p->items, kernel_base[symbol], kernel_size[symbol]);
2fa6973e
AD
194
195 last_state->next = p;
196 last_state = p;
2fa6973e 197 nstates++;
40675e7c 198
2fa6973e
AD
199 return p;
200}
40675e7c 201
2fa6973e
AD
202
203/*--------------------------------------------------------------.
204| Find the state number for the state we would get to (from the |
205| current state) by shifting symbol. Create a new state if no |
97db7bd4 206| equivalent one exists already. Used by append_states. |
2fa6973e 207`--------------------------------------------------------------*/
40675e7c 208
4a120d45 209static int
d2729d44 210get_state (int symbol)
40675e7c 211{
2fa6973e 212 int key;
97db7bd4 213 int i;
f693ad14 214 state_t *sp;
40675e7c 215
9bfe901c 216 if (trace_flag)
c87d4863
AD
217 fprintf (stderr, "Entering get_state, state = %d, symbol = %d (%s)\n",
218 this_state->number, symbol, tags[symbol]);
40675e7c 219
97db7bd4
AD
220 /* Add up the target state's active item numbers to get a hash key.
221 */
40675e7c 222 key = 0;
125ecb56 223 for (i = 0; i < kernel_size[symbol]; ++i)
97db7bd4 224 key += kernel_base[symbol][i];
f693ad14
AD
225 key = key % STATE_HASH_SIZE;
226 sp = state_hash[key];
40675e7c
DM
227
228 if (sp)
229 {
97db7bd4 230 int found = 0;
40675e7c
DM
231 while (!found)
232 {
125ecb56 233 if (sp->nitems == kernel_size[symbol])
40675e7c
DM
234 {
235 found = 1;
125ecb56 236 for (i = 0; i < kernel_size[symbol]; ++i)
97db7bd4
AD
237 if (kernel_base[symbol][i] != sp->items[i])
238 found = 0;
40675e7c
DM
239 }
240
241 if (!found)
242 {
243 if (sp->link)
244 {
245 sp = sp->link;
246 }
2fa6973e 247 else /* bucket exhausted and no match */
40675e7c 248 {
2fa6973e 249 sp = sp->link = new_state (symbol);
40675e7c
DM
250 found = 1;
251 }
252 }
253 }
254 }
2fa6973e 255 else /* bucket is empty */
40675e7c 256 {
f693ad14 257 state_hash[key] = sp = new_state (symbol);
40675e7c
DM
258 }
259
c87d4863
AD
260 if (trace_flag)
261 fprintf (stderr, "Exiting get_state => %d\n", sp->number);
262
36281465 263 return sp->number;
40675e7c
DM
264}
265
2fa6973e
AD
266/*------------------------------------------------------------------.
267| Use the information computed by new_itemsets to find the state |
268| numbers reached by each shift transition from the current state. |
269| |
270| shiftset is set up as a vector of state numbers of those states. |
271`------------------------------------------------------------------*/
40675e7c 272
2fa6973e
AD
273static void
274append_states (void)
40675e7c 275{
2fa6973e
AD
276 int i;
277 int j;
278 int symbol;
40675e7c 279
9bfe901c 280 if (trace_flag)
c87d4863
AD
281 fprintf (stderr, "Entering append_states, state = %d\n",
282 this_state->number);
40675e7c 283
2fa6973e 284 /* first sort shift_symbol into increasing order */
40675e7c 285
2fa6973e
AD
286 for (i = 1; i < nshifts; i++)
287 {
288 symbol = shift_symbol[i];
289 j = i;
290 while (j > 0 && shift_symbol[j - 1] > symbol)
291 {
292 shift_symbol[j] = shift_symbol[j - 1];
293 j--;
294 }
295 shift_symbol[j] = symbol;
296 }
40675e7c 297
2fa6973e 298 for (i = 0; i < nshifts; i++)
97db7bd4 299 shiftset[i] = get_state (shift_symbol[i]);
40675e7c
DM
300}
301
302
4a120d45 303static void
2fa6973e 304new_states (void)
40675e7c 305{
f693ad14 306 first_state = last_state = this_state = STATE_ALLOC (0);
40675e7c
DM
307 nstates = 1;
308}
309
310
4a38e613
AD
311/*------------------------------------------------------------.
312| Save the NSHIFTS of SHIFTSET into the current linked list. |
313`------------------------------------------------------------*/
314
4a120d45 315static void
d2729d44 316save_shifts (void)
40675e7c 317{
4a38e613 318 shifts *p = shifts_new (nshifts);
40675e7c
DM
319
320 p->number = this_state->number;
300f275f 321 shortcpy (p->shifts, shiftset, nshifts);
80e25d4d 322 this_state->shifts = p;
40675e7c
DM
323}
324
325
2fa6973e
AD
326/*------------------------------------------------------------------.
327| Subroutine of augment_automaton. Create the next-to-final state, |
328| to which a shift has already been made in the initial state. |
0ab3728b
AD
329| |
330| The task of this state consists in shifting (actually, it's a |
331| goto, but shifts and gotos are both stored in SHIFTS) the start |
332| symbols, hence the name. |
2fa6973e 333`------------------------------------------------------------------*/
40675e7c 334
4a120d45 335static void
0ab3728b 336insert_start_shifting_state (void)
40675e7c 337{
f693ad14 338 state_t *statep;
2fa6973e 339 shifts *sp;
40675e7c 340
f693ad14 341 statep = STATE_ALLOC (0);
2fa6973e 342 statep->number = nstates;
0ab3728b
AD
343
344 /* The distinctive feature of this state from the
345 eof_shifting_state, is that it is labeled as post-start-symbol
346 shifting. I fail to understand why this state, and the
347 post-start-start can't be merged into one. But it does fail if
348 you try. --akim */
2fa6973e 349 statep->accessing_symbol = start_symbol;
40675e7c 350
2fa6973e
AD
351 last_state->next = statep;
352 last_state = statep;
40675e7c 353
2fa6973e 354 /* Make a shift from this state to (what will be) the final state. */
4a38e613 355 sp = shifts_new (1);
80e25d4d 356 statep->shifts = sp;
2fa6973e 357 sp->number = nstates++;
2fa6973e 358 sp->shifts[0] = nstates;
40675e7c
DM
359}
360
361
0ab3728b
AD
362/*-----------------------------------------------------------------.
363| Subroutine of augment_automaton. Create the final state, which |
364| shifts `0', the end of file. The initial state shifts the start |
365| symbol, and goes to here. |
366`-----------------------------------------------------------------*/
367
368static void
369insert_eof_shifting_state (void)
370{
371 state_t *statep;
372 shifts *sp;
373
374 /* Make the final state--the one that follows a shift from the
375 next-to-final state.
376 The symbol for that shift is 0 (end-of-file). */
377 statep = STATE_ALLOC (0);
378 statep->number = nstates;
379
380 last_state->next = statep;
381 last_state = statep;
382
383 /* Make the shift from the final state to the termination state. */
384 sp = shifts_new (1);
80e25d4d 385 statep->shifts = sp;
0ab3728b
AD
386 sp->number = nstates++;
387 sp->shifts[0] = nstates;
0ab3728b
AD
388}
389
390
391/*---------------------------------------------------------------.
392| Subroutine of augment_automaton. Create the accepting state. |
393`---------------------------------------------------------------*/
394
395static void
396insert_accepting_state (void)
397{
398 state_t *statep;
399
400 /* Note that the variable `final_state' refers to what we sometimes
401 call the termination state. */
402 final_state = nstates;
403
404 /* Make the termination state. */
405 statep = STATE_ALLOC (0);
406 statep->number = nstates++;
407 last_state->next = statep;
408 last_state = statep;
409}
410
411
412
413
414
2fa6973e
AD
415/*------------------------------------------------------------------.
416| Make sure that the initial state has a shift that accepts the |
417| grammar's start symbol and goes to the next-to-final state, which |
418| has a shift going to the final state, which has a shift to the |
419| termination state. Create such states and shifts if they don't |
420| happen to exist already. |
421`------------------------------------------------------------------*/
40675e7c 422
4a120d45 423static void
d2729d44 424augment_automaton (void)
40675e7c 425{
37c82725 426 if (!first_state->shifts->nshifts)
40675e7c 427 {
37c82725 428 /* The first state has no shifts. Make one shift, from the
b178c8cc 429 initial state to the next-to-final state. */
40675e7c 430
0ab3728b 431 shifts *sp = shifts_new (1);
80e25d4d 432 first_state->shifts = sp;
b178c8cc 433 sp->shifts[0] = nstates;
40675e7c 434
b178c8cc
AD
435 /* Create the next-to-final state, with shift to
436 what will be the final state. */
0ab3728b 437 insert_start_shifting_state ();
b178c8cc 438 }
37c82725 439 else
b178c8cc 440 {
0ab3728b 441 state_t *statep = first_state->next;
b178c8cc 442 /* The states reached by shifts from FIRST_STATE are numbered
74392f6a
AD
443 1..(SP->NSHIFTS). Look for one reached by START_SYMBOL.
444 This is typical of `start: start ... ;': there is a state
445 with the item `start: start . ...'. We want to add a `shift
446 on EOF to eof-shifting state here. */
0ab3728b 447 while (statep->accessing_symbol != start_symbol
2a73b93d 448 && statep->number < first_state->shifts->nshifts)
b178c8cc 449 statep = statep->next;
40675e7c 450
b178c8cc
AD
451 if (statep->accessing_symbol == start_symbol)
452 {
2a73b93d 453 /* We already have STATEP, a next-to-final state for `start:
74392f6a
AD
454 start . ...'. Make sure it has a shift to what will be
455 the final state. */
2a73b93d
AD
456 int i;
457
190c4f5f
AD
458 /* Find the shift of the inital state that leads to STATEP. */
459 shifts *sp = statep->shifts;
40675e7c 460
190c4f5f
AD
461 shifts *sp1 = shifts_new (sp->nshifts + 1);
462 sp1->number = statep->number;
463 statep->shifts = sp1;
464 sp1->shifts[0] = nstates;
2a73b93d 465 for (i = sp->nshifts; i > 0; i--)
190c4f5f
AD
466 sp1->shifts[i] = sp->shifts[i - 1];
467
2a73b93d
AD
468 XFREE (sp);
469
74392f6a 470 insert_eof_shifting_state ();
40675e7c
DM
471 }
472 else
473 {
74392f6a 474 /* There is no state for `start: start . ...'. */
b178c8cc 475 int i, k;
2a73b93d 476 shifts *sp = first_state->shifts;
190c4f5f 477 shifts *sp1 = NULL;
40675e7c 478
190c4f5f
AD
479 /* Add one more shift to the initial state, going to the
480 next-to-final state (yet to be made). */
481 sp1 = shifts_new (sp->nshifts + 1);
482 first_state->shifts = sp1;
b178c8cc
AD
483 /* Stick this shift into the vector at the proper place. */
484 statep = first_state->next;
485 for (k = 0, i = 0; i < sp->nshifts; k++, i++)
486 {
487 if (statep->accessing_symbol > start_symbol && i == k)
190c4f5f
AD
488 sp1->shifts[k++] = nstates;
489 sp1->shifts[k] = sp->shifts[i];
b178c8cc
AD
490 statep = statep->next;
491 }
492 if (i == k)
190c4f5f 493 sp1->shifts[k++] = nstates;
b178c8cc
AD
494
495 XFREE (sp);
40675e7c 496
74392f6a
AD
497 /* Create the next-to-final state, with shift to what will
498 be the final state. Corresponds to `start: start . ...'. */
0ab3728b 499 insert_start_shifting_state ();
40675e7c
DM
500 }
501 }
40675e7c 502
0ab3728b 503 insert_accepting_state ();
40675e7c
DM
504}
505
506
2fa6973e
AD
507/*----------------------------------------------------------------.
508| Find which rules can be used for reduction transitions from the |
509| current state and make a reductions structure for the state to |
510| record their rule numbers. |
511`----------------------------------------------------------------*/
512
4a120d45 513static void
2fa6973e 514save_reductions (void)
40675e7c 515{
2fa6973e 516 int count;
fb908786 517 int i;
40675e7c 518
2fa6973e 519 /* Find and count the active items that represent ends of rules. */
40675e7c 520
2fa6973e 521 count = 0;
b2872512 522 for (i = 0; i < nitemset; ++i)
2fa6973e 523 {
fb908786 524 int item = ritem[itemset[i]];
2fa6973e
AD
525 if (item < 0)
526 redset[count++] = -item;
527 }
40675e7c 528
2fa6973e
AD
529 /* Make a reductions structure and copy the data into it. */
530
531 if (count)
532 {
fb908786 533 reductions *p = REDUCTIONS_ALLOC (count);
2fa6973e 534 p->nreds = count;
300f275f 535 shortcpy (p->rules, redset, count);
2fa6973e 536
49701457 537 this_state->reductions = p;
2fa6973e
AD
538 }
539}
540
541\f
6a164e0c
AD
542/*--------------------.
543| Build STATE_TABLE. |
544`--------------------*/
545
546static void
547set_state_table (void)
548{
549 /* NSTATES + 1 because lookahead for the pseudo state number NSTATES
550 might be used (see conflicts.c). It is too opaque for me to
551 provide a probably less hacky implementation. --akim */
552 state_table = XCALLOC (state_t *, nstates + 1);
553
554 {
555 state_t *sp;
556 for (sp = first_state; sp; sp = sp->next)
557 state_table[sp->number] = sp;
558 }
559
560 /* Pessimization, but simplification of the code: make sure all the
561 states have a shifts, even if reduced to 0 shifts. */
562 {
563 int i;
564 for (i = 0; i < nstates; i++)
565 if (!state_table[i]->shifts)
566 state_table[i]->shifts = shifts_new (0);
567 }
568
569 /* Initializing the lookaheads members. Please note that it must be
570 performed after having set some of the other members which are
571 used below. Change with extreme caution. */
572 {
573 int i;
574 int count = 0;
575 for (i = 0; i < nstates; i++)
576 {
577 int k;
578 reductions *rp = state_table[i]->reductions;
579 shifts *sp = state_table[i]->shifts;
580
581 state_table[i]->lookaheads = count;
582
583 if (rp
584 && (rp->nreds > 1 || (sp->nshifts && SHIFT_IS_SHIFT (sp, 0))))
585 count += rp->nreds;
586 else
587 state_table[i]->consistent = 1;
588
589 for (k = 0; k < sp->nshifts; k++)
590 if (SHIFT_IS_ERROR (sp, k))
591 {
592 state_table[i]->consistent = 0;
593 break;
594 }
595 }
596
597 /* Seems to be needed by conflicts.c. */
598 state_table[nstates] = STATE_ALLOC (0);
599 state_table[nstates]->lookaheads = count;
600 }
601}
602
2fa6973e
AD
603/*-------------------------------------------------------------------.
604| Compute the nondeterministic finite state machine (see state.h for |
605| details) from the grammar. |
606`-------------------------------------------------------------------*/
607
608void
609generate_states (void)
610{
611 allocate_storage ();
612 new_closure (nitems);
613 new_states ();
614
615 while (this_state)
616 {
23cbcc6c
AD
617 if (trace_flag)
618 fprintf (stderr, "Processing state %d (reached by %s)\n",
619 this_state->number, tags[this_state->accessing_symbol]);
2fa6973e
AD
620 /* Set up ruleset and itemset for the transitions out of this
621 state. ruleset gets a 1 bit for each rule that could reduce
622 now. itemset gets a vector of all the items that could be
623 accepted next. */
624 closure (this_state->items, this_state->nitems);
625 /* record the reductions allowed out of this state */
626 save_reductions ();
627 /* find the itemsets of the states that shifts can reach */
628 new_itemsets ();
629 /* find or create the core structures for those states */
630 append_states ();
631
632 /* create the shifts structures for the shifts to those states,
633 now that the state numbers transitioning to are known */
d954473d 634 save_shifts ();
2fa6973e
AD
635
636 /* states are queued when they are created; process them all */
637 this_state = this_state->next;
638 }
639
640 /* discard various storage */
641 free_closure ();
642 free_storage ();
643
644 /* set up initial and final states as parser wants them */
645 augment_automaton ();
6a164e0c
AD
646
647 /* Set up STATE_TABLE. */
648 set_state_table ();
40675e7c 649}