1 /* Type definitions for nondeterministic finite state machine for Bison.
3 Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
5 This file is part of Bison, the GNU Compiler Compiler.
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)
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.
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
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
32 /*-------------------.
34 `-------------------*/
37 /*-----------------------------------------.
38 | Create a new array of NUM shifts/gotos. |
39 `-----------------------------------------*/
42 transitions_new (int num
, state
**the_states
)
44 size_t states_size
= num
* sizeof *the_states
;
45 transitions
*res
= xmalloc (offsetof (transitions
, states
) + states_size
);
47 memcpy (res
->states
, the_states
, states_size
);
52 /*-------------------------------------------------------.
53 | Return the state such that SHIFTS contain a shift/goto |
54 | to it on SYM. Abort if none found. |
55 `-------------------------------------------------------*/
58 transitions_to (transitions
*shifts
, symbol_number sym
)
61 for (j
= 0; j
< shifts
->num
; j
++)
62 if (TRANSITION_SYMBOL (shifts
, j
) == sym
)
63 return shifts
->states
[j
];
68 /*--------------------.
69 | Error transitions. |
70 `--------------------*/
73 /*---------------------------------.
74 | Create a new array of NUM errs. |
75 `---------------------------------*/
78 errs_new (int num
, symbol
**tokens
)
80 size_t symbols_size
= num
* sizeof *tokens
;
81 errs
*res
= xmalloc (offsetof (errs
, symbols
) + symbols_size
);
83 memcpy (res
->symbols
, tokens
, symbols_size
);
95 /*---------------------------------------.
96 | Create a new array of NUM reductions. |
97 `---------------------------------------*/
100 reductions_new (int num
, rule
**reds
)
102 size_t rules_size
= num
* sizeof *reds
;
103 reductions
*res
= xmalloc (offsetof (reductions
, rules
) + rules_size
);
105 res
->lookaheads
= NULL
;
106 memcpy (res
->rules
, reds
, rules_size
);
117 state_number nstates
= 0;
118 /* FINAL_STATE is properly set by new_state when it recognizes its
119 accessing symbol: $end. */
120 state
*final_state
= NULL
;
123 /*------------------------------------------------------------------.
124 | Create a new state with ACCESSING_SYMBOL, for those items. Store |
125 | it in the state hash table. |
126 `------------------------------------------------------------------*/
129 state_new (symbol_number accessing_symbol
,
130 size_t nitems
, item_number
*core
)
133 size_t items_size
= nitems
* sizeof *core
;
135 if (STATE_NUMBER_MAXIMUM
<= nstates
)
138 res
= xmalloc (offsetof (state
, items
) + items_size
);
139 res
->number
= nstates
++;
140 res
->accessing_symbol
= accessing_symbol
;
141 res
->transitions
= NULL
;
142 res
->reductions
= NULL
;
145 res
->solved_conflicts
= NULL
;
147 res
->nitems
= nitems
;
148 memcpy (res
->items
, core
, items_size
);
150 state_hash_insert (res
);
161 state_free (state
*s
)
163 free (s
->transitions
);
164 free (s
->reductions
);
170 /*---------------------------.
171 | Set the transitions of S. |
172 `---------------------------*/
175 state_transitions_set (state
*s
, int num
, state
**trans
)
179 s
->transitions
= transitions_new (num
, trans
);
183 /*--------------------------.
184 | Set the reductions of S. |
185 `--------------------------*/
188 state_reductions_set (state
*s
, int num
, rule
**reds
)
192 s
->reductions
= reductions_new (num
, reds
);
197 state_reduction_find (state
*s
, rule
*r
)
200 reductions
*reds
= s
->reductions
;
201 for (i
= 0; i
< reds
->num
; ++i
)
202 if (reds
->rules
[i
] == r
)
208 /*--------------------.
209 | Set the errs of S. |
210 `--------------------*/
213 state_errs_set (state
*s
, int num
, symbol
**tokens
)
217 s
->errs
= errs_new (num
, tokens
);
222 /*-----------------------------------------------------.
223 | Print on OUT all the lookaheads such that S wants to |
225 `-----------------------------------------------------*/
228 state_rule_lookaheads_print (state
*s
, rule
*r
, FILE *out
)
230 /* Find the reduction we are handling. */
231 reductions
*reds
= s
->reductions
;
232 int red
= state_reduction_find (s
, r
);
234 /* Print them if there are. */
235 if (reds
->lookaheads
&& red
!= -1)
237 bitset_iterator biter
;
241 BITSET_FOR_EACH (biter
, reds
->lookaheads
[red
], k
, 0)
242 fprintf (out
, "%s%s",
243 not_first
++ ? ", " : "",
250 /*----------------------.
251 | A state hash table. |
252 `----------------------*/
254 /* Initial capacity of states hash table. */
255 #define HT_INITIAL_CAPACITY 257
257 static struct hash_table
*state_table
= NULL
;
259 /* Two states are equal if they have the same core items. */
261 state_compare (state
const *s1
, state
const *s2
)
265 if (s1
->nitems
!= s2
->nitems
)
268 for (i
= 0; i
< s1
->nitems
; ++i
)
269 if (s1
->items
[i
] != s2
->items
[i
])
276 state_comparator (void const *s1
, void const *s2
)
278 return state_compare (s1
, s2
);
281 static inline unsigned int
282 state_hash (state
const *s
, unsigned int tablesize
)
284 /* Add up the state's item numbers to get a hash key. */
285 unsigned int key
= 0;
287 for (i
= 0; i
< s
->nitems
; ++i
)
289 return key
% tablesize
;
293 state_hasher (void const *s
, unsigned int tablesize
)
295 return state_hash (s
, tablesize
);
299 /*-------------------------------.
300 | Create the states hash table. |
301 `-------------------------------*/
304 state_hash_new (void)
306 state_table
= hash_initialize (HT_INITIAL_CAPACITY
,
314 /*---------------------------------------------.
315 | Free the states hash table, not the states. |
316 `---------------------------------------------*/
319 state_hash_free (void)
321 hash_free (state_table
);
325 /*-----------------------------------.
326 | Insert S in the state hash table. |
327 `-----------------------------------*/
330 state_hash_insert (state
*s
)
332 hash_insert (state_table
, s
);
336 /*------------------------------------------------------------------.
337 | Find the state associated to the CORE, and return it. If it does |
338 | not exist yet, return NULL. |
339 `------------------------------------------------------------------*/
342 state_hash_lookup (size_t nitems
, item_number
*core
)
344 size_t items_size
= nitems
* sizeof *core
;
345 state
*probe
= xmalloc (offsetof (state
, items
) + items_size
);
348 probe
->nitems
= nitems
;
349 memcpy (probe
->items
, core
, items_size
);
350 entry
= hash_lookup (state_table
, probe
);
355 /* All the decorated states, indexed by the state number. */
356 state
**states
= NULL
;
359 /*----------------------.
360 | Free all the states. |
361 `----------------------*/
367 for (i
= 0; i
< nstates
; ++i
)
368 state_free (states
[i
]);