]>
git.saurik.com Git - bison.git/blob - src/LR0.c
1 /* Generate the nondeterministic finite state machine for bison,
2 Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.
4 This file is part of Bison, the GNU Compiler Compiler.
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)
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.
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. */
22 /* See comments in state.h for the data structures that represent it.
23 The entry point is generate_states. */
32 extern char *nullable
;
33 extern short *itemset
;
34 extern short *itemsetend
;
41 reductions
*first_reduction
;
43 extern void generate_states
PARAMS ((void));
45 static core
*this_state
;
46 static core
*last_state
;
47 static shifts
*last_shift
;
48 static reductions
*last_reduction
;
51 static short *shift_symbol
;
54 static short *shiftset
;
56 static short **kernel_base
;
57 static short **kernel_end
;
58 static short *kernel_items
;
60 /* hash table for states, to recognize equivalent ones. */
62 #define STATE_TABLE_SIZE 1009
63 static core
**state_table
;
67 allocate_itemsets (void)
76 symbol_count
= NEW2 (nsyms
, short);
85 symbol_count
[symbol
]++;
90 /* See comments before new_itemsets. All the vectors of items
91 live inside KERNEL_ITEMS. The number of active items after
92 some symbol cannot be more than the number of times that symbol
93 appears as an item, which is symbol_count[symbol].
94 We allocate that much space for each symbol. */
96 kernel_base
= NEW2 (nsyms
, short *);
97 kernel_items
= NEW2 (count
, short);
100 for (i
= 0; i
< nsyms
; i
++)
102 kernel_base
[i
] = kernel_items
+ count
;
103 count
+= symbol_count
[i
];
106 shift_symbol
= symbol_count
;
107 kernel_end
= NEW2 (nsyms
, short *);
112 allocate_storage (void)
114 allocate_itemsets ();
116 shiftset
= NEW2 (nsyms
, short);
117 redset
= NEW2 (nrules
+ 1, short);
118 state_table
= NEW2 (STATE_TABLE_SIZE
, core
*);
137 /*----------------------------------------------------------------.
138 | Find which symbols can be shifted in the current state, and for |
139 | each one record which items would be active after that shift. |
140 | Uses the contents of itemset. |
142 | shift_symbol is set to a vector of the symbols that can be |
143 | shifted. For each symbol in the grammar, kernel_base[symbol] |
144 | points to a vector of item numbers activated if that symbol is |
145 | shifted, and kernel_end[symbol] points after the end of that |
147 `----------------------------------------------------------------*/
159 fprintf (stderr
, "Entering new_itemsets\n");
162 for (i
= 0; i
< nsyms
; i
++)
163 kernel_end
[i
] = NULL
;
169 while (isp
< itemsetend
)
175 ksp
= kernel_end
[symbol
];
179 shift_symbol
[shiftcount
++] = symbol
;
180 ksp
= kernel_base
[symbol
];
184 kernel_end
[symbol
] = ksp
;
188 nshifts
= shiftcount
;
193 /*-----------------------------------------------------------------.
194 | Subroutine of get_state. Create a new state for those items, if |
196 `-----------------------------------------------------------------*/
199 new_state (int symbol
)
208 fprintf (stderr
, "Entering new_state, symbol = %d\n", symbol
);
211 if (nstates
>= MAXSHORT
)
212 fatal (_("too many states (max %d)"), MAXSHORT
);
214 isp1
= kernel_base
[symbol
];
215 iend
= kernel_end
[symbol
];
219 (core
*) xmalloc ((unsigned) (sizeof (core
) + (n
- 1) * sizeof (short)));
220 p
->accessing_symbol
= symbol
;
228 last_state
->next
= p
;
237 /*--------------------------------------------------------------.
238 | Find the state number for the state we would get to (from the |
239 | current state) by shifting symbol. Create a new state if no |
240 | equivalent one exists already. Used by append_states. |
241 `--------------------------------------------------------------*/
244 get_state (int symbol
)
256 fprintf (stderr
, "Entering get_state, symbol = %d\n", symbol
);
259 isp1
= kernel_base
[symbol
];
260 iend
= kernel_end
[symbol
];
263 /* add up the target state's active item numbers to get a hash key */
268 key
= key
% STATE_TABLE_SIZE
;
270 sp
= state_table
[key
];
280 isp1
= kernel_base
[symbol
];
283 while (found
&& isp1
< iend
)
285 if (*isp1
++ != *isp2
++)
296 else /* bucket exhausted and no match */
298 sp
= sp
->link
= new_state (symbol
);
304 else /* bucket is empty */
306 state_table
[key
] = sp
= new_state (symbol
);
312 /*------------------------------------------------------------------.
313 | Use the information computed by new_itemsets to find the state |
314 | numbers reached by each shift transition from the current state. |
316 | shiftset is set up as a vector of state numbers of those states. |
317 `------------------------------------------------------------------*/
327 fprintf (stderr
, "Entering append_states\n");
330 /* first sort shift_symbol into increasing order */
332 for (i
= 1; i
< nshifts
; i
++)
334 symbol
= shift_symbol
[i
];
336 while (j
> 0 && shift_symbol
[j
- 1] > symbol
)
338 shift_symbol
[j
] = shift_symbol
[j
- 1];
341 shift_symbol
[j
] = symbol
;
344 for (i
= 0; i
< nshifts
; i
++)
346 symbol
= shift_symbol
[i
];
347 shiftset
[i
] = get_state (symbol
);
357 p
= (core
*) xmalloc ((unsigned) (sizeof (core
) - sizeof (short)));
358 first_state
= last_state
= this_state
= p
;
371 p
= (shifts
*) xmalloc ((unsigned) (sizeof (shifts
) +
372 (nshifts
- 1) * sizeof (short)));
374 p
->number
= this_state
->number
;
375 p
->nshifts
= nshifts
;
379 send
= shiftset
+ nshifts
;
386 last_shift
->next
= p
;
397 /*------------------------------------------------------------------.
398 | Subroutine of augment_automaton. Create the next-to-final state, |
399 | to which a shift has already been made in the initial state. |
400 `------------------------------------------------------------------*/
403 insert_start_shift (void)
408 statep
= (core
*) xmalloc ((unsigned) (sizeof (core
) - sizeof (short)));
409 statep
->number
= nstates
;
410 statep
->accessing_symbol
= start_symbol
;
412 last_state
->next
= statep
;
415 /* Make a shift from this state to (what will be) the final state. */
417 sp
->number
= nstates
++;
419 sp
->shifts
[0] = nstates
;
421 last_shift
->next
= sp
;
426 /*------------------------------------------------------------------.
427 | Make sure that the initial state has a shift that accepts the |
428 | grammar's start symbol and goes to the next-to-final state, which |
429 | has a shift going to the final state, which has a shift to the |
430 | termination state. Create such states and shifts if they don't |
431 | happen to exist already. |
432 `------------------------------------------------------------------*/
435 augment_automaton (void)
451 statep
= first_state
->next
;
453 /* The states reached by shifts from first_state are numbered 1...K.
454 Look for one reached by start_symbol. */
455 while (statep
->accessing_symbol
< start_symbol
456 && statep
->number
< k
)
457 statep
= statep
->next
;
459 if (statep
->accessing_symbol
== start_symbol
)
461 /* We already have a next-to-final state.
462 Make sure it has a shift to what will be the final state. */
465 while (sp
&& sp
->number
< k
)
471 if (sp
&& sp
->number
== k
)
473 sp2
= (shifts
*) xmalloc ((unsigned) (sizeof (shifts
)
478 sp2
->nshifts
= sp
->nshifts
+ 1;
479 sp2
->shifts
[0] = nstates
;
480 for (i
= sp
->nshifts
; i
> 0; i
--)
481 sp2
->shifts
[i
] = sp
->shifts
[i
- 1];
483 /* Patch sp2 into the chain of shifts in place of sp,
485 sp2
->next
= sp
->next
;
487 if (sp
== last_shift
)
496 sp2
->shifts
[0] = nstates
;
498 /* Patch sp2 into the chain of shifts between sp1 and sp. */
507 /* There is no next-to-final state as yet. */
508 /* Add one more shift in first_shift,
509 going to the next-to-final state (yet to be made). */
512 sp2
= (shifts
*) xmalloc (sizeof (shifts
)
513 + sp
->nshifts
* sizeof (short));
514 sp2
->nshifts
= sp
->nshifts
+ 1;
516 /* Stick this shift into the vector at the proper place. */
517 statep
= first_state
->next
;
518 for (k
= 0, i
= 0; i
< sp
->nshifts
; k
++, i
++)
520 if (statep
->accessing_symbol
> start_symbol
&& i
== k
)
521 sp2
->shifts
[k
++] = nstates
;
522 sp2
->shifts
[k
] = sp
->shifts
[i
];
523 statep
= statep
->next
;
526 sp2
->shifts
[k
++] = nstates
;
528 /* Patch sp2 into the chain of shifts
529 in place of sp, at the beginning. */
530 sp2
->next
= sp
->next
;
532 if (last_shift
== sp
)
537 /* Create the next-to-final state, with shift to
538 what will be the final state. */
539 insert_start_shift ();
544 /* The initial state didn't even have any shifts.
545 Give it one shift, to the next-to-final state. */
548 sp
->shifts
[0] = nstates
;
550 /* Patch sp into the chain of shifts at the beginning. */
551 sp
->next
= first_shift
;
554 /* Create the next-to-final state, with shift to
555 what will be the final state. */
556 insert_start_shift ();
561 /* There are no shifts for any state.
562 Make one shift, from the initial state to the next-to-final state. */
566 sp
->shifts
[0] = nstates
;
568 /* Initialize the chain of shifts with sp. */
572 /* Create the next-to-final state, with shift to
573 what will be the final state. */
574 insert_start_shift ();
577 /* Make the final state--the one that follows a shift from the
579 The symbol for that shift is 0 (end-of-file). */
580 statep
= (core
*) xmalloc ((unsigned) (sizeof (core
) - sizeof (short)));
581 statep
->number
= nstates
;
582 last_state
->next
= statep
;
585 /* Make the shift from the final state to the termination state. */
587 sp
->number
= nstates
++;
589 sp
->shifts
[0] = nstates
;
590 last_shift
->next
= sp
;
593 /* Note that the variable `final_state' refers to what we sometimes call
594 the termination state. */
595 final_state
= nstates
;
597 /* Make the termination state. */
598 statep
= (core
*) xmalloc ((unsigned) (sizeof (core
) - sizeof (short)));
599 statep
->number
= nstates
++;
600 last_state
->next
= statep
;
605 /*----------------------------------------------------------------.
606 | Find which rules can be used for reduction transitions from the |
607 | current state and make a reductions structure for the state to |
608 | record their rule numbers. |
609 `----------------------------------------------------------------*/
612 save_reductions (void)
623 /* Find and count the active items that represent ends of rules. */
626 for (isp
= itemset
; isp
< itemsetend
; isp
++)
630 redset
[count
++] = -item
;
633 /* Make a reductions structure and copy the data into it. */
637 p
= (reductions
*) xmalloc ((unsigned) (sizeof (reductions
) +
638 (count
- 1) * sizeof (short)));
640 p
->number
= this_state
->number
;
647 for (/* nothing */; rp1
< rend
; ++rp1
, ++rp2
)
652 last_reduction
->next
= p
;
664 /*-------------------------------------------------------------------.
665 | Compute the nondeterministic finite state machine (see state.h for |
666 | details) from the grammar. |
667 `-------------------------------------------------------------------*/
670 generate_states (void)
673 new_closure (nitems
);
678 /* Set up ruleset and itemset for the transitions out of this
679 state. ruleset gets a 1 bit for each rule that could reduce
680 now. itemset gets a vector of all the items that could be
682 closure (this_state
->items
, this_state
->nitems
);
683 /* record the reductions allowed out of this state */
685 /* find the itemsets of the states that shifts can reach */
687 /* find or create the core structures for those states */
690 /* create the shifts structures for the shifts to those states,
691 now that the state numbers transitioning to are known */
695 /* states are queued when they are created; process them all */
696 this_state
= this_state
->next
;
699 /* discard various storage */
703 /* set up initial and final states as parser wants them */
704 augment_automaton ();