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