]> git.saurik.com Git - bison.git/blob - src/LR0.c
* src/conflicts.c (print_reductions): Pessimize, but clarify.
[bison.git] / src / LR0.c
1 /* Generate the nondeterministic finite state machine for bison,
2 Copyright 1984, 1986, 1989, 2000, 2001 Free Software Foundation, Inc.
3
4 This file is part of Bison, the GNU Compiler Compiler.
5
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.
10
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.
15
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. */
20
21
22 /* See comments in state.h for the data structures that represent it.
23 The entry point is generate_states. */
24
25 #include "system.h"
26 #include "getargs.h"
27 #include "reader.h"
28 #include "gram.h"
29 #include "state.h"
30 #include "complain.h"
31 #include "closure.h"
32 #include "LR0.h"
33 #include "reduce.h"
34
35 int nstates;
36 int final_state;
37 core *first_state = NULL;
38 shifts *first_shift = NULL;
39 reductions *first_reduction = NULL;
40
41 static core *this_state = NULL;
42 static core *last_state = NULL;
43 static shifts *last_shift = NULL;
44 static reductions *last_reduction = NULL;
45
46 static int nshifts;
47 static short *shift_symbol = NULL;
48
49 static short *redset = NULL;
50 static short *shiftset = NULL;
51
52 static short **kernel_base = NULL;
53 static int *kernel_size = NULL;
54 static short *kernel_items = NULL;
55
56 /* hash table for states, to recognize equivalent ones. */
57
58 #define STATE_TABLE_SIZE 1009
59 static core **state_table = NULL;
60
61 \f
62 static void
63 allocate_itemsets (void)
64 {
65 int i;
66
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);
73
74 for (i = 0; ritem[i]; ++i)
75 if (ritem[i] > 0)
76 {
77 count++;
78 symbol_count[ritem[i]]++;
79 }
80
81 /* See comments before new_itemsets. All the vectors of items
82 live inside KERNEL_ITEMS. The number of active items after
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
87 kernel_base = XCALLOC (short *, nsyms);
88 if (count)
89 kernel_items = XCALLOC (short, count);
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
98 free (symbol_count);
99 kernel_size = XCALLOC (int, nsyms);
100 }
101
102
103 static void
104 allocate_storage (void)
105 {
106 allocate_itemsets ();
107
108 shiftset = XCALLOC (short, nsyms);
109 redset = XCALLOC (short, nrules + 1);
110 state_table = XCALLOC (core *, STATE_TABLE_SIZE);
111 }
112
113
114 static void
115 free_storage (void)
116 {
117 free (shift_symbol);
118 free (redset);
119 free (shiftset);
120 free (kernel_base);
121 free (kernel_size);
122 XFREE (kernel_items);
123 free (state_table);
124 }
125
126
127
128
129 /*----------------------------------------------------------------.
130 | Find which symbols can be shifted in the current state, and for |
131 | each one record which items would be active after that shift. |
132 | Uses the contents of itemset. |
133 | |
134 | shift_symbol is set to a vector of the symbols that can be |
135 | shifted. For each symbol in the grammar, kernel_base[symbol] |
136 | points to a vector of item numbers activated if that symbol is |
137 | shifted, and kernel_size[symbol] is their numbers. |
138 `----------------------------------------------------------------*/
139
140 static void
141 new_itemsets (void)
142 {
143 int i;
144 int shiftcount;
145
146 if (trace_flag)
147 fprintf (stderr, "Entering new_itemsets, state = %d\n",
148 this_state->number);
149
150 for (i = 0; i < nsyms; i++)
151 kernel_size[i] = 0;
152
153 shift_symbol = XCALLOC (short, nsyms);
154 shiftcount = 0;
155
156 for (i = 0; i < itemsetsize; ++i)
157 {
158 int symbol = ritem[itemset[i]];
159 if (symbol > 0)
160 {
161 if (!kernel_size[symbol])
162 {
163 shift_symbol[shiftcount] = symbol;
164 shiftcount++;
165 }
166
167 kernel_base[symbol][kernel_size[symbol]] = itemset[i] + 1;
168 kernel_size[symbol]++;
169 }
170 }
171
172 nshifts = shiftcount;
173 }
174
175
176
177 /*-----------------------------------------------------------------.
178 | Subroutine of get_state. Create a new state for those items, if |
179 | necessary. |
180 `-----------------------------------------------------------------*/
181
182 static core *
183 new_state (int symbol)
184 {
185 core *p;
186
187 if (trace_flag)
188 fprintf (stderr, "Entering new_state, state = %d, symbol = %d (%s)\n",
189 this_state->number, symbol, tags[symbol]);
190
191 if (nstates >= MAXSHORT)
192 fatal (_("too many states (max %d)"), MAXSHORT);
193
194 p = CORE_ALLOC (kernel_size[symbol]);
195 p->accessing_symbol = symbol;
196 p->number = nstates;
197 p->nitems = kernel_size[symbol];
198
199 shortcpy (p->items, kernel_base[symbol], kernel_size[symbol]);
200
201 last_state->next = p;
202 last_state = p;
203 nstates++;
204
205 return p;
206 }
207
208
209 /*--------------------------------------------------------------.
210 | Find the state number for the state we would get to (from the |
211 | current state) by shifting symbol. Create a new state if no |
212 | equivalent one exists already. Used by append_states. |
213 `--------------------------------------------------------------*/
214
215 static int
216 get_state (int symbol)
217 {
218 int key;
219 int i;
220 core *sp;
221
222 if (trace_flag)
223 fprintf (stderr, "Entering get_state, state = %d, symbol = %d (%s)\n",
224 this_state->number, symbol, tags[symbol]);
225
226 /* Add up the target state's active item numbers to get a hash key.
227 */
228 key = 0;
229 for (i = 0; i < kernel_size[symbol]; ++i)
230 key += kernel_base[symbol][i];
231 key = key % STATE_TABLE_SIZE;
232 sp = state_table[key];
233
234 if (sp)
235 {
236 int found = 0;
237 while (!found)
238 {
239 if (sp->nitems == kernel_size[symbol])
240 {
241 found = 1;
242 for (i = 0; i < kernel_size[symbol]; ++i)
243 if (kernel_base[symbol][i] != sp->items[i])
244 found = 0;
245 }
246
247 if (!found)
248 {
249 if (sp->link)
250 {
251 sp = sp->link;
252 }
253 else /* bucket exhausted and no match */
254 {
255 sp = sp->link = new_state (symbol);
256 found = 1;
257 }
258 }
259 }
260 }
261 else /* bucket is empty */
262 {
263 state_table[key] = sp = new_state (symbol);
264 }
265
266 if (trace_flag)
267 fprintf (stderr, "Exiting get_state => %d\n", sp->number);
268
269 return sp->number;
270 }
271
272 /*------------------------------------------------------------------.
273 | Use the information computed by new_itemsets to find the state |
274 | numbers reached by each shift transition from the current state. |
275 | |
276 | shiftset is set up as a vector of state numbers of those states. |
277 `------------------------------------------------------------------*/
278
279 static void
280 append_states (void)
281 {
282 int i;
283 int j;
284 int symbol;
285
286 if (trace_flag)
287 fprintf (stderr, "Entering append_states, state = %d\n",
288 this_state->number);
289
290 /* first sort shift_symbol into increasing order */
291
292 for (i = 1; i < nshifts; i++)
293 {
294 symbol = shift_symbol[i];
295 j = i;
296 while (j > 0 && shift_symbol[j - 1] > symbol)
297 {
298 shift_symbol[j] = shift_symbol[j - 1];
299 j--;
300 }
301 shift_symbol[j] = symbol;
302 }
303
304 for (i = 0; i < nshifts; i++)
305 shiftset[i] = get_state (shift_symbol[i]);
306 }
307
308
309 static void
310 new_states (void)
311 {
312 first_state = last_state = this_state = CORE_ALLOC (0);
313 nstates = 1;
314 }
315
316
317 /*---------------------------------.
318 | Create a new array of N shitfs. |
319 `---------------------------------*/
320
321 static shifts *
322 shifts_new (int n)
323 {
324 shifts *res = SHIFTS_ALLOC (n);
325 res->nshifts = n;
326 return res;
327 }
328
329
330 /*------------------------------------------------------------.
331 | Save the NSHIFTS of SHIFTSET into the current linked list. |
332 `------------------------------------------------------------*/
333
334 static void
335 save_shifts (void)
336 {
337 shifts *p = shifts_new (nshifts);
338
339 p->number = this_state->number;
340
341 shortcpy (p->shifts, shiftset, nshifts);
342
343 if (last_shift)
344 last_shift->next = p;
345 else
346 first_shift = p;
347 last_shift = p;
348 }
349
350
351 /*------------------------------------------------------------------.
352 | Subroutine of augment_automaton. Create the next-to-final state, |
353 | to which a shift has already been made in the initial state. |
354 `------------------------------------------------------------------*/
355
356 static void
357 insert_start_shift (void)
358 {
359 core *statep;
360 shifts *sp;
361
362 statep = CORE_ALLOC (0);
363 statep->number = nstates;
364 statep->accessing_symbol = start_symbol;
365
366 last_state->next = statep;
367 last_state = statep;
368
369 /* Make a shift from this state to (what will be) the final state. */
370 sp = shifts_new (1);
371 sp->number = nstates++;
372 sp->shifts[0] = nstates;
373
374 last_shift->next = sp;
375 last_shift = sp;
376 }
377
378
379 /*------------------------------------------------------------------.
380 | Make sure that the initial state has a shift that accepts the |
381 | grammar's start symbol and goes to the next-to-final state, which |
382 | has a shift going to the final state, which has a shift to the |
383 | termination state. Create such states and shifts if they don't |
384 | happen to exist already. |
385 `------------------------------------------------------------------*/
386
387 static void
388 augment_automaton (void)
389 {
390 core *statep;
391 shifts *sp;
392 shifts *sp1 = NULL;
393
394 sp = first_shift;
395
396 if (!sp)
397 {
398 /* There are no shifts for any state. Make one shift, from the
399 initial state to the next-to-final state. */
400
401 sp = shifts_new (1);
402 sp->shifts[0] = nstates;
403
404 /* Initialize the chain of shifts with sp. */
405 first_shift = sp;
406 last_shift = sp;
407
408 /* Create the next-to-final state, with shift to
409 what will be the final state. */
410 insert_start_shift ();
411 }
412 else if (sp->number == 0)
413 {
414 statep = first_state->next;
415
416 /* The states reached by shifts from FIRST_STATE are numbered
417 1..(SP->NSHIFTS). Look for one reached by START_SYMBOL. */
418 while (statep->accessing_symbol < start_symbol
419 && statep->number < sp->nshifts)
420 statep = statep->next;
421
422 if (statep->accessing_symbol == start_symbol)
423 {
424 /* We already have a next-to-final state.
425 Make sure it has a shift to what will be the final state. */
426 while (sp && sp->number < statep->number)
427 {
428 sp1 = sp;
429 sp = sp->next;
430 }
431
432 if (sp && sp->number == statep->number)
433 {
434 int i;
435 shifts *sp2 = shifts_new (sp->nshifts + 1);
436 sp2->number = statep->number;
437 sp2->shifts[0] = nstates;
438 for (i = sp->nshifts; i > 0; i--)
439 sp2->shifts[i] = sp->shifts[i - 1];
440
441 /* Patch sp2 into the chain of shifts in place of sp,
442 following sp1. */
443 sp2->next = sp->next;
444 sp1->next = sp2;
445 if (sp == last_shift)
446 last_shift = sp2;
447 XFREE (sp);
448 }
449 else
450 {
451 shifts *sp2 = shifts_new (1);
452 sp2->number = statep->number;
453 sp2->shifts[0] = nstates;
454
455 /* Patch sp2 into the chain of shifts between sp1 and sp. */
456 sp2->next = sp;
457 sp1->next = sp2;
458 if (sp == 0)
459 last_shift = sp2;
460 }
461 }
462 else
463 {
464 int i, k;
465 shifts *sp2;
466
467 /* There is no next-to-final state as yet. */
468 /* Add one more shift in first_shift,
469 going to the next-to-final state (yet to be made). */
470 sp = first_shift;
471
472 sp2 = shifts_new (sp->nshifts + 1);
473
474 /* Stick this shift into the vector at the proper place. */
475 statep = first_state->next;
476 for (k = 0, i = 0; i < sp->nshifts; k++, i++)
477 {
478 if (statep->accessing_symbol > start_symbol && i == k)
479 sp2->shifts[k++] = nstates;
480 sp2->shifts[k] = sp->shifts[i];
481 statep = statep->next;
482 }
483 if (i == k)
484 sp2->shifts[k++] = nstates;
485
486 /* Patch sp2 into the chain of shifts
487 in place of sp, at the beginning. */
488 sp2->next = sp->next;
489 first_shift = sp2;
490 if (last_shift == sp)
491 last_shift = sp2;
492
493 XFREE (sp);
494
495 /* Create the next-to-final state, with shift to
496 what will be the final state. */
497 insert_start_shift ();
498 }
499 }
500 else
501 {
502 /* The initial state didn't even have any shifts.
503 Give it one shift, to the next-to-final state. */
504 sp = shifts_new (1);
505 sp->shifts[0] = nstates;
506
507 /* Patch sp into the chain of shifts at the beginning. */
508 sp->next = first_shift;
509 first_shift = sp;
510
511 /* Create the next-to-final state, with shift to
512 what will be the final state. */
513 insert_start_shift ();
514 }
515
516 /* Make the final state--the one that follows a shift from the
517 next-to-final state.
518 The symbol for that shift is 0 (end-of-file). */
519 statep = CORE_ALLOC (0);
520 statep->number = nstates;
521 last_state->next = statep;
522 last_state = statep;
523
524 /* Make the shift from the final state to the termination state. */
525 sp = shifts_new (1);
526 sp->number = nstates++;
527 sp->shifts[0] = nstates;
528 last_shift->next = sp;
529 last_shift = sp;
530
531 /* Note that the variable `final_state' refers to what we sometimes call
532 the termination state. */
533 final_state = nstates;
534
535 /* Make the termination state. */
536 statep = CORE_ALLOC (0);
537 statep->number = nstates++;
538 last_state->next = statep;
539 last_state = statep;
540 }
541
542
543 /*----------------------------------------------------------------.
544 | Find which rules can be used for reduction transitions from the |
545 | current state and make a reductions structure for the state to |
546 | record their rule numbers. |
547 `----------------------------------------------------------------*/
548
549 static void
550 save_reductions (void)
551 {
552 int count;
553 int i;
554
555 /* Find and count the active items that represent ends of rules. */
556
557 count = 0;
558 for (i = 0; i < itemsetsize; ++i)
559 {
560 int item = ritem[itemset[i]];
561 if (item < 0)
562 redset[count++] = -item;
563 }
564
565 /* Make a reductions structure and copy the data into it. */
566
567 if (count)
568 {
569 reductions *p = REDUCTIONS_ALLOC (count);
570
571 p->number = this_state->number;
572 p->nreds = count;
573
574 shortcpy (p->rules, redset, count);
575
576 if (last_reduction)
577 last_reduction->next = p;
578 else
579 first_reduction = p;
580 last_reduction = p;
581 }
582 }
583
584 \f
585 /*-------------------------------------------------------------------.
586 | Compute the nondeterministic finite state machine (see state.h for |
587 | details) from the grammar. |
588 `-------------------------------------------------------------------*/
589
590 void
591 generate_states (void)
592 {
593 allocate_storage ();
594 new_closure (nitems);
595 new_states ();
596
597 while (this_state)
598 {
599 /* Set up ruleset and itemset for the transitions out of this
600 state. ruleset gets a 1 bit for each rule that could reduce
601 now. itemset gets a vector of all the items that could be
602 accepted next. */
603 closure (this_state->items, this_state->nitems);
604 /* record the reductions allowed out of this state */
605 save_reductions ();
606 /* find the itemsets of the states that shifts can reach */
607 new_itemsets ();
608 /* find or create the core structures for those states */
609 append_states ();
610
611 /* create the shifts structures for the shifts to those states,
612 now that the state numbers transitioning to are known */
613 if (nshifts > 0)
614 save_shifts ();
615
616 /* states are queued when they are created; process them all */
617 this_state = this_state->next;
618 }
619
620 /* discard various storage */
621 free_closure ();
622 free_storage ();
623
624 /* set up initial and final states as parser wants them */
625 augment_automaton ();
626 }