]>
Commit | Line | Data |
---|---|---|
5e893e1c | 1 | /* Type definitions for nondeterministic finite state machine for bison, |
62a3e4f0 | 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" | |
c7ca99d4 | 23 | #include "hash.h" |
df0e7316 | 24 | #include "complain.h" |
62a3e4f0 | 25 | #include "gram.h" |
5e893e1c AD |
26 | #include "state.h" |
27 | ||
df0e7316 AD |
28 | |
29 | /*-------------------. | |
30 | | Shifts and Gotos. | | |
31 | `-------------------*/ | |
32 | ||
33 | ||
c7ca99d4 AD |
34 | /*---------------------------------------. |
35 | | Create a new array of N shifts/gotos. | | |
36 | `---------------------------------------*/ | |
5e893e1c | 37 | |
2cec70b9 | 38 | #define SHIFTS_ALLOC(Nshifts) \ |
32e1e0a4 AD |
39 | (shifts_t *) xcalloc ((unsigned) (sizeof (shifts_t) \ |
40 | + (Nshifts - 1) * sizeof (state_number_t)), 1) | |
2cec70b9 | 41 | |
32e1e0a4 AD |
42 | static shifts_t * |
43 | shifts_new (int nshifts, state_number_t *shifts) | |
5e893e1c | 44 | { |
32e1e0a4 AD |
45 | shifts_t *res = SHIFTS_ALLOC (nshifts); |
46 | res->nshifts = nshifts; | |
47 | memcpy (res->shifts, shifts, nshifts * sizeof (shifts[0])); | |
5e893e1c AD |
48 | return res; |
49 | } | |
2cec70b9 AD |
50 | |
51 | ||
df0e7316 AD |
52 | |
53 | ||
54 | /*--------------------. | |
55 | | Error transitions. | | |
56 | `--------------------*/ | |
57 | ||
58 | ||
2cec70b9 AD |
59 | /*-------------------------------. |
60 | | Create a new array of N errs. | | |
61 | `-------------------------------*/ | |
62 | ||
63 | #define ERRS_ALLOC(Nerrs) \ | |
64 | (errs *) xcalloc ((unsigned) (sizeof (errs) \ | |
65 | + (Nerrs - 1) * sizeof (short)), 1) | |
66 | ||
67 | ||
68 | errs * | |
69 | errs_new (int n) | |
70 | { | |
71 | errs *res = ERRS_ALLOC (n); | |
72 | res->nerrs = n; | |
73 | return res; | |
74 | } | |
75 | ||
76 | ||
77 | errs * | |
78 | errs_dup (errs *src) | |
79 | { | |
80 | errs *res = errs_new (src->nerrs); | |
82841af7 | 81 | memcpy (res->errs, src->errs, src->nerrs * sizeof (src->errs[0])); |
2cec70b9 AD |
82 | return res; |
83 | } | |
80dac38c | 84 | |
df0e7316 AD |
85 | |
86 | ||
87 | ||
88 | /*-------------. | |
89 | | Reductions. | | |
90 | `-------------*/ | |
91 | ||
92 | ||
80dac38c AD |
93 | /*-------------------------------------. |
94 | | Create a new array of N reductions. | | |
95 | `-------------------------------------*/ | |
96 | ||
97 | #define REDUCTIONS_ALLOC(Nreductions) \ | |
98 | (reductions *) xcalloc ((unsigned) (sizeof (reductions) \ | |
99 | + (Nreductions - 1) * sizeof (short)), 1) | |
100 | ||
101 | reductions * | |
102 | reductions_new (int n) | |
103 | { | |
104 | reductions *res = REDUCTIONS_ALLOC (n); | |
105 | res->nreds = n; | |
106 | return res; | |
107 | } | |
10e5b8bd AD |
108 | |
109 | ||
df0e7316 AD |
110 | |
111 | /*---------. | |
112 | | States. | | |
113 | `---------*/ | |
114 | ||
115 | ||
116 | state_number_t nstates = 0; | |
117 | /* FINAL_STATE is properly set by new_state when it recognizes its | |
118 | accessing symbol: EOF. */ | |
119 | state_t *final_state = NULL; | |
120 | ||
c7ca99d4 AD |
121 | #define STATE_ALLOC(Nitems) \ |
122 | (state_t *) xcalloc ((unsigned) (sizeof (state_t) \ | |
123 | + (Nitems - 1) * sizeof (item_number_t)), 1) | |
124 | ||
df0e7316 AD |
125 | /*------------------------------------------------------------. |
126 | | Create a new state with ACCESSING_SYMBOL, for those items. | | |
127 | `------------------------------------------------------------*/ | |
128 | ||
129 | state_t * | |
130 | state_new (symbol_number_t accessing_symbol, | |
131 | size_t core_size, item_number_t *core) | |
132 | { | |
133 | state_t *res; | |
134 | ||
135 | if (nstates >= STATE_NUMBER_MAX) | |
136 | fatal (_("too many states (max %d)"), STATE_NUMBER_MAX); | |
137 | ||
df0e7316 AD |
138 | res = STATE_ALLOC (core_size); |
139 | res->accessing_symbol = accessing_symbol; | |
140 | res->number = nstates; | |
141 | ++nstates; | |
142 | res->solved_conflicts = NULL; | |
143 | ||
144 | res->nitems = core_size; | |
145 | memcpy (res->items, core, core_size * sizeof (core[0])); | |
146 | ||
147 | return res; | |
148 | } | |
149 | ||
150 | ||
32e1e0a4 AD |
151 | /*--------------------------. |
152 | | Set the shifts of STATE. | | |
153 | `--------------------------*/ | |
154 | ||
155 | void | |
156 | state_shifts_set (state_t *state, int nshifts, state_number_t *shifts) | |
157 | { | |
158 | state->shifts = shifts_new (nshifts, shifts); | |
159 | } | |
160 | ||
161 | ||
162 | ||
10e5b8bd AD |
163 | /*--------------------------------------------------------------. |
164 | | Print on OUT all the lookaheads such that this STATE wants to | | |
165 | | reduce this RULE. | | |
166 | `--------------------------------------------------------------*/ | |
167 | ||
168 | void | |
169 | state_rule_lookaheads_print (state_t *state, rule_t *rule, FILE *out) | |
170 | { | |
171 | int j, k; | |
172 | int nlookaheads = 0; | |
173 | /* Count the number of lookaheads corresponding to this rule. */ | |
174 | for (j = 0; j < state->nlookaheads; ++j) | |
175 | for (k = 0; k < ntokens; ++k) | |
176 | if (bitset_test (state->lookaheads[j], k) | |
177 | && state->lookaheads_rule[j]->number == rule->number) | |
178 | nlookaheads++; | |
179 | ||
180 | /* Print them if there are. */ | |
181 | if (nlookaheads) | |
182 | { | |
183 | fprintf (out, " ["); | |
184 | for (j = 0; j < state->nlookaheads; ++j) | |
185 | for (k = 0; k < ntokens; ++k) | |
186 | if (bitset_test (state->lookaheads[j], k) | |
187 | && state->lookaheads_rule[j]->number == rule->number) | |
188 | fprintf (out, "%s%s", | |
189 | symbol_tag_get (symbols[k]), | |
190 | --nlookaheads ? ", " : ""); | |
191 | fprintf (out, "]"); | |
192 | } | |
193 | } | |
c7ca99d4 AD |
194 | |
195 | ||
196 | /*----------------------. | |
197 | | A state hash table. | | |
198 | `----------------------*/ | |
199 | ||
200 | /* Initial capacity of states hash table. */ | |
201 | #define HT_INITIAL_CAPACITY 257 | |
202 | ||
203 | static struct hash_table *state_table = NULL; | |
204 | ||
205 | /* Two states are equal if they have the same core items. */ | |
206 | static bool | |
207 | state_compare (const state_t *s1, const state_t *s2) | |
208 | { | |
209 | int i; | |
210 | ||
211 | if (s1->nitems != s2->nitems) | |
212 | return FALSE; | |
213 | ||
214 | for (i = 0; i < s1->nitems; ++i) | |
215 | if (s1->items[i] != s2->items[i]) | |
216 | return FALSE; | |
217 | ||
218 | return TRUE; | |
219 | } | |
220 | ||
221 | static unsigned int | |
222 | state_hash (const state_t *state, unsigned int tablesize) | |
223 | { | |
224 | /* Add up the state's item numbers to get a hash key. */ | |
225 | int key = 0; | |
226 | int i; | |
227 | for (i = 0; i < state->nitems; ++i) | |
228 | key += state->items[i]; | |
229 | return key % tablesize; | |
230 | } | |
231 | ||
232 | ||
233 | /*-------------------------------. | |
234 | | Create the states hash table. | | |
235 | `-------------------------------*/ | |
236 | ||
237 | void | |
238 | state_hash_new (void) | |
239 | { | |
240 | state_table = hash_initialize (HT_INITIAL_CAPACITY, | |
241 | NULL, | |
242 | (Hash_hasher) state_hash, | |
243 | (Hash_comparator) state_compare, | |
244 | (Hash_data_freer) NULL); | |
245 | } | |
246 | ||
247 | ||
248 | /*---------------------------------------------. | |
249 | | Free the states hash table, not the states. | | |
250 | `---------------------------------------------*/ | |
251 | ||
252 | void | |
253 | state_hash_free (void) | |
254 | { | |
255 | hash_free (state_table); | |
256 | } | |
257 | ||
258 | ||
259 | /*---------------------------------------. | |
260 | | Insert STATE in the state hash table. | | |
261 | `---------------------------------------*/ | |
262 | ||
263 | void | |
264 | state_hash_insert (state_t *state) | |
265 | { | |
266 | hash_insert (state_table, state); | |
267 | } | |
268 | ||
269 | ||
270 | /*------------------------------------------------------------------. | |
271 | | Find the state associated to the CORE, and return it. If it does | | |
272 | | not exist yet, return NULL. | | |
273 | `------------------------------------------------------------------*/ | |
274 | ||
275 | state_t * | |
276 | state_hash_lookup (size_t core_size, item_number_t *core) | |
277 | { | |
278 | state_t *probe = STATE_ALLOC (core_size); | |
279 | state_t *entry; | |
280 | ||
281 | probe->nitems = core_size; | |
282 | memcpy (probe->items, core, core_size * sizeof (core[0])); | |
283 | entry = hash_lookup (state_table, probe); | |
284 | free (probe); | |
285 | return entry; | |
286 | } | |
287 | ||
288 | /* All the decorated states, indexed by the state number. */ | |
289 | state_t **states = NULL; | |
290 | ||
291 | ||
292 | /*----------------------. | |
293 | | Free all the states. | | |
294 | `----------------------*/ | |
295 | ||
296 | void | |
297 | states_free (void) | |
298 | { | |
299 | state_number_t i; | |
300 | ||
301 | for (i = 0; i < nstates; ++i) | |
302 | { | |
303 | free (states[i]->shifts); | |
304 | XFREE (states[i]->reductions); | |
305 | free (states[i]->errs); | |
306 | free (states[i]); | |
307 | } | |
308 | XFREE (states); | |
309 | } |