]>
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, 2001 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. */
36 core
*first_state
= NULL
;
37 shifts
*first_shift
= NULL
;
38 reductions
*first_reduction
= NULL
;
40 static core
*this_state
= NULL
;
41 static core
*last_state
= NULL
;
42 static shifts
*last_shift
= NULL
;
43 static reductions
*last_reduction
= NULL
;
46 static short *shift_symbol
= NULL
;
48 static short *redset
= NULL
;
49 static short *shiftset
= NULL
;
51 static short **kernel_base
= NULL
;
52 static short **kernel_end
= NULL
;
53 static short *kernel_items
= NULL
;
55 /* hash table for states, to recognize equivalent ones. */
57 #define STATE_TABLE_SIZE 1009
58 static core
**state_table
= NULL
;
62 allocate_itemsets (void)
68 short *symbol_count
= NULL
;
71 symbol_count
= XCALLOC (short, nsyms
);
80 symbol_count
[symbol
]++;
85 /* See comments before new_itemsets. All the vectors of items
86 live inside KERNEL_ITEMS. The number of active items after
87 some symbol cannot be more than the number of times that symbol
88 appears as an item, which is symbol_count[symbol].
89 We allocate that much space for each symbol. */
91 kernel_base
= XCALLOC (short *, nsyms
);
93 kernel_items
= XCALLOC (short, count
);
96 for (i
= 0; i
< nsyms
; i
++)
98 kernel_base
[i
] = kernel_items
+ count
;
99 count
+= symbol_count
[i
];
102 shift_symbol
= symbol_count
;
103 kernel_end
= XCALLOC (short *, nsyms
);
108 allocate_storage (void)
110 allocate_itemsets ();
112 shiftset
= XCALLOC (short, nsyms
);
113 redset
= XCALLOC (short, nrules
+ 1);
114 state_table
= XCALLOC (core
*, STATE_TABLE_SIZE
);
121 XFREE (shift_symbol
);
126 XFREE (kernel_items
);
133 /*----------------------------------------------------------------.
134 | Find which symbols can be shifted in the current state, and for |
135 | each one record which items would be active after that shift. |
136 | Uses the contents of itemset. |
138 | shift_symbol is set to a vector of the symbols that can be |
139 | shifted. For each symbol in the grammar, kernel_base[symbol] |
140 | points to a vector of item numbers activated if that symbol is |
141 | shifted, and kernel_end[symbol] points after the end of that |
143 `----------------------------------------------------------------*/
152 fprintf (stderr
, "Entering new_itemsets, state = %d\n", nstates
);
154 for (i
= 0; i
< nsyms
; i
++)
155 kernel_end
[i
] = NULL
;
159 for (i
= 0; i
< itemsetend
- itemset
; ++i
)
161 int symbol
= ritem
[itemset
[i
]];
164 short *ksp
= kernel_end
[symbol
];
168 shift_symbol
[shiftcount
] = symbol
;
169 ksp
= kernel_base
[symbol
];
173 *ksp
++ = itemset
[i
] + 1;
174 kernel_end
[symbol
] = ksp
;
178 nshifts
= shiftcount
;
183 /*-----------------------------------------------------------------.
184 | Subroutine of get_state. Create a new state for those items, if |
186 `-----------------------------------------------------------------*/
189 new_state (int symbol
)
195 fprintf (stderr
, "Entering new_state, state = %d, symbol = %d\n",
198 if (nstates
>= MAXSHORT
)
199 fatal (_("too many states (max %d)"), MAXSHORT
);
201 n
= kernel_end
[symbol
] - kernel_base
[symbol
];
204 p
->accessing_symbol
= symbol
;
208 shortcpy (p
->items
, kernel_base
[symbol
], n
);
210 last_state
->next
= p
;
218 /*--------------------------------------------------------------.
219 | Find the state number for the state we would get to (from the |
220 | current state) by shifting symbol. Create a new state if no |
221 | equivalent one exists already. Used by append_states. |
222 `--------------------------------------------------------------*/
225 get_state (int symbol
)
232 int n
= kernel_end
[symbol
] - kernel_base
[symbol
];
235 fprintf (stderr
, "Entering get_state, state = %d, symbol = %d\n",
238 /* Add up the target state's active item numbers to get a hash key.
241 for (i
= 0; i
< n
; ++i
)
242 key
+= kernel_base
[symbol
][i
];
243 key
= key
% STATE_TABLE_SIZE
;
244 sp
= state_table
[key
];
255 for (i
= 0; i
< n
; ++i
)
256 if (kernel_base
[symbol
][i
] != sp
->items
[i
])
266 else /* bucket exhausted and no match */
268 sp
= sp
->link
= new_state (symbol
);
274 else /* bucket is empty */
276 state_table
[key
] = sp
= new_state (symbol
);
282 /*------------------------------------------------------------------.
283 | Use the information computed by new_itemsets to find the state |
284 | numbers reached by each shift transition from the current state. |
286 | shiftset is set up as a vector of state numbers of those states. |
287 `------------------------------------------------------------------*/
297 fprintf (stderr
, "Entering append_states\n");
300 /* first sort shift_symbol into increasing order */
302 for (i
= 1; i
< nshifts
; i
++)
304 symbol
= shift_symbol
[i
];
306 while (j
> 0 && shift_symbol
[j
- 1] > symbol
)
308 shift_symbol
[j
] = shift_symbol
[j
- 1];
311 shift_symbol
[j
] = symbol
;
314 for (i
= 0; i
< nshifts
; i
++)
315 shiftset
[i
] = get_state (shift_symbol
[i
]);
322 first_state
= last_state
= this_state
= CORE_ALLOC (0);
330 shifts
*p
= SHIFTS_ALLOC (nshifts
);
332 p
->number
= this_state
->number
;
333 p
->nshifts
= nshifts
;
335 shortcpy (p
->shifts
, shiftset
, nshifts
);
338 last_shift
->next
= p
;
345 /*------------------------------------------------------------------.
346 | Subroutine of augment_automaton. Create the next-to-final state, |
347 | to which a shift has already been made in the initial state. |
348 `------------------------------------------------------------------*/
351 insert_start_shift (void)
356 statep
= CORE_ALLOC (0);
357 statep
->number
= nstates
;
358 statep
->accessing_symbol
= start_symbol
;
360 last_state
->next
= statep
;
363 /* Make a shift from this state to (what will be) the final state. */
364 sp
= SHIFTS_ALLOC (1);
365 sp
->number
= nstates
++;
367 sp
->shifts
[0] = nstates
;
369 last_shift
->next
= sp
;
374 /*------------------------------------------------------------------.
375 | Make sure that the initial state has a shift that accepts the |
376 | grammar's start symbol and goes to the next-to-final state, which |
377 | has a shift going to the final state, which has a shift to the |
378 | termination state. Create such states and shifts if they don't |
379 | happen to exist already. |
380 `------------------------------------------------------------------*/
383 augment_automaton (void)
399 statep
= first_state
->next
;
401 /* The states reached by shifts from first_state are numbered 1...K.
402 Look for one reached by start_symbol. */
403 while (statep
->accessing_symbol
< start_symbol
404 && statep
->number
< k
)
405 statep
= statep
->next
;
407 if (statep
->accessing_symbol
== start_symbol
)
409 /* We already have a next-to-final state.
410 Make sure it has a shift to what will be the final state. */
413 while (sp
&& sp
->number
< k
)
419 if (sp
&& sp
->number
== k
)
421 sp2
= SHIFTS_ALLOC (sp
->nshifts
+ 1);
423 sp2
->nshifts
= sp
->nshifts
+ 1;
424 sp2
->shifts
[0] = nstates
;
425 for (i
= sp
->nshifts
; i
> 0; i
--)
426 sp2
->shifts
[i
] = sp
->shifts
[i
- 1];
428 /* Patch sp2 into the chain of shifts in place of sp,
430 sp2
->next
= sp
->next
;
432 if (sp
== last_shift
)
438 sp2
= SHIFTS_ALLOC (1);
441 sp2
->shifts
[0] = nstates
;
443 /* Patch sp2 into the chain of shifts between sp1 and sp. */
452 /* There is no next-to-final state as yet. */
453 /* Add one more shift in first_shift,
454 going to the next-to-final state (yet to be made). */
457 sp2
= SHIFTS_ALLOC (sp
->nshifts
+ 1);
458 sp2
->nshifts
= sp
->nshifts
+ 1;
460 /* Stick this shift into the vector at the proper place. */
461 statep
= first_state
->next
;
462 for (k
= 0, i
= 0; i
< sp
->nshifts
; k
++, i
++)
464 if (statep
->accessing_symbol
> start_symbol
&& i
== k
)
465 sp2
->shifts
[k
++] = nstates
;
466 sp2
->shifts
[k
] = sp
->shifts
[i
];
467 statep
= statep
->next
;
470 sp2
->shifts
[k
++] = nstates
;
472 /* Patch sp2 into the chain of shifts
473 in place of sp, at the beginning. */
474 sp2
->next
= sp
->next
;
476 if (last_shift
== sp
)
481 /* Create the next-to-final state, with shift to
482 what will be the final state. */
483 insert_start_shift ();
488 /* The initial state didn't even have any shifts.
489 Give it one shift, to the next-to-final state. */
490 sp
= SHIFTS_ALLOC (1);
492 sp
->shifts
[0] = nstates
;
494 /* Patch sp into the chain of shifts at the beginning. */
495 sp
->next
= first_shift
;
498 /* Create the next-to-final state, with shift to
499 what will be the final state. */
500 insert_start_shift ();
505 /* There are no shifts for any state.
506 Make one shift, from the initial state to the next-to-final state. */
508 sp
= SHIFTS_ALLOC (1);
510 sp
->shifts
[0] = nstates
;
512 /* Initialize the chain of shifts with sp. */
516 /* Create the next-to-final state, with shift to
517 what will be the final state. */
518 insert_start_shift ();
521 /* Make the final state--the one that follows a shift from the
523 The symbol for that shift is 0 (end-of-file). */
524 statep
= CORE_ALLOC (0);
525 statep
->number
= nstates
;
526 last_state
->next
= statep
;
529 /* Make the shift from the final state to the termination state. */
530 sp
= SHIFTS_ALLOC (1);
531 sp
->number
= nstates
++;
533 sp
->shifts
[0] = nstates
;
534 last_shift
->next
= sp
;
537 /* Note that the variable `final_state' refers to what we sometimes call
538 the termination state. */
539 final_state
= nstates
;
541 /* Make the termination state. */
542 statep
= CORE_ALLOC (0);
543 statep
->number
= nstates
++;
544 last_state
->next
= statep
;
549 /*----------------------------------------------------------------.
550 | Find which rules can be used for reduction transitions from the |
551 | current state and make a reductions structure for the state to |
552 | record their rule numbers. |
553 `----------------------------------------------------------------*/
556 save_reductions (void)
565 /* Find and count the active items that represent ends of rules. */
568 for (isp
= itemset
; isp
< itemsetend
; isp
++)
572 redset
[count
++] = -item
;
575 /* Make a reductions structure and copy the data into it. */
579 p
= REDUCTIONS_ALLOC (count
);
581 p
->number
= this_state
->number
;
584 shortcpy (p
->rules
, redset
, count
);
587 last_reduction
->next
= p
;
595 /*-------------------------------------------------------------------.
596 | Compute the nondeterministic finite state machine (see state.h for |
597 | details) from the grammar. |
598 `-------------------------------------------------------------------*/
601 generate_states (void)
604 new_closure (nitems
);
609 /* Set up ruleset and itemset for the transitions out of this
610 state. ruleset gets a 1 bit for each rule that could reduce
611 now. itemset gets a vector of all the items that could be
613 closure (this_state
->items
, this_state
->nitems
);
614 /* record the reductions allowed out of this state */
616 /* find the itemsets of the states that shifts can reach */
618 /* find or create the core structures for those states */
621 /* create the shifts structures for the shifts to those states,
622 now that the state numbers transitioning to are known */
626 /* states are queued when they are created; process them all */
627 this_state
= this_state
->next
;
630 /* discard various storage */
634 /* set up initial and final states as parser wants them */
635 augment_automaton ();