]> git.saurik.com Git - bison.git/blame - src/LR0.c
* src/print.c (print_shifts, print_gotos): Merge into...
[bison.git] / src / LR0.c
CommitLineData
40675e7c 1/* Generate the nondeterministic finite state machine for bison,
602bbf31 2 Copyright 1984, 1986, 1989, 2000, 2001, 2002 Free Software Foundation, Inc.
40675e7c 3
2fa6973e 4 This file is part of Bison, the GNU Compiler Compiler.
40675e7c 5
2fa6973e
AD
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.
40675e7c 10
2fa6973e
AD
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.
40675e7c 15
2fa6973e
AD
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. */
40675e7c
DM
20
21
22/* See comments in state.h for the data structures that represent it.
23 The entry point is generate_states. */
24
40675e7c 25#include "system.h"
602bbf31 26#include "bitset.h"
8b3df748 27#include "quotearg.h"
0e78e603 28#include "symtab.h"
5fbb0954 29#include "gram.h"
9bfe901c 30#include "getargs.h"
c87d4863 31#include "reader.h"
40675e7c
DM
32#include "gram.h"
33#include "state.h"
a0f6b076 34#include "complain.h"
2fa6973e 35#include "closure.h"
403b315b 36#include "LR0.h"
49701457 37#include "lalr.h"
630e182b 38#include "reduce.h"
40675e7c 39
32e1e0a4
AD
40typedef struct state_list_s
41{
42 struct state_list_s *next;
43 state_t *state;
44} state_list_t;
45
46static state_list_t *first_state = NULL;
47static state_list_t *last_state = NULL;
48
49static void
50state_list_append (state_t *state)
51{
52 state_list_t *node = XMALLOC (state_list_t, 1);
53 node->next = NULL;
54 node->state = state;
40675e7c 55
32e1e0a4
AD
56 if (!first_state)
57 first_state = node;
58 if (last_state)
59 last_state->next = node;
60 last_state = node;
61}
40675e7c
DM
62
63static int nshifts;
a49aecd5 64static symbol_number_t *shift_symbol = NULL;
40675e7c 65
342b8b6e 66static short *redset = NULL;
d57650a5 67static state_number_t *shiftset = NULL;
40675e7c 68
62a3e4f0 69static item_number_t **kernel_base = NULL;
6255b435 70static int *kernel_size = NULL;
62a3e4f0 71static item_number_t *kernel_items = NULL;
40675e7c 72
2fa6973e 73\f
4a120d45 74static void
d2729d44 75allocate_itemsets (void)
40675e7c 76{
9222837b
AD
77 symbol_number_t i;
78 rule_number_t r;
62a3e4f0 79 item_number_t *rhsp;
40675e7c 80
630e182b
AD
81 /* Count the number of occurrences of all the symbols in RITEMS.
82 Note that useless productions (hence useless nonterminals) are
83 browsed too, hence we need to allocate room for _all_ the
84 symbols. */
85 int count = 0;
86 short *symbol_count = XCALLOC (short, nsyms + nuseless_nonterminals);
40675e7c 87
b4c4ccc2
AD
88 for (r = 1; r < nrules + 1; ++r)
89 for (rhsp = rules[r].rhs; *rhsp >= 0; ++rhsp)
c87d4863
AD
90 {
91 count++;
b4c4ccc2 92 symbol_count[*rhsp]++;
c87d4863 93 }
40675e7c 94
2fa6973e
AD
95 /* See comments before new_itemsets. All the vectors of items
96 live inside KERNEL_ITEMS. The number of active items after
40675e7c 97 some symbol cannot be more than the number of times that symbol
8a731ca8 98 appears as an item, which is SYMBOL_COUNT[SYMBOL].
40675e7c
DM
99 We allocate that much space for each symbol. */
100
62a3e4f0 101 kernel_base = XCALLOC (item_number_t *, nsyms);
342b8b6e 102 if (count)
62a3e4f0 103 kernel_items = XCALLOC (item_number_t, count);
40675e7c
DM
104
105 count = 0;
106 for (i = 0; i < nsyms; i++)
107 {
108 kernel_base[i] = kernel_items + count;
109 count += symbol_count[i];
110 }
111
630e182b 112 free (symbol_count);
0e41b407 113 kernel_size = XCALLOC (int, nsyms);
40675e7c
DM
114}
115
116
4a120d45 117static void
d2729d44 118allocate_storage (void)
40675e7c 119{
2fa6973e 120 allocate_itemsets ();
40675e7c 121
d57650a5 122 shiftset = XCALLOC (state_number_t, nsyms);
d7913476 123 redset = XCALLOC (short, nrules + 1);
c7ca99d4 124 state_hash_new ();
a49aecd5 125 shift_symbol = XCALLOC (symbol_number_t, nsyms);
40675e7c
DM
126}
127
128
4a120d45 129static void
d2729d44 130free_storage (void)
40675e7c 131{
630e182b
AD
132 free (shift_symbol);
133 free (redset);
134 free (shiftset);
135 free (kernel_base);
136 free (kernel_size);
d7913476 137 XFREE (kernel_items);
c7ca99d4 138 state_hash_free ();
40675e7c
DM
139}
140
141
142
40675e7c 143
32e1e0a4
AD
144/*---------------------------------------------------------------.
145| Find which symbols can be shifted in STATE, and for each one |
146| record which items would be active after that shift. Uses the |
147| contents of itemset. |
148| |
149| shift_symbol is set to a vector of the symbols that can be |
150| shifted. For each symbol in the grammar, kernel_base[symbol] |
151| points to a vector of item numbers activated if that symbol is |
152| shifted, and kernel_size[symbol] is their numbers. |
153`---------------------------------------------------------------*/
40675e7c 154
4a120d45 155static void
32e1e0a4 156new_itemsets (state_t *state)
40675e7c 157{
2fa6973e 158 int i;
2fa6973e 159
9bfe901c 160 if (trace_flag)
c87d4863 161 fprintf (stderr, "Entering new_itemsets, state = %d\n",
32e1e0a4 162 state->number);
40675e7c
DM
163
164 for (i = 0; i < nsyms; i++)
125ecb56 165 kernel_size[i] = 0;
40675e7c 166
b2872512 167 nshifts = 0;
40675e7c 168
5123689b 169 for (i = 0; i < nritemset; ++i)
5fbb0954
AD
170 if (ritem[itemset[i]] >= 0)
171 {
a49aecd5
AD
172 symbol_number_t symbol
173 = item_number_as_symbol_number (ritem[itemset[i]]);
5fbb0954
AD
174 if (!kernel_size[symbol])
175 {
176 shift_symbol[nshifts] = symbol;
177 nshifts++;
178 }
179
180 kernel_base[symbol][kernel_size[symbol]] = itemset[i] + 1;
181 kernel_size[symbol]++;
182 }
40675e7c
DM
183}
184
185
186
2fa6973e
AD
187/*-----------------------------------------------------------------.
188| Subroutine of get_state. Create a new state for those items, if |
189| necessary. |
190`-----------------------------------------------------------------*/
40675e7c 191
f693ad14 192static state_t *
a49aecd5 193new_state (symbol_number_t symbol, size_t core_size, item_number_t *core)
40675e7c 194{
df0e7316 195 state_t *res;
40675e7c 196
9bfe901c 197 if (trace_flag)
c87d4863 198 fprintf (stderr, "Entering new_state, state = %d, symbol = %d (%s)\n",
6b98e4b5 199 nstates, symbol, symbol_tag_get (symbols[symbol]));
40675e7c 200
df0e7316 201 res = state_new (symbol, core_size, core);
c7ca99d4 202 state_hash_insert (res);
2fa6973e 203
643a5994
AD
204 /* If this is the eoftoken, and this is not the initial state, then
205 this is the final state. */
206 if (symbol == 0 && first_state)
df0e7316 207 final_state = res;
643a5994 208
32e1e0a4 209 state_list_append (res);
df0e7316 210 return res;
2fa6973e 211}
40675e7c 212
2fa6973e
AD
213
214/*--------------------------------------------------------------.
215| Find the state number for the state we would get to (from the |
216| current state) by shifting symbol. Create a new state if no |
97db7bd4 217| equivalent one exists already. Used by append_states. |
2fa6973e 218`--------------------------------------------------------------*/
40675e7c 219
d57650a5 220static state_number_t
a49aecd5 221get_state (symbol_number_t symbol, size_t core_size, item_number_t *core)
40675e7c 222{
f693ad14 223 state_t *sp;
40675e7c 224
9bfe901c 225 if (trace_flag)
32e1e0a4
AD
226 fprintf (stderr, "Entering get_state, symbol = %d (%s)\n",
227 symbol, symbol_tag_get (symbols[symbol]));
40675e7c 228
c7ca99d4
AD
229 sp = state_hash_lookup (core_size, core);
230 if (!sp)
231 sp = new_state (symbol, core_size, core);
40675e7c 232
c87d4863
AD
233 if (trace_flag)
234 fprintf (stderr, "Exiting get_state => %d\n", sp->number);
235
36281465 236 return sp->number;
40675e7c
DM
237}
238
2fa6973e
AD
239/*------------------------------------------------------------------.
240| Use the information computed by new_itemsets to find the state |
32e1e0a4 241| numbers reached by each shift transition from STATE. |
2fa6973e 242| |
32e1e0a4 243| SHIFTSET is set up as a vector of state numbers of those states. |
2fa6973e 244`------------------------------------------------------------------*/
40675e7c 245
2fa6973e 246static void
32e1e0a4 247append_states (state_t *state)
40675e7c 248{
2fa6973e
AD
249 int i;
250 int j;
a49aecd5 251 symbol_number_t symbol;
40675e7c 252
9bfe901c 253 if (trace_flag)
c87d4863 254 fprintf (stderr, "Entering append_states, state = %d\n",
32e1e0a4 255 state->number);
40675e7c 256
2fa6973e 257 /* first sort shift_symbol into increasing order */
40675e7c 258
2fa6973e
AD
259 for (i = 1; i < nshifts; i++)
260 {
261 symbol = shift_symbol[i];
262 j = i;
263 while (j > 0 && shift_symbol[j - 1] > symbol)
264 {
265 shift_symbol[j] = shift_symbol[j - 1];
266 j--;
267 }
268 shift_symbol[j] = symbol;
269 }
40675e7c 270
2fa6973e 271 for (i = 0; i < nshifts; i++)
458be8e0
AD
272 {
273 symbol = shift_symbol[i];
274 shiftset[i] = get_state (symbol,
275 kernel_size[symbol], kernel_base[symbol]);
276 }
40675e7c
DM
277}
278
279
4a120d45 280static void
2fa6973e 281new_states (void)
40675e7c 282{
643a5994
AD
283 /* The 0 at the lhs is the index of the item of this initial rule. */
284 kernel_base[0][0] = 0;
285 kernel_size[0] = 1;
32e1e0a4 286 state_list_append (new_state (0, kernel_size[0], kernel_base[0]));
40675e7c
DM
287}
288
289
40675e7c 290
2fa6973e
AD
291/*----------------------------------------------------------------.
292| Find which rules can be used for reduction transitions from the |
293| current state and make a reductions structure for the state to |
294| record their rule numbers. |
295`----------------------------------------------------------------*/
296
4a120d45 297static void
32e1e0a4 298save_reductions (state_t *state)
40675e7c 299{
30171f79 300 int count = 0;
fb908786 301 int i;
40675e7c 302
30171f79
AD
303 /* If this is the final state, we want it to have no reductions at
304 all, although it has one for `START_SYMBOL EOF .'. */
32e1e0a4 305 if (final_state && state->number == final_state->number)
30171f79 306 return;
40675e7c 307
30171f79 308 /* Find and count the active items that represent ends of rules. */
5123689b 309 for (i = 0; i < nritemset; ++i)
2fa6973e 310 {
fb908786 311 int item = ritem[itemset[i]];
2fa6973e
AD
312 if (item < 0)
313 redset[count++] = -item;
314 }
40675e7c 315
2fa6973e 316 /* Make a reductions structure and copy the data into it. */
8a731ca8 317 state_reductions_set (state, count, redset);
2fa6973e
AD
318}
319
320\f
82841af7 321/*---------------.
29e88316 322| Build STATES. |
82841af7 323`---------------*/
6a164e0c
AD
324
325static void
29e88316 326set_states (void)
6a164e0c 327{
29e88316 328 states = XCALLOC (state_t *, nstates);
6a164e0c 329
32e1e0a4 330 while (first_state)
2cec70b9 331 {
32e1e0a4
AD
332 state_list_t *this = first_state;
333
2cec70b9 334 /* Pessimization, but simplification of the code: make sure all
80dac38c
AD
335 the states have a shifts, errs, and reductions, even if
336 reduced to 0. */
32e1e0a4
AD
337 state_t *state = this->state;
338 if (!state->shifts)
339 state_shifts_set (state, 0, 0);
340 if (!state->errs)
341 state->errs = errs_new (0);
342 if (!state->reductions)
8a731ca8 343 state_reductions_set (state, 0, 0);
32e1e0a4
AD
344
345 states[state->number] = state;
346
347 first_state = this->next;
348 free (this);
2cec70b9 349 }
32e1e0a4
AD
350 first_state = NULL;
351 last_state = NULL;
6a164e0c
AD
352}
353
c7ca99d4 354
2fa6973e
AD
355/*-------------------------------------------------------------------.
356| Compute the nondeterministic finite state machine (see state.h for |
357| details) from the grammar. |
358`-------------------------------------------------------------------*/
359
360void
361generate_states (void)
362{
32e1e0a4 363 state_list_t *list = NULL;
2fa6973e 364 allocate_storage ();
9e7f6bbd 365 new_closure (nritems);
2fa6973e 366 new_states ();
32e1e0a4 367 list = first_state;
2fa6973e 368
32e1e0a4 369 while (list)
2fa6973e 370 {
32e1e0a4 371 state_t *state = list->state;
23cbcc6c
AD
372 if (trace_flag)
373 fprintf (stderr, "Processing state %d (reached by %s)\n",
32e1e0a4
AD
374 state->number,
375 symbol_tag_get (symbols[state->accessing_symbol]));
2fa6973e
AD
376 /* Set up ruleset and itemset for the transitions out of this
377 state. ruleset gets a 1 bit for each rule that could reduce
378 now. itemset gets a vector of all the items that could be
379 accepted next. */
32e1e0a4
AD
380 closure (state->items, state->nitems);
381 /* Record the reductions allowed out of this state. */
382 save_reductions (state);
383 /* Find the itemsets of the states that shifts can reach. */
384 new_itemsets (state);
385 /* Find or create the core structures for those states. */
386 append_states (state);
387
388 /* Create the shifts structures for the shifts to those states,
389 now that the state numbers transitioning to are known. */
390 state_shifts_set (state, nshifts, shiftset);
391
392 /* States are queued when they are created; process them all.
393 */
394 list = list->next;
2fa6973e
AD
395 }
396
397 /* discard various storage */
398 free_closure ();
399 free_storage ();
400
29e88316
AD
401 /* Set up STATES. */
402 set_states ();
40675e7c 403}