1 /* Type definitions for nondeterministic finite state machine for Bison.
3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 Free Software
6 This file is part of Bison, the GNU Compiler Compiler.
8 Bison is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 Bison is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Bison; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
33 /*-------------------.
35 `-------------------*/
38 /*-----------------------------------------.
39 | Create a new array of NUM shifts/gotos. |
40 `-----------------------------------------*/
43 transitions_new (int num
, state
**the_states
)
45 size_t states_size
= num
* sizeof *the_states
;
46 transitions
*res
= xmalloc (offsetof (transitions
, states
) + states_size
);
48 memcpy (res
->states
, the_states
, states_size
);
53 /*-------------------------------------------------------.
54 | Return the state such that SHIFTS contain a shift/goto |
55 | to it on SYM. Abort if none found. |
56 `-------------------------------------------------------*/
59 transitions_to (transitions
*shifts
, symbol_number sym
)
66 if (TRANSITION_SYMBOL (shifts
, j
) == sym
)
67 return shifts
->states
[j
];
72 /*--------------------.
73 | Error transitions. |
74 `--------------------*/
77 /*---------------------------------.
78 | Create a new array of NUM errs. |
79 `---------------------------------*/
82 errs_new (int num
, symbol
**tokens
)
84 size_t symbols_size
= num
* sizeof *tokens
;
85 errs
*res
= xmalloc (offsetof (errs
, symbols
) + symbols_size
);
87 memcpy (res
->symbols
, tokens
, symbols_size
);
99 /*---------------------------------------.
100 | Create a new array of NUM reductions. |
101 `---------------------------------------*/
104 reductions_new (int num
, rule
**reds
)
106 size_t rules_size
= num
* sizeof *reds
;
107 reductions
*res
= xmalloc (offsetof (reductions
, rules
) + rules_size
);
109 res
->look_ahead_tokens
= NULL
;
110 memcpy (res
->rules
, reds
, rules_size
);
121 state_number nstates
= 0;
122 /* FINAL_STATE is properly set by new_state when it recognizes its
123 accessing symbol: $end. */
124 state
*final_state
= NULL
;
127 /*------------------------------------------------------------------.
128 | Create a new state with ACCESSING_SYMBOL, for those items. Store |
129 | it in the state hash table. |
130 `------------------------------------------------------------------*/
133 state_new (symbol_number accessing_symbol
,
134 size_t nitems
, item_number
*core
)
137 size_t items_size
= nitems
* sizeof *core
;
139 if (STATE_NUMBER_MAXIMUM
<= nstates
)
142 res
= xmalloc (offsetof (state
, items
) + items_size
);
143 res
->number
= nstates
++;
144 res
->accessing_symbol
= accessing_symbol
;
145 res
->transitions
= NULL
;
146 res
->reductions
= NULL
;
149 res
->solved_conflicts
= NULL
;
151 res
->nitems
= nitems
;
152 memcpy (res
->items
, core
, items_size
);
154 state_hash_insert (res
);
165 state_free (state
*s
)
167 free (s
->transitions
);
168 free (s
->reductions
);
174 /*---------------------------.
175 | Set the transitions of S. |
176 `---------------------------*/
179 state_transitions_set (state
*s
, int num
, state
**trans
)
181 assert (!s
->transitions
);
182 s
->transitions
= transitions_new (num
, trans
);
186 /*--------------------------.
187 | Set the reductions of S. |
188 `--------------------------*/
191 state_reductions_set (state
*s
, int num
, rule
**reds
)
193 assert (!s
->reductions
);
194 s
->reductions
= reductions_new (num
, reds
);
199 state_reduction_find (state
*s
, rule
*r
)
202 reductions
*reds
= s
->reductions
;
203 for (i
= 0; i
< reds
->num
; ++i
)
204 if (reds
->rules
[i
] == r
)
210 /*--------------------.
211 | Set the errs of S. |
212 `--------------------*/
215 state_errs_set (state
*s
, int num
, symbol
**tokens
)
219 s
->errs
= errs_new (num
, tokens
);
224 /*---------------------------------------------------.
225 | Print on OUT all the look-ahead tokens such that S |
226 | wants to reduce R. |
227 `---------------------------------------------------*/
230 state_rule_look_ahead_tokens_print (state
*s
, rule
*r
, FILE *out
)
232 /* Find the reduction we are handling. */
233 reductions
*reds
= s
->reductions
;
234 int red
= state_reduction_find (s
, r
);
236 /* Print them if there are. */
237 if (reds
->look_ahead_tokens
&& red
!= -1)
239 bitset_iterator biter
;
241 char const *sep
= "";
243 BITSET_FOR_EACH (biter
, reds
->look_ahead_tokens
[red
], k
, 0)
245 fprintf (out
, "%s%s", sep
, symbols
[k
]->tag
);
253 /*---------------------.
254 | A state hash table. |
255 `---------------------*/
257 /* Initial capacity of states hash table. */
258 #define HT_INITIAL_CAPACITY 257
260 static struct hash_table
*state_table
= NULL
;
262 /* Two states are equal if they have the same core items. */
264 state_compare (state
const *s1
, state
const *s2
)
268 if (s1
->nitems
!= s2
->nitems
)
271 for (i
= 0; i
< s1
->nitems
; ++i
)
272 if (s1
->items
[i
] != s2
->items
[i
])
279 state_comparator (void const *s1
, void const *s2
)
281 return state_compare (s1
, s2
);
285 state_hash (state
const *s
, size_t 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 state_hasher (void const *s
, size_t tablesize
)
298 return state_hash (s
, tablesize
);
302 /*-------------------------------.
303 | Create the states hash table. |
304 `-------------------------------*/
307 state_hash_new (void)
309 state_table
= hash_initialize (HT_INITIAL_CAPACITY
,
317 /*---------------------------------------------.
318 | Free the states hash table, not the states. |
319 `---------------------------------------------*/
322 state_hash_free (void)
324 hash_free (state_table
);
328 /*-----------------------------------.
329 | Insert S in the state hash table. |
330 `-----------------------------------*/
333 state_hash_insert (state
*s
)
335 hash_insert (state_table
, s
);
339 /*------------------------------------------------------------------.
340 | Find the state associated to the CORE, and return it. If it does |
341 | not exist yet, return NULL. |
342 `------------------------------------------------------------------*/
345 state_hash_lookup (size_t nitems
, item_number
*core
)
347 size_t items_size
= nitems
* sizeof *core
;
348 state
*probe
= xmalloc (offsetof (state
, items
) + items_size
);
351 probe
->nitems
= nitems
;
352 memcpy (probe
->items
, core
, items_size
);
353 entry
= hash_lookup (state_table
, probe
);
358 /* All the decorated states, indexed by the state number. */
359 state
**states
= NULL
;
362 /*----------------------.
363 | Free all the states. |
364 `----------------------*/
370 for (i
= 0; i
< nstates
; ++i
)
371 state_free (states
[i
]);