]>
git.saurik.com Git - bison.git/blob - src/LR0.c
61ac236c53c5c73c115c76b6002d95d0894f0983
1 /* Generate the nondeterministic finite state machine for bison,
2 Copyright (C) 1984, 1986, 1989, 2000 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 short *itemset
;
33 extern short *itemsetend
;
39 reductions
*first_reduction
;
41 extern void generate_states
PARAMS ((void));
43 static core
*this_state
;
44 static core
*last_state
;
45 static shifts
*last_shift
;
46 static reductions
*last_reduction
;
49 static short *shift_symbol
;
52 static short *shiftset
;
54 static short **kernel_base
;
55 static short **kernel_end
;
56 static short *kernel_items
;
58 /* hash table for states, to recognize equivalent ones. */
60 #define STATE_TABLE_SIZE 1009
61 static core
**state_table
;
65 allocate_itemsets (void)
74 symbol_count
= NEW2 (nsyms
, short);
83 symbol_count
[symbol
]++;
88 /* See comments before new_itemsets. All the vectors of items
89 live inside KERNEL_ITEMS. The number of active items after
90 some symbol cannot be more than the number of times that symbol
91 appears as an item, which is symbol_count[symbol].
92 We allocate that much space for each symbol. */
94 kernel_base
= NEW2 (nsyms
, short *);
95 kernel_items
= NEW2 (count
, short);
98 for (i
= 0; i
< nsyms
; i
++)
100 kernel_base
[i
] = kernel_items
+ count
;
101 count
+= symbol_count
[i
];
104 shift_symbol
= symbol_count
;
105 kernel_end
= NEW2 (nsyms
, short *);
110 allocate_storage (void)
112 allocate_itemsets ();
114 shiftset
= NEW2 (nsyms
, short);
115 redset
= NEW2 (nrules
+ 1, short);
116 state_table
= NEW2 (STATE_TABLE_SIZE
, core
*);
135 /*----------------------------------------------------------------.
136 | Find which symbols can be shifted in the current state, and for |
137 | each one record which items would be active after that shift. |
138 | Uses the contents of itemset. |
140 | shift_symbol is set to a vector of the symbols that can be |
141 | shifted. For each symbol in the grammar, kernel_base[symbol] |
142 | points to a vector of item numbers activated if that symbol is |
143 | shifted, and kernel_end[symbol] points after the end of that |
145 `----------------------------------------------------------------*/
157 fprintf (stderr
, "Entering new_itemsets\n");
160 for (i
= 0; i
< nsyms
; i
++)
161 kernel_end
[i
] = NULL
;
167 while (isp
< itemsetend
)
173 ksp
= kernel_end
[symbol
];
177 shift_symbol
[shiftcount
++] = symbol
;
178 ksp
= kernel_base
[symbol
];
182 kernel_end
[symbol
] = ksp
;
186 nshifts
= shiftcount
;
191 /*-----------------------------------------------------------------.
192 | Subroutine of get_state. Create a new state for those items, if |
194 `-----------------------------------------------------------------*/
197 new_state (int symbol
)
206 fprintf (stderr
, "Entering new_state, symbol = %d\n", symbol
);
209 if (nstates
>= MAXSHORT
)
210 fatal (_("too many states (max %d)"), MAXSHORT
);
212 isp1
= kernel_base
[symbol
];
213 iend
= kernel_end
[symbol
];
217 (core
*) xmalloc ((unsigned) (sizeof (core
) + (n
- 1) * sizeof (short)));
218 p
->accessing_symbol
= symbol
;
226 last_state
->next
= p
;
235 /*--------------------------------------------------------------.
236 | Find the state number for the state we would get to (from the |
237 | current state) by shifting symbol. Create a new state if no |
238 | equivalent one exists already. Used by append_states. |
239 `--------------------------------------------------------------*/
242 get_state (int symbol
)
254 fprintf (stderr
, "Entering get_state, symbol = %d\n", symbol
);
257 isp1
= kernel_base
[symbol
];
258 iend
= kernel_end
[symbol
];
261 /* add up the target state's active item numbers to get a hash key */
266 key
= key
% STATE_TABLE_SIZE
;
268 sp
= state_table
[key
];
278 isp1
= kernel_base
[symbol
];
281 while (found
&& isp1
< iend
)
283 if (*isp1
++ != *isp2
++)
294 else /* bucket exhausted and no match */
296 sp
= sp
->link
= new_state (symbol
);
302 else /* bucket is empty */
304 state_table
[key
] = sp
= new_state (symbol
);
310 /*------------------------------------------------------------------.
311 | Use the information computed by new_itemsets to find the state |
312 | numbers reached by each shift transition from the current state. |
314 | shiftset is set up as a vector of state numbers of those states. |
315 `------------------------------------------------------------------*/
325 fprintf (stderr
, "Entering append_states\n");
328 /* first sort shift_symbol into increasing order */
330 for (i
= 1; i
< nshifts
; i
++)
332 symbol
= shift_symbol
[i
];
334 while (j
> 0 && shift_symbol
[j
- 1] > symbol
)
336 shift_symbol
[j
] = shift_symbol
[j
- 1];
339 shift_symbol
[j
] = symbol
;
342 for (i
= 0; i
< nshifts
; i
++)
344 symbol
= shift_symbol
[i
];
345 shiftset
[i
] = get_state (symbol
);
355 p
= (core
*) xmalloc ((unsigned) (sizeof (core
) - sizeof (short)));
356 first_state
= last_state
= this_state
= p
;
369 p
= (shifts
*) xmalloc ((unsigned) (sizeof (shifts
) +
370 (nshifts
- 1) * sizeof (short)));
372 p
->number
= this_state
->number
;
373 p
->nshifts
= nshifts
;
377 send
= shiftset
+ nshifts
;
384 last_shift
->next
= p
;
395 /*------------------------------------------------------------------.
396 | Subroutine of augment_automaton. Create the next-to-final state, |
397 | to which a shift has already been made in the initial state. |
398 `------------------------------------------------------------------*/
401 insert_start_shift (void)
406 statep
= (core
*) xmalloc ((unsigned) (sizeof (core
) - sizeof (short)));
407 statep
->number
= nstates
;
408 statep
->accessing_symbol
= start_symbol
;
410 last_state
->next
= statep
;
413 /* Make a shift from this state to (what will be) the final state. */
415 sp
->number
= nstates
++;
417 sp
->shifts
[0] = nstates
;
419 last_shift
->next
= sp
;
424 /*------------------------------------------------------------------.
425 | Make sure that the initial state has a shift that accepts the |
426 | grammar's start symbol and goes to the next-to-final state, which |
427 | has a shift going to the final state, which has a shift to the |
428 | termination state. Create such states and shifts if they don't |
429 | happen to exist already. |
430 `------------------------------------------------------------------*/
433 augment_automaton (void)
449 statep
= first_state
->next
;
451 /* The states reached by shifts from first_state are numbered 1...K.
452 Look for one reached by start_symbol. */
453 while (statep
->accessing_symbol
< start_symbol
454 && statep
->number
< k
)
455 statep
= statep
->next
;
457 if (statep
->accessing_symbol
== start_symbol
)
459 /* We already have a next-to-final state.
460 Make sure it has a shift to what will be the final state. */
463 while (sp
&& sp
->number
< k
)
469 if (sp
&& sp
->number
== k
)
471 sp2
= (shifts
*) xmalloc ((unsigned) (sizeof (shifts
)
476 sp2
->nshifts
= sp
->nshifts
+ 1;
477 sp2
->shifts
[0] = nstates
;
478 for (i
= sp
->nshifts
; i
> 0; i
--)
479 sp2
->shifts
[i
] = sp
->shifts
[i
- 1];
481 /* Patch sp2 into the chain of shifts in place of sp,
483 sp2
->next
= sp
->next
;
485 if (sp
== last_shift
)
494 sp2
->shifts
[0] = nstates
;
496 /* Patch sp2 into the chain of shifts between sp1 and sp. */
505 /* There is no next-to-final state as yet. */
506 /* Add one more shift in first_shift,
507 going to the next-to-final state (yet to be made). */
510 sp2
= (shifts
*) xmalloc (sizeof (shifts
)
511 + sp
->nshifts
* sizeof (short));
512 sp2
->nshifts
= sp
->nshifts
+ 1;
514 /* Stick this shift into the vector at the proper place. */
515 statep
= first_state
->next
;
516 for (k
= 0, i
= 0; i
< sp
->nshifts
; k
++, i
++)
518 if (statep
->accessing_symbol
> start_symbol
&& i
== k
)
519 sp2
->shifts
[k
++] = nstates
;
520 sp2
->shifts
[k
] = sp
->shifts
[i
];
521 statep
= statep
->next
;
524 sp2
->shifts
[k
++] = nstates
;
526 /* Patch sp2 into the chain of shifts
527 in place of sp, at the beginning. */
528 sp2
->next
= sp
->next
;
530 if (last_shift
== sp
)
535 /* Create the next-to-final state, with shift to
536 what will be the final state. */
537 insert_start_shift ();
542 /* The initial state didn't even have any shifts.
543 Give it one shift, to the next-to-final state. */
546 sp
->shifts
[0] = nstates
;
548 /* Patch sp into the chain of shifts at the beginning. */
549 sp
->next
= first_shift
;
552 /* Create the next-to-final state, with shift to
553 what will be the final state. */
554 insert_start_shift ();
559 /* There are no shifts for any state.
560 Make one shift, from the initial state to the next-to-final state. */
564 sp
->shifts
[0] = nstates
;
566 /* Initialize the chain of shifts with sp. */
570 /* Create the next-to-final state, with shift to
571 what will be the final state. */
572 insert_start_shift ();
575 /* Make the final state--the one that follows a shift from the
577 The symbol for that shift is 0 (end-of-file). */
578 statep
= (core
*) xmalloc ((unsigned) (sizeof (core
) - sizeof (short)));
579 statep
->number
= nstates
;
580 last_state
->next
= statep
;
583 /* Make the shift from the final state to the termination state. */
585 sp
->number
= nstates
++;
587 sp
->shifts
[0] = nstates
;
588 last_shift
->next
= sp
;
591 /* Note that the variable `final_state' refers to what we sometimes call
592 the termination state. */
593 final_state
= nstates
;
595 /* Make the termination state. */
596 statep
= (core
*) xmalloc ((unsigned) (sizeof (core
) - sizeof (short)));
597 statep
->number
= nstates
++;
598 last_state
->next
= statep
;
603 /*----------------------------------------------------------------.
604 | Find which rules can be used for reduction transitions from the |
605 | current state and make a reductions structure for the state to |
606 | record their rule numbers. |
607 `----------------------------------------------------------------*/
610 save_reductions (void)
621 /* Find and count the active items that represent ends of rules. */
624 for (isp
= itemset
; isp
< itemsetend
; isp
++)
628 redset
[count
++] = -item
;
631 /* Make a reductions structure and copy the data into it. */
635 p
= (reductions
*) xmalloc ((unsigned) (sizeof (reductions
) +
636 (count
- 1) * sizeof (short)));
638 p
->number
= this_state
->number
;
645 for (/* nothing */; rp1
< rend
; ++rp1
, ++rp2
)
650 last_reduction
->next
= p
;
662 /*-------------------------------------------------------------------.
663 | Compute the nondeterministic finite state machine (see state.h for |
664 | details) from the grammar. |
665 `-------------------------------------------------------------------*/
668 generate_states (void)
671 new_closure (nitems
);
676 /* Set up ruleset and itemset for the transitions out of this
677 state. ruleset gets a 1 bit for each rule that could reduce
678 now. itemset gets a vector of all the items that could be
680 closure (this_state
->items
, this_state
->nitems
);
681 /* record the reductions allowed out of this state */
683 /* find the itemsets of the states that shifts can reach */
685 /* find or create the core structures for those states */
688 /* create the shifts structures for the shifts to those states,
689 now that the state numbers transitioning to are known */
693 /* states are queued when they are created; process them all */
694 this_state
= this_state
->next
;
697 /* discard various storage */
701 /* set up initial and final states as parser wants them */
702 augment_automaton ();