1 /* Type definitions for nondeterministic finite state machine for Bison.
2 Copyright (C) 2001, 2002 Free Software Foundation, Inc.
4 This file is part of Bison, the GNU Compiler Compiler.
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)
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.
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. */
31 /*-------------------.
33 `-------------------*/
36 /*---------------------------------------.
37 | Create a new array of N shifts/gotos. |
38 `---------------------------------------*/
40 #define TRANSITIONS_ALLOC(Num) \
41 (transitions *) xcalloc ((sizeof (transitions) \
42 + (Num - 1) * sizeof (state *)), \
46 transitions_new (int num
, state
**the_states
)
48 transitions
*res
= TRANSITIONS_ALLOC (num
);
50 memcpy (res
->states
, the_states
, num
* sizeof (the_states
[0]));
55 /*-------------------------------------------------------------------.
56 | Return the state such these TRANSITIONS contain a shift/goto to it |
57 | on S. Abort if none found. |
58 `-------------------------------------------------------------------*/
61 transitions_to (transitions
*shifts
, symbol_number s
)
64 for (j
= 0; j
< shifts
->num
; j
++)
65 if (TRANSITION_SYMBOL (shifts
, j
) == s
)
66 return shifts
->states
[j
];
71 /*--------------------.
72 | Error transitions. |
73 `--------------------*/
76 /*-------------------------------.
77 | Create a new array of N errs. |
78 `-------------------------------*/
80 #define ERRS_ALLOC(Nerrs) \
81 ((errs *) xcalloc ((sizeof (errs) + (Nerrs - 1) * sizeof (symbol *)), 1))
85 errs_new (int num
, symbol
**tokens
)
87 errs
*res
= ERRS_ALLOC (num
);
89 memcpy (res
->symbols
, tokens
, num
* sizeof (tokens
[0]));
101 /*-------------------------------------.
102 | Create a new array of N reductions. |
103 `-------------------------------------*/
105 #define REDUCTIONS_ALLOC(Nreductions) \
106 (reductions *) xcalloc ((sizeof (reductions) \
107 + (Nreductions - 1) * sizeof (rule *)), 1)
110 reductions_new (int num
, rule
**reds
)
112 reductions
*res
= REDUCTIONS_ALLOC (num
);
114 memcpy (res
->rules
, reds
, num
* sizeof (reds
[0]));
115 res
->lookaheads
= NULL
;
126 state_number nstates
= 0;
127 /* FINAL_STATE is properly set by new_state when it recognizes its
128 accessing symbol: $end. */
129 state
*final_state
= NULL
;
131 #define STATE_ALLOC(Nitems) \
132 (state *) xcalloc ((sizeof (state) \
133 + (Nitems - 1) * sizeof (item_number)), \
136 /*------------------------------------------------------------------.
137 | Create a new state with ACCESSING_SYMBOL, for those items. Store |
138 | it in the state hash table. |
139 `------------------------------------------------------------------*/
142 state_new (symbol_number accessing_symbol
,
143 size_t core_size
, item_number
*core
)
147 if (STATE_NUMBER_MAXIMUM
<= nstates
)
150 res
= STATE_ALLOC (core_size
);
151 res
->accessing_symbol
= accessing_symbol
;
152 res
->number
= nstates
;
154 res
->solved_conflicts
= NULL
;
156 res
->nitems
= core_size
;
157 memcpy (res
->items
, core
, core_size
* sizeof (core
[0]));
159 state_hash_insert (res
);
170 state_free (state
*s
)
172 free (s
->transitions
);
173 free (s
->reductions
);
179 /*---------------------------.
180 | Set the transitions of S. |
181 `---------------------------*/
184 state_transitions_set (state
*s
, int num
, state
**trans
)
188 s
->transitions
= transitions_new (num
, trans
);
192 /*--------------------------.
193 | Set the reductions of S. |
194 `--------------------------*/
197 state_reductions_set (state
*s
, int num
, rule
**reds
)
201 s
->reductions
= reductions_new (num
, reds
);
206 state_reduction_find (state
*s
, rule
*r
)
209 reductions
*reds
= s
->reductions
;
210 for (i
= 0; i
< reds
->num
; ++i
)
211 if (reds
->rules
[i
] == r
)
217 /*--------------------.
218 | Set the errs of S. |
219 `--------------------*/
222 state_errs_set (state
*s
, int num
, symbol
**tokens
)
226 s
->errs
= errs_new (num
, tokens
);
231 /*-----------------------------------------------------.
232 | Print on OUT all the lookaheads such that S wants to |
234 `-----------------------------------------------------*/
237 state_rule_lookaheads_print (state
*s
, rule
*r
, FILE *out
)
239 /* Find the reduction we are handling. */
240 reductions
*reds
= s
->reductions
;
241 int red
= state_reduction_find (s
, r
);
243 /* Print them if there are. */
244 if (reds
->lookaheads
&& red
!= -1)
246 bitset_iterator biter
;
250 BITSET_FOR_EACH (biter
, reds
->lookaheads
[red
], k
, 0)
251 fprintf (out
, "%s%s",
252 not_first
++ ? ", " : "",
259 /*----------------------.
260 | A state hash table. |
261 `----------------------*/
263 /* Initial capacity of states hash table. */
264 #define HT_INITIAL_CAPACITY 257
266 static struct hash_table
*state_table
= NULL
;
268 /* Two states are equal if they have the same core items. */
270 state_compare (state
const *s1
, state
const *s2
)
274 if (s1
->nitems
!= s2
->nitems
)
277 for (i
= 0; i
< s1
->nitems
; ++i
)
278 if (s1
->items
[i
] != s2
->items
[i
])
285 state_hash (state
const *s
, unsigned int tablesize
)
287 /* Add up the state's item numbers to get a hash key. */
290 for (i
= 0; i
< s
->nitems
; ++i
)
292 return key
% tablesize
;
296 /*-------------------------------.
297 | Create the states hash table. |
298 `-------------------------------*/
301 state_hash_new (void)
303 state_table
= hash_initialize (HT_INITIAL_CAPACITY
,
305 (Hash_hasher
) state_hash
,
306 (Hash_comparator
) state_compare
,
307 (Hash_data_freer
) NULL
);
311 /*---------------------------------------------.
312 | Free the states hash table, not the states. |
313 `---------------------------------------------*/
316 state_hash_free (void)
318 hash_free (state_table
);
322 /*-----------------------------------.
323 | Insert S in the state hash table. |
324 `-----------------------------------*/
327 state_hash_insert (state
*s
)
329 hash_insert (state_table
, s
);
333 /*------------------------------------------------------------------.
334 | Find the state associated to the CORE, and return it. If it does |
335 | not exist yet, return NULL. |
336 `------------------------------------------------------------------*/
339 state_hash_lookup (size_t core_size
, item_number
*core
)
341 state
*probe
= STATE_ALLOC (core_size
);
344 probe
->nitems
= core_size
;
345 memcpy (probe
->items
, core
, core_size
* sizeof (core
[0]));
346 entry
= hash_lookup (state_table
, probe
);
351 /* All the decorated states, indexed by the state number. */
352 state
**states
= NULL
;
355 /*----------------------.
356 | Free all the states. |
357 `----------------------*/
363 for (i
= 0; i
< nstates
; ++i
)
364 state_free (states
[i
]);