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