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