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