]>
git.saurik.com Git - bison.git/blob - src/LR0.c
1 /* Generate the nondeterministic finite state machine for bison,
2 Copyright 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. */
35 core
*first_state
= NULL
;
36 shifts
*first_shift
= NULL
;
37 reductions
*first_reduction
= NULL
;
39 static core
*this_state
= NULL
;
40 static core
*last_state
= NULL
;
41 static shifts
*last_shift
= NULL
;
42 static reductions
*last_reduction
= NULL
;
45 static short *shift_symbol
= NULL
;
47 static short *redset
= NULL
;
48 static short *shiftset
= NULL
;
50 static short **kernel_base
= NULL
;
51 static short **kernel_end
= NULL
;
52 static short *kernel_items
= NULL
;
54 /* hash table for states, to recognize equivalent ones. */
56 #define STATE_TABLE_SIZE 1009
57 static core
**state_table
= NULL
;
61 allocate_itemsets (void)
67 short *symbol_count
= NULL
;
70 symbol_count
= XCALLOC (short, nsyms
);
79 symbol_count
[symbol
]++;
84 /* See comments before new_itemsets. All the vectors of items
85 live inside KERNEL_ITEMS. The number of active items after
86 some symbol cannot be more than the number of times that symbol
87 appears as an item, which is symbol_count[symbol].
88 We allocate that much space for each symbol. */
90 kernel_base
= XCALLOC (short *, nsyms
);
92 kernel_items
= XCALLOC (short, count
);
95 for (i
= 0; i
< nsyms
; i
++)
97 kernel_base
[i
] = kernel_items
+ count
;
98 count
+= symbol_count
[i
];
101 shift_symbol
= symbol_count
;
102 kernel_end
= XCALLOC (short *, nsyms
);
107 allocate_storage (void)
109 allocate_itemsets ();
111 shiftset
= XCALLOC (short, nsyms
);
112 redset
= XCALLOC (short, nrules
+ 1);
113 state_table
= XCALLOC (core
*, STATE_TABLE_SIZE
);
120 XFREE (shift_symbol
);
125 XFREE (kernel_items
);
132 /*----------------------------------------------------------------.
133 | Find which symbols can be shifted in the current state, and for |
134 | each one record which items would be active after that shift. |
135 | Uses the contents of itemset. |
137 | shift_symbol is set to a vector of the symbols that can be |
138 | shifted. For each symbol in the grammar, kernel_base[symbol] |
139 | points to a vector of item numbers activated if that symbol is |
140 | shifted, and kernel_end[symbol] points after the end of that |
142 `----------------------------------------------------------------*/
154 fprintf (stderr
, "Entering new_itemsets, state = %d\n",
158 for (i
= 0; i
< nsyms
; i
++)
159 kernel_end
[i
] = NULL
;
165 while (isp
< itemsetend
)
171 ksp
= kernel_end
[symbol
];
175 shift_symbol
[shiftcount
++] = symbol
;
176 ksp
= kernel_base
[symbol
];
180 kernel_end
[symbol
] = ksp
;
184 nshifts
= shiftcount
;
189 /*-----------------------------------------------------------------.
190 | Subroutine of get_state. Create a new state for those items, if |
192 `-----------------------------------------------------------------*/
195 new_state (int symbol
)
201 fprintf (stderr
, "Entering new_state, state = %d, symbol = %d\n",
205 if (nstates
>= MAXSHORT
)
206 fatal (_("too many states (max %d)"), MAXSHORT
);
208 n
= kernel_end
[symbol
] - kernel_base
[symbol
];
211 p
->accessing_symbol
= symbol
;
215 shortcpy (p
->items
, kernel_base
[symbol
], n
);
217 last_state
->next
= p
;
226 /*--------------------------------------------------------------.
227 | Find the state number for the state we would get to (from the |
228 | current state) by shifting symbol. Create a new state if no |
229 | equivalent one exists already. Used by append_states. |
230 `--------------------------------------------------------------*/
233 get_state (int symbol
)
245 fprintf (stderr
, "Entering get_state, state = %d, symbol = %d\n",
249 isp1
= kernel_base
[symbol
];
250 iend
= kernel_end
[symbol
];
253 /* add up the target state's active item numbers to get a hash key */
258 key
= key
% STATE_TABLE_SIZE
;
260 sp
= state_table
[key
];
270 isp1
= kernel_base
[symbol
];
273 while (found
&& isp1
< iend
)
275 if (*isp1
++ != *isp2
++)
286 else /* bucket exhausted and no match */
288 sp
= sp
->link
= new_state (symbol
);
294 else /* bucket is empty */
296 state_table
[key
] = sp
= new_state (symbol
);
302 /*------------------------------------------------------------------.
303 | Use the information computed by new_itemsets to find the state |
304 | numbers reached by each shift transition from the current state. |
306 | shiftset is set up as a vector of state numbers of those states. |
307 `------------------------------------------------------------------*/
317 fprintf (stderr
, "Entering append_states\n");
320 /* first sort shift_symbol into increasing order */
322 for (i
= 1; i
< nshifts
; i
++)
324 symbol
= shift_symbol
[i
];
326 while (j
> 0 && shift_symbol
[j
- 1] > symbol
)
328 shift_symbol
[j
] = shift_symbol
[j
- 1];
331 shift_symbol
[j
] = symbol
;
334 for (i
= 0; i
< nshifts
; i
++)
336 symbol
= shift_symbol
[i
];
337 shiftset
[i
] = get_state (symbol
);
348 first_state
= last_state
= this_state
= p
;
358 p
= SHIFTS_ALLOC (nshifts
);
360 p
->number
= this_state
->number
;
361 p
->nshifts
= nshifts
;
363 shortcpy (p
->shifts
, shiftset
, nshifts
);
367 last_shift
->next
= p
;
378 /*------------------------------------------------------------------.
379 | Subroutine of augment_automaton. Create the next-to-final state, |
380 | to which a shift has already been made in the initial state. |
381 `------------------------------------------------------------------*/
384 insert_start_shift (void)
389 statep
= CORE_ALLOC (0);
390 statep
->number
= nstates
;
391 statep
->accessing_symbol
= start_symbol
;
393 last_state
->next
= statep
;
396 /* Make a shift from this state to (what will be) the final state. */
397 sp
= SHIFTS_ALLOC (1);
398 sp
->number
= nstates
++;
400 sp
->shifts
[0] = nstates
;
402 last_shift
->next
= sp
;
407 /*------------------------------------------------------------------.
408 | Make sure that the initial state has a shift that accepts the |
409 | grammar's start symbol and goes to the next-to-final state, which |
410 | has a shift going to the final state, which has a shift to the |
411 | termination state. Create such states and shifts if they don't |
412 | happen to exist already. |
413 `------------------------------------------------------------------*/
416 augment_automaton (void)
432 statep
= first_state
->next
;
434 /* The states reached by shifts from first_state are numbered 1...K.
435 Look for one reached by start_symbol. */
436 while (statep
->accessing_symbol
< start_symbol
437 && statep
->number
< k
)
438 statep
= statep
->next
;
440 if (statep
->accessing_symbol
== start_symbol
)
442 /* We already have a next-to-final state.
443 Make sure it has a shift to what will be the final state. */
446 while (sp
&& sp
->number
< k
)
452 if (sp
&& sp
->number
== k
)
454 sp2
= SHIFTS_ALLOC (sp
->nshifts
+ 1);
456 sp2
->nshifts
= sp
->nshifts
+ 1;
457 sp2
->shifts
[0] = nstates
;
458 for (i
= sp
->nshifts
; i
> 0; i
--)
459 sp2
->shifts
[i
] = sp
->shifts
[i
- 1];
461 /* Patch sp2 into the chain of shifts in place of sp,
463 sp2
->next
= sp
->next
;
465 if (sp
== last_shift
)
471 sp2
= SHIFTS_ALLOC (1);
474 sp2
->shifts
[0] = nstates
;
476 /* Patch sp2 into the chain of shifts between sp1 and sp. */
485 /* There is no next-to-final state as yet. */
486 /* Add one more shift in first_shift,
487 going to the next-to-final state (yet to be made). */
490 sp2
= SHIFTS_ALLOC (sp
->nshifts
+ 1);
491 sp2
->nshifts
= sp
->nshifts
+ 1;
493 /* Stick this shift into the vector at the proper place. */
494 statep
= first_state
->next
;
495 for (k
= 0, i
= 0; i
< sp
->nshifts
; k
++, i
++)
497 if (statep
->accessing_symbol
> start_symbol
&& i
== k
)
498 sp2
->shifts
[k
++] = nstates
;
499 sp2
->shifts
[k
] = sp
->shifts
[i
];
500 statep
= statep
->next
;
503 sp2
->shifts
[k
++] = nstates
;
505 /* Patch sp2 into the chain of shifts
506 in place of sp, at the beginning. */
507 sp2
->next
= sp
->next
;
509 if (last_shift
== sp
)
514 /* Create the next-to-final state, with shift to
515 what will be the final state. */
516 insert_start_shift ();
521 /* The initial state didn't even have any shifts.
522 Give it one shift, to the next-to-final state. */
523 sp
= SHIFTS_ALLOC (1);
525 sp
->shifts
[0] = nstates
;
527 /* Patch sp into the chain of shifts at the beginning. */
528 sp
->next
= first_shift
;
531 /* Create the next-to-final state, with shift to
532 what will be the final state. */
533 insert_start_shift ();
538 /* There are no shifts for any state.
539 Make one shift, from the initial state to the next-to-final state. */
541 sp
= SHIFTS_ALLOC (1);
543 sp
->shifts
[0] = nstates
;
545 /* Initialize the chain of shifts with sp. */
549 /* Create the next-to-final state, with shift to
550 what will be the final state. */
551 insert_start_shift ();
554 /* Make the final state--the one that follows a shift from the
556 The symbol for that shift is 0 (end-of-file). */
557 statep
= CORE_ALLOC (0);
558 statep
->number
= nstates
;
559 last_state
->next
= statep
;
562 /* Make the shift from the final state to the termination state. */
563 sp
= SHIFTS_ALLOC (1);
564 sp
->number
= nstates
++;
566 sp
->shifts
[0] = nstates
;
567 last_shift
->next
= sp
;
570 /* Note that the variable `final_state' refers to what we sometimes call
571 the termination state. */
572 final_state
= nstates
;
574 /* Make the termination state. */
575 statep
= CORE_ALLOC (0);
576 statep
->number
= nstates
++;
577 last_state
->next
= statep
;
582 /*----------------------------------------------------------------.
583 | Find which rules can be used for reduction transitions from the |
584 | current state and make a reductions structure for the state to |
585 | record their rule numbers. |
586 `----------------------------------------------------------------*/
589 save_reductions (void)
598 /* Find and count the active items that represent ends of rules. */
601 for (isp
= itemset
; isp
< itemsetend
; isp
++)
605 redset
[count
++] = -item
;
608 /* Make a reductions structure and copy the data into it. */
612 p
= REDUCTIONS_ALLOC (count
);
614 p
->number
= this_state
->number
;
617 shortcpy (p
->rules
, redset
, count
);
621 last_reduction
->next
= p
;
633 /*-------------------------------------------------------------------.
634 | Compute the nondeterministic finite state machine (see state.h for |
635 | details) from the grammar. |
636 `-------------------------------------------------------------------*/
639 generate_states (void)
642 new_closure (nitems
);
647 /* Set up ruleset and itemset for the transitions out of this
648 state. ruleset gets a 1 bit for each rule that could reduce
649 now. itemset gets a vector of all the items that could be
651 closure (this_state
->items
, this_state
->nitems
);
652 /* record the reductions allowed out of this state */
654 /* find the itemsets of the states that shifts can reach */
656 /* find or create the core structures for those states */
659 /* create the shifts structures for the shifts to those states,
660 now that the state numbers transitioning to are known */
664 /* states are queued when they are created; process them all */
665 this_state
= this_state
->next
;
668 /* discard various storage */
672 /* set up initial and final states as parser wants them */
673 augment_automaton ();