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. */
29 /*-------------------.
31 `-------------------*/
34 /*---------------------------------------.
35 | Create a new array of N shifts/gotos. |
36 `---------------------------------------*/
38 #define TRANSITIONS_ALLOC(Nshifts) \
39 (transitions_t *) xcalloc ((unsigned) (sizeof (transitions_t) \
40 + (Nshifts - 1) * sizeof (state_number_t)), 1)
42 static transitions_t
*
43 transitions_new (int num
, state_number_t
*the_states
)
45 transitions_t
*res
= TRANSITIONS_ALLOC (num
);
47 memcpy (res
->states
, the_states
, num
* sizeof (the_states
[0]));
52 /*-------------------------------------------------------------------.
53 | Return the state such these TRANSITIONS contain a shift/goto to it |
54 | on SYMBOL. Aborts if none found. |
55 `-------------------------------------------------------------------*/
58 transitions_to (transitions_t
*shifts
, symbol_number_t s
)
61 for (j
= 0; j
< shifts
->num
; j
++)
62 if (TRANSITION_SYMBOL (shifts
, j
) == s
)
63 return states
[shifts
->states
[j
]];
68 /*--------------------.
69 | Error transitions. |
70 `--------------------*/
73 /*-------------------------------.
74 | Create a new array of N errs. |
75 `-------------------------------*/
77 #define ERRS_ALLOC(Nerrs) \
78 (errs_t *) xcalloc ((sizeof (errs_t) \
79 + (Nerrs - 1) * sizeof (symbol_number_t)), 1)
83 errs_new (int num
, symbol_number_t
*tokens
)
85 errs_t
*res
= ERRS_ALLOC (num
);
87 memcpy (res
->symbols
, tokens
, num
* sizeof (tokens
[0]));
99 /*-------------------------------------.
100 | Create a new array of N reductions. |
101 `-------------------------------------*/
103 #define REDUCTIONS_ALLOC(Nreductions) \
104 (reductions_t *) xcalloc ((sizeof (reductions_t) \
105 + (Nreductions - 1) * sizeof (rule_number_t)), 1)
107 static reductions_t
*
108 reductions_new (int num
, rule_number_t
*reductions
)
110 reductions_t
*res
= REDUCTIONS_ALLOC (num
);
112 memcpy (res
->rules
, reductions
, num
* sizeof (reductions
[0]));
123 state_number_t nstates
= 0;
124 /* FINAL_STATE is properly set by new_state when it recognizes its
125 accessing symbol: EOF. */
126 state_t
*final_state
= NULL
;
128 #define STATE_ALLOC(Nitems) \
129 (state_t *) xcalloc ((unsigned) (sizeof (state_t) \
130 + (Nitems - 1) * sizeof (item_number_t)), 1)
132 /*------------------------------------------------------------------.
133 | Create a new state with ACCESSING_SYMBOL, for those items. Store |
134 | it in the state hash table. |
135 `------------------------------------------------------------------*/
138 state_new (symbol_number_t accessing_symbol
,
139 size_t core_size
, item_number_t
*core
)
143 if (nstates
>= STATE_NUMBER_MAX
)
144 fatal (_("too many states (max %d)"), STATE_NUMBER_MAX
);
146 res
= STATE_ALLOC (core_size
);
147 res
->accessing_symbol
= accessing_symbol
;
148 res
->number
= nstates
;
150 res
->solved_conflicts
= NULL
;
152 res
->nitems
= core_size
;
153 memcpy (res
->items
, core
, core_size
* sizeof (core
[0]));
155 state_hash_insert (res
);
166 state_free (state_t
*state
)
168 free (state
->transitions
);
169 free (state
->reductions
);
175 /*-------------------------------.
176 | Set the transitions of STATE. |
177 `-------------------------------*/
180 state_transitions_set (state_t
*state
, int num
, state_number_t
*transitions
)
182 assert (!state
->transitions
);
183 state
->transitions
= transitions_new (num
, transitions
);
187 /*------------------------------.
188 | Set the reductions of STATE. |
189 `------------------------------*/
192 state_reductions_set (state_t
*state
, int num
, rule_number_t
*reductions
)
194 assert (!state
->reductions
);
195 state
->reductions
= reductions_new (num
, reductions
);
199 /*------------------------.
200 | Set the errs of STATE. |
201 `------------------------*/
204 state_errs_set (state_t
*state
, int num
, symbol_number_t
*tokens
)
206 assert (!state
->errs
);
207 state
->errs
= errs_new (num
, tokens
);
212 /*--------------------------------------------------------------.
213 | Print on OUT all the lookaheads such that this STATE wants to |
214 | reduce this RULE. |
215 `--------------------------------------------------------------*/
218 state_rule_lookaheads_print (state_t
*state
, rule_t
*rule
, FILE *out
)
221 bitset_iterator biter
;
223 /* Count the number of lookaheads corresponding to this rule. */
224 for (j
= 0; j
< state
->nlookaheads
; ++j
)
225 BITSET_FOR_EACH (biter
, state
->lookaheads
[j
], k
, 0)
226 if (state
->lookaheads_rule
[j
]->number
== rule
->number
)
229 /* Print them if there are. */
233 for (j
= 0; j
< state
->nlookaheads
; ++j
)
234 BITSET_FOR_EACH (biter
, state
->lookaheads
[j
], k
, 0)
235 if (state
->lookaheads_rule
[j
]->number
== rule
->number
)
236 fprintf (out
, "%s%s",
238 --nlookaheads
? ", " : "");
244 /*----------------------.
245 | A state hash table. |
246 `----------------------*/
248 /* Initial capacity of states hash table. */
249 #define HT_INITIAL_CAPACITY 257
251 static struct hash_table
*state_table
= NULL
;
253 /* Two states are equal if they have the same core items. */
255 state_compare (const state_t
*s1
, const state_t
*s2
)
259 if (s1
->nitems
!= s2
->nitems
)
262 for (i
= 0; i
< s1
->nitems
; ++i
)
263 if (s1
->items
[i
] != s2
->items
[i
])
270 state_hash (const state_t
*state
, unsigned int tablesize
)
272 /* Add up the state's item numbers to get a hash key. */
275 for (i
= 0; i
< state
->nitems
; ++i
)
276 key
+= state
->items
[i
];
277 return key
% tablesize
;
281 /*-------------------------------.
282 | Create the states hash table. |
283 `-------------------------------*/
286 state_hash_new (void)
288 state_table
= hash_initialize (HT_INITIAL_CAPACITY
,
290 (Hash_hasher
) state_hash
,
291 (Hash_comparator
) state_compare
,
292 (Hash_data_freer
) NULL
);
296 /*---------------------------------------------.
297 | Free the states hash table, not the states. |
298 `---------------------------------------------*/
301 state_hash_free (void)
303 hash_free (state_table
);
307 /*---------------------------------------.
308 | Insert STATE in the state hash table. |
309 `---------------------------------------*/
312 state_hash_insert (state_t
*state
)
314 hash_insert (state_table
, state
);
318 /*------------------------------------------------------------------.
319 | Find the state associated to the CORE, and return it. If it does |
320 | not exist yet, return NULL. |
321 `------------------------------------------------------------------*/
324 state_hash_lookup (size_t core_size
, item_number_t
*core
)
326 state_t
*probe
= STATE_ALLOC (core_size
);
329 probe
->nitems
= core_size
;
330 memcpy (probe
->items
, core
, core_size
* sizeof (core
[0]));
331 entry
= hash_lookup (state_table
, probe
);
336 /* All the decorated states, indexed by the state number. */
337 state_t
**states
= NULL
;
340 /*----------------------.
341 | Free all the states. |
342 `----------------------*/
348 for (i
= 0; i
< nstates
; ++i
)
349 state_free (states
[i
]);