]> git.saurik.com Git - bison.git/blame - src/LR0.c
The computation of nullable is broken: it doesn't handle empty
[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);
300f275f 319 shortcpy (p->shifts, shiftset, nshifts);
80e25d4d 320 this_state->shifts = p;
40675e7c
DM
321}
322
323
2fa6973e
AD
324/*------------------------------------------------------------------.
325| Subroutine of augment_automaton. Create the next-to-final state, |
326| to which a shift has already been made in the initial state. |
0ab3728b
AD
327| |
328| The task of this state consists in shifting (actually, it's a |
329| goto, but shifts and gotos are both stored in SHIFTS) the start |
330| symbols, hence the name. |
2fa6973e 331`------------------------------------------------------------------*/
40675e7c 332
4a120d45 333static void
0ab3728b 334insert_start_shifting_state (void)
40675e7c 335{
f693ad14 336 state_t *statep;
2fa6973e 337 shifts *sp;
40675e7c 338
f693ad14 339 statep = STATE_ALLOC (0);
0279f8e9 340 statep->number = nstates++;
0ab3728b
AD
341
342 /* The distinctive feature of this state from the
343 eof_shifting_state, is that it is labeled as post-start-symbol
344 shifting. I fail to understand why this state, and the
345 post-start-start can't be merged into one. But it does fail if
346 you try. --akim */
2fa6973e 347 statep->accessing_symbol = start_symbol;
40675e7c 348
2fa6973e
AD
349 last_state->next = statep;
350 last_state = statep;
40675e7c 351
2fa6973e 352 /* Make a shift from this state to (what will be) the final state. */
4a38e613 353 sp = shifts_new (1);
80e25d4d 354 statep->shifts = sp;
2fa6973e 355 sp->shifts[0] = nstates;
40675e7c
DM
356}
357
358
0ab3728b
AD
359/*-----------------------------------------------------------------.
360| Subroutine of augment_automaton. Create the final state, which |
361| shifts `0', the end of file. The initial state shifts the start |
362| symbol, and goes to here. |
363`-----------------------------------------------------------------*/
364
365static void
366insert_eof_shifting_state (void)
367{
368 state_t *statep;
369 shifts *sp;
370
371 /* Make the final state--the one that follows a shift from the
372 next-to-final state.
373 The symbol for that shift is 0 (end-of-file). */
374 statep = STATE_ALLOC (0);
0279f8e9 375 statep->number = nstates++;
0ab3728b
AD
376
377 last_state->next = statep;
378 last_state = statep;
379
380 /* Make the shift from the final state to the termination state. */
381 sp = shifts_new (1);
80e25d4d 382 statep->shifts = sp;
0ab3728b 383 sp->shifts[0] = nstates;
0ab3728b
AD
384}
385
386
387/*---------------------------------------------------------------.
388| Subroutine of augment_automaton. Create the accepting state. |
389`---------------------------------------------------------------*/
390
391static void
392insert_accepting_state (void)
393{
394 state_t *statep;
395
396 /* Note that the variable `final_state' refers to what we sometimes
397 call the termination state. */
398 final_state = nstates;
399
400 /* Make the termination state. */
401 statep = STATE_ALLOC (0);
402 statep->number = nstates++;
403 last_state->next = statep;
404 last_state = statep;
405}
406
407
408
409
410
2fa6973e
AD
411/*------------------------------------------------------------------.
412| Make sure that the initial state has a shift that accepts the |
413| grammar's start symbol and goes to the next-to-final state, which |
414| has a shift going to the final state, which has a shift to the |
415| termination state. Create such states and shifts if they don't |
416| happen to exist already. |
417`------------------------------------------------------------------*/
40675e7c 418
4a120d45 419static void
d2729d44 420augment_automaton (void)
40675e7c 421{
37c82725 422 if (!first_state->shifts->nshifts)
40675e7c 423 {
37c82725 424 /* The first state has no shifts. Make one shift, from the
b178c8cc 425 initial state to the next-to-final state. */
40675e7c 426
0ab3728b 427 shifts *sp = shifts_new (1);
80e25d4d 428 first_state->shifts = sp;
b178c8cc 429 sp->shifts[0] = nstates;
40675e7c 430
b178c8cc
AD
431 /* Create the next-to-final state, with shift to
432 what will be the final state. */
0ab3728b 433 insert_start_shifting_state ();
b178c8cc 434 }
37c82725 435 else
b178c8cc 436 {
0ab3728b 437 state_t *statep = first_state->next;
b178c8cc 438 /* The states reached by shifts from FIRST_STATE are numbered
74392f6a
AD
439 1..(SP->NSHIFTS). Look for one reached by START_SYMBOL.
440 This is typical of `start: start ... ;': there is a state
441 with the item `start: start . ...'. We want to add a `shift
442 on EOF to eof-shifting state here. */
0ab3728b 443 while (statep->accessing_symbol != start_symbol
2a73b93d 444 && statep->number < first_state->shifts->nshifts)
b178c8cc 445 statep = statep->next;
40675e7c 446
b178c8cc
AD
447 if (statep->accessing_symbol == start_symbol)
448 {
2a73b93d 449 /* We already have STATEP, a next-to-final state for `start:
74392f6a
AD
450 start . ...'. Make sure it has a shift to what will be
451 the final state. */
2a73b93d
AD
452 int i;
453
190c4f5f
AD
454 /* Find the shift of the inital state that leads to STATEP. */
455 shifts *sp = statep->shifts;
40675e7c 456
190c4f5f 457 shifts *sp1 = shifts_new (sp->nshifts + 1);
190c4f5f
AD
458 statep->shifts = sp1;
459 sp1->shifts[0] = nstates;
2a73b93d 460 for (i = sp->nshifts; i > 0; i--)
190c4f5f
AD
461 sp1->shifts[i] = sp->shifts[i - 1];
462
2a73b93d
AD
463 XFREE (sp);
464
74392f6a 465 insert_eof_shifting_state ();
40675e7c
DM
466 }
467 else
468 {
74392f6a 469 /* There is no state for `start: start . ...'. */
b178c8cc 470 int i, k;
2a73b93d 471 shifts *sp = first_state->shifts;
190c4f5f 472 shifts *sp1 = NULL;
40675e7c 473
190c4f5f
AD
474 /* Add one more shift to the initial state, going to the
475 next-to-final state (yet to be made). */
476 sp1 = shifts_new (sp->nshifts + 1);
477 first_state->shifts = sp1;
b178c8cc
AD
478 /* Stick this shift into the vector at the proper place. */
479 statep = first_state->next;
480 for (k = 0, i = 0; i < sp->nshifts; k++, i++)
481 {
482 if (statep->accessing_symbol > start_symbol && i == k)
190c4f5f
AD
483 sp1->shifts[k++] = nstates;
484 sp1->shifts[k] = sp->shifts[i];
b178c8cc
AD
485 statep = statep->next;
486 }
487 if (i == k)
190c4f5f 488 sp1->shifts[k++] = nstates;
b178c8cc
AD
489
490 XFREE (sp);
40675e7c 491
74392f6a
AD
492 /* Create the next-to-final state, with shift to what will
493 be the final state. Corresponds to `start: start . ...'. */
0ab3728b 494 insert_start_shifting_state ();
40675e7c
DM
495 }
496 }
40675e7c 497
0ab3728b 498 insert_accepting_state ();
40675e7c
DM
499}
500
501
2fa6973e
AD
502/*----------------------------------------------------------------.
503| Find which rules can be used for reduction transitions from the |
504| current state and make a reductions structure for the state to |
505| record their rule numbers. |
506`----------------------------------------------------------------*/
507
4a120d45 508static void
2fa6973e 509save_reductions (void)
40675e7c 510{
2fa6973e 511 int count;
fb908786 512 int i;
40675e7c 513
2fa6973e 514 /* Find and count the active items that represent ends of rules. */
40675e7c 515
2fa6973e 516 count = 0;
b2872512 517 for (i = 0; i < nitemset; ++i)
2fa6973e 518 {
fb908786 519 int item = ritem[itemset[i]];
2fa6973e
AD
520 if (item < 0)
521 redset[count++] = -item;
522 }
40675e7c 523
2fa6973e
AD
524 /* Make a reductions structure and copy the data into it. */
525
526 if (count)
527 {
fb908786 528 reductions *p = REDUCTIONS_ALLOC (count);
2fa6973e 529 p->nreds = count;
300f275f 530 shortcpy (p->rules, redset, count);
2fa6973e 531
49701457 532 this_state->reductions = p;
2fa6973e
AD
533 }
534}
535
536\f
6a164e0c
AD
537/*--------------------.
538| Build STATE_TABLE. |
539`--------------------*/
540
541static void
542set_state_table (void)
543{
544 /* NSTATES + 1 because lookahead for the pseudo state number NSTATES
545 might be used (see conflicts.c). It is too opaque for me to
546 provide a probably less hacky implementation. --akim */
547 state_table = XCALLOC (state_t *, nstates + 1);
548
549 {
550 state_t *sp;
551 for (sp = first_state; sp; sp = sp->next)
552 state_table[sp->number] = sp;
553 }
554
555 /* Pessimization, but simplification of the code: make sure all the
556 states have a shifts, even if reduced to 0 shifts. */
557 {
558 int i;
559 for (i = 0; i < nstates; i++)
560 if (!state_table[i]->shifts)
561 state_table[i]->shifts = shifts_new (0);
562 }
6a164e0c
AD
563}
564
2fa6973e
AD
565/*-------------------------------------------------------------------.
566| Compute the nondeterministic finite state machine (see state.h for |
567| details) from the grammar. |
568`-------------------------------------------------------------------*/
569
570void
571generate_states (void)
572{
573 allocate_storage ();
574 new_closure (nitems);
575 new_states ();
576
577 while (this_state)
578 {
23cbcc6c
AD
579 if (trace_flag)
580 fprintf (stderr, "Processing state %d (reached by %s)\n",
581 this_state->number, tags[this_state->accessing_symbol]);
2fa6973e
AD
582 /* Set up ruleset and itemset for the transitions out of this
583 state. ruleset gets a 1 bit for each rule that could reduce
584 now. itemset gets a vector of all the items that could be
585 accepted next. */
586 closure (this_state->items, this_state->nitems);
587 /* record the reductions allowed out of this state */
588 save_reductions ();
589 /* find the itemsets of the states that shifts can reach */
590 new_itemsets ();
591 /* find or create the core structures for those states */
592 append_states ();
593
594 /* create the shifts structures for the shifts to those states,
595 now that the state numbers transitioning to are known */
d954473d 596 save_shifts ();
2fa6973e
AD
597
598 /* states are queued when they are created; process them all */
599 this_state = this_state->next;
600 }
601
602 /* discard various storage */
603 free_closure ();
604 free_storage ();
605
606 /* set up initial and final states as parser wants them */
607 augment_automaton ();
6a164e0c
AD
608
609 /* Set up STATE_TABLE. */
610 set_state_table ();
40675e7c 611}