]> git.saurik.com Git - bison.git/blame - src/state.c
(G_EXPAND): Renamed from G_STRETCH.
[bison.git] / src / state.c
CommitLineData
ec14f0c8
PE
1/* Type definitions for nondeterministic finite state machine for Bison.
2 Copyright (C) 2001, 2002 Free Software Foundation, Inc.
5e893e1c
AD
3
4 This file is part of Bison, the GNU Compiler Compiler.
5
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.
10
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.
15
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. */
20
21
22#include "system.h"
17ee7397
PE
23
24#include <hash.h>
25
df0e7316 26#include "complain.h"
62a3e4f0 27#include "gram.h"
5e893e1c
AD
28#include "state.h"
29
df0e7316
AD
30
31 /*-------------------.
32 | Shifts and Gotos. |
33 `-------------------*/
34
35
c7ca99d4
AD
36/*---------------------------------------.
37| Create a new array of N shifts/gotos. |
38`---------------------------------------*/
5e893e1c 39
640748ee 40#define TRANSITIONS_ALLOC(Num) \
17ee7397
PE
41 (transitions *) xcalloc ((sizeof (transitions) \
42 + (Num - 1) * sizeof (state *)), \
43 1)
2cec70b9 44
17ee7397
PE
45static transitions *
46transitions_new (int num, state **the_states)
5e893e1c 47{
17ee7397 48 transitions *res = TRANSITIONS_ALLOC (num);
ccaf65bc
AD
49 res->num = num;
50 memcpy (res->states, the_states, num * sizeof (the_states[0]));
5e893e1c
AD
51 return res;
52}
2cec70b9
AD
53
54
ccaf65bc
AD
55/*-------------------------------------------------------------------.
56| Return the state such these TRANSITIONS contain a shift/goto to it |
17ee7397 57| on S. Abort if none found. |
ccaf65bc 58`-------------------------------------------------------------------*/
24c7d800 59
17ee7397
PE
60state *
61transitions_to (transitions *shifts, symbol_number s)
24c7d800
AD
62{
63 int j;
ccaf65bc
AD
64 for (j = 0; j < shifts->num; j++)
65 if (TRANSITION_SYMBOL (shifts, j) == s)
640748ee 66 return shifts->states[j];
24c7d800
AD
67 abort ();
68}
df0e7316
AD
69
70
71 /*--------------------.
72 | Error transitions. |
73 `--------------------*/
74
75
2cec70b9
AD
76/*-------------------------------.
77| Create a new array of N errs. |
78`-------------------------------*/
79
17ee7397
PE
80#define ERRS_ALLOC(Nerrs) \
81 ((errs *) xcalloc ((sizeof (errs) + (Nerrs - 1) * sizeof (symbol *)), 1))
2cec70b9
AD
82
83
17ee7397
PE
84errs *
85errs_new (int num, symbol **tokens)
2cec70b9 86{
17ee7397 87 errs *res = ERRS_ALLOC (num);
8b752b00
AD
88 res->num = num;
89 memcpy (res->symbols, tokens, num * sizeof (tokens[0]));
2cec70b9
AD
90 return res;
91}
80dac38c 92
df0e7316
AD
93
94
95
96 /*-------------.
97 | Reductions. |
98 `-------------*/
99
100
80dac38c
AD
101/*-------------------------------------.
102| Create a new array of N reductions. |
103`-------------------------------------*/
104
cd08e51e 105#define REDUCTIONS_ALLOC(Nreductions) \
17ee7397
PE
106 (reductions *) xcalloc ((sizeof (reductions) \
107 + (Nreductions - 1) * sizeof (rule *)), 1)
80dac38c 108
17ee7397
PE
109static reductions *
110reductions_new (int num, rule **reds)
80dac38c 111{
17ee7397 112 reductions *res = REDUCTIONS_ALLOC (num);
8b752b00 113 res->num = num;
17ee7397 114 memcpy (res->rules, reds, num * sizeof (reds[0]));
cd08e51e 115 res->lookaheads = NULL;
80dac38c
AD
116 return res;
117}
10e5b8bd
AD
118
119
df0e7316
AD
120
121 /*---------.
122 | States. |
123 `---------*/
124
125
17ee7397 126state_number nstates = 0;
df0e7316 127/* FINAL_STATE is properly set by new_state when it recognizes its
88bce5a2 128 accessing symbol: $end. */
17ee7397 129state *final_state = NULL;
df0e7316 130
c7ca99d4 131#define STATE_ALLOC(Nitems) \
17ee7397
PE
132 (state *) xcalloc ((sizeof (state) \
133 + (Nitems - 1) * sizeof (item_number)), \
134 1)
c7ca99d4 135
8b752b00
AD
136/*------------------------------------------------------------------.
137| Create a new state with ACCESSING_SYMBOL, for those items. Store |
138| it in the state hash table. |
139`------------------------------------------------------------------*/
df0e7316 140
17ee7397
PE
141state *
142state_new (symbol_number accessing_symbol,
143 size_t core_size, item_number *core)
df0e7316 144{
17ee7397 145 state *res;
df0e7316 146
17ee7397 147 if (STATE_NUMBER_MAXIMUM <= nstates)
ec14f0c8 148 abort ();
df0e7316 149
df0e7316
AD
150 res = STATE_ALLOC (core_size);
151 res->accessing_symbol = accessing_symbol;
152 res->number = nstates;
153 ++nstates;
154 res->solved_conflicts = NULL;
155
156 res->nitems = core_size;
157 memcpy (res->items, core, core_size * sizeof (core[0]));
158
8b752b00
AD
159 state_hash_insert (res);
160
df0e7316
AD
161 return res;
162}
163
164
17ee7397
PE
165/*---------.
166| Free S. |
167`---------*/
8b752b00
AD
168
169static void
17ee7397 170state_free (state *s)
8b752b00 171{
17ee7397
PE
172 free (s->transitions);
173 free (s->reductions);
174 free (s->errs);
175 free (s);
8b752b00
AD
176}
177
178
17ee7397
PE
179/*---------------------------.
180| Set the transitions of S. |
181`---------------------------*/
32e1e0a4
AD
182
183void
17ee7397 184state_transitions_set (state *s, int num, state **trans)
32e1e0a4 185{
17ee7397 186 if (s->transitions)
ec14f0c8 187 abort ();
17ee7397 188 s->transitions = transitions_new (num, trans);
32e1e0a4
AD
189}
190
191
17ee7397
PE
192/*--------------------------.
193| Set the reductions of S. |
194`--------------------------*/
8a731ca8
AD
195
196void
17ee7397 197state_reductions_set (state *s, int num, rule **reds)
8a731ca8 198{
17ee7397 199 if (s->reductions)
ec14f0c8 200 abort ();
17ee7397 201 s->reductions = reductions_new (num, reds);
8b752b00
AD
202}
203
204
cd08e51e 205int
17ee7397 206state_reduction_find (state *s, rule *r)
cd08e51e
AD
207{
208 int i;
17ee7397 209 reductions *reds = s->reductions;
cd08e51e 210 for (i = 0; i < reds->num; ++i)
17ee7397 211 if (reds->rules[i] == r)
cd08e51e
AD
212 return i;
213 return -1;
214}
215
216
17ee7397
PE
217/*--------------------.
218| Set the errs of S. |
219`--------------------*/
8b752b00
AD
220
221void
17ee7397 222state_errs_set (state *s, int num, symbol **tokens)
8b752b00 223{
17ee7397 224 if (s->errs)
ec14f0c8 225 abort ();
17ee7397 226 s->errs = errs_new (num, tokens);
8a731ca8
AD
227}
228
229
32e1e0a4 230
17ee7397
PE
231/*-----------------------------------------------------.
232| Print on OUT all the lookaheads such that S wants to |
233| reduce R. |
234`-----------------------------------------------------*/
10e5b8bd
AD
235
236void
17ee7397 237state_rule_lookaheads_print (state *s, rule *r, FILE *out)
10e5b8bd 238{
cd08e51e 239 /* Find the reduction we are handling. */
17ee7397
PE
240 reductions *reds = s->reductions;
241 int red = state_reduction_find (s, r);
10e5b8bd
AD
242
243 /* Print them if there are. */
cd08e51e 244 if (reds->lookaheads && red != -1)
10e5b8bd 245 {
cd08e51e
AD
246 bitset_iterator biter;
247 int k;
248 int not_first = 0;
10e5b8bd 249 fprintf (out, " [");
cd08e51e
AD
250 BITSET_FOR_EACH (biter, reds->lookaheads[red], k, 0)
251 fprintf (out, "%s%s",
252 not_first++ ? ", " : "",
253 symbols[k]->tag);
10e5b8bd
AD
254 fprintf (out, "]");
255 }
256}
c7ca99d4
AD
257
258
259/*----------------------.
260| A state hash table. |
261`----------------------*/
262
263/* Initial capacity of states hash table. */
264#define HT_INITIAL_CAPACITY 257
265
266static struct hash_table *state_table = NULL;
267
268/* Two states are equal if they have the same core items. */
269static bool
17ee7397 270state_compare (state const *s1, state const *s2)
c7ca99d4
AD
271{
272 int i;
273
274 if (s1->nitems != s2->nitems)
8307162d 275 return false;
c7ca99d4
AD
276
277 for (i = 0; i < s1->nitems; ++i)
278 if (s1->items[i] != s2->items[i])
8307162d 279 return false;
c7ca99d4 280
8307162d 281 return true;
c7ca99d4
AD
282}
283
284static unsigned int
17ee7397 285state_hash (state const *s, unsigned int tablesize)
c7ca99d4
AD
286{
287 /* Add up the state's item numbers to get a hash key. */
288 int key = 0;
289 int i;
17ee7397
PE
290 for (i = 0; i < s->nitems; ++i)
291 key += s->items[i];
c7ca99d4
AD
292 return key % tablesize;
293}
294
295
296/*-------------------------------.
297| Create the states hash table. |
298`-------------------------------*/
299
300void
301state_hash_new (void)
302{
303 state_table = hash_initialize (HT_INITIAL_CAPACITY,
304 NULL,
305 (Hash_hasher) state_hash,
306 (Hash_comparator) state_compare,
307 (Hash_data_freer) NULL);
308}
309
310
311/*---------------------------------------------.
312| Free the states hash table, not the states. |
313`---------------------------------------------*/
314
315void
316state_hash_free (void)
317{
318 hash_free (state_table);
319}
320
321
17ee7397
PE
322/*-----------------------------------.
323| Insert S in the state hash table. |
324`-----------------------------------*/
c7ca99d4
AD
325
326void
17ee7397 327state_hash_insert (state *s)
c7ca99d4 328{
17ee7397 329 hash_insert (state_table, s);
c7ca99d4
AD
330}
331
332
333/*------------------------------------------------------------------.
334| Find the state associated to the CORE, and return it. If it does |
335| not exist yet, return NULL. |
336`------------------------------------------------------------------*/
337
17ee7397
PE
338state *
339state_hash_lookup (size_t core_size, item_number *core)
c7ca99d4 340{
17ee7397
PE
341 state *probe = STATE_ALLOC (core_size);
342 state *entry;
c7ca99d4
AD
343
344 probe->nitems = core_size;
345 memcpy (probe->items, core, core_size * sizeof (core[0]));
346 entry = hash_lookup (state_table, probe);
347 free (probe);
348 return entry;
349}
350
351/* All the decorated states, indexed by the state number. */
17ee7397 352state **states = NULL;
c7ca99d4
AD
353
354
355/*----------------------.
356| Free all the states. |
357`----------------------*/
358
359void
360states_free (void)
361{
17ee7397 362 state_number i;
c7ca99d4 363 for (i = 0; i < nstates; ++i)
8b752b00
AD
364 state_free (states[i]);
365 free (states);
c7ca99d4 366}