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
)
64 aver (j
< shifts
->num
);
65 if (TRANSITION_SYMBOL (shifts
, j
) == sym
)
66 return shifts
->states
[j
];
71 /*--------------------.
72 | Error transitions. |
73 `--------------------*/
76 /*---------------------------------.
77 | Create a new array of NUM errs. |
78 `---------------------------------*/
81 errs_new (int num
, symbol
**tokens
)
83 size_t symbols_size
= num
* sizeof *tokens
;
84 errs
*res
= xmalloc (offsetof (errs
, symbols
) + symbols_size
);
86 memcpy (res
->symbols
, tokens
, symbols_size
);
98 /*---------------------------------------.
99 | Create a new array of NUM reductions. |
100 `---------------------------------------*/
103 reductions_new (int num
, rule
**reds
)
105 size_t rules_size
= num
* sizeof *reds
;
106 reductions
*res
= xmalloc (offsetof (reductions
, rules
) + rules_size
);
108 res
->lookahead_tokens
= NULL
;
109 memcpy (res
->rules
, reds
, rules_size
);
120 state_number nstates
= 0;
121 /* FINAL_STATE is properly set by new_state when it recognizes its
122 accessing symbol: $end. */
123 state
*final_state
= NULL
;
126 /*------------------------------------------------------------------.
127 | Create a new state with ACCESSING_SYMBOL, for those items. Store |
128 | it in the state hash table. |
129 `------------------------------------------------------------------*/
132 state_new (symbol_number accessing_symbol
,
133 size_t nitems
, item_number
*core
)
136 size_t items_size
= nitems
* sizeof *core
;
138 aver (nstates
< STATE_NUMBER_MAXIMUM
);
140 res
= xmalloc (offsetof (state
, items
) + items_size
);
141 res
->number
= nstates
++;
142 res
->accessing_symbol
= accessing_symbol
;
143 res
->transitions
= NULL
;
144 res
->reductions
= NULL
;
147 res
->solved_conflicts
= NULL
;
148 res
->reachable
= false;
150 res
->nitems
= nitems
;
151 memcpy (res
->items
, core
, items_size
);
153 state_hash_insert (res
);
164 state_free (state
*s
)
166 free (s
->transitions
);
167 free (s
->reductions
);
173 /*---------------------------.
174 | Set the transitions of S. |
175 `---------------------------*/
178 state_transitions_set (state
*s
, int num
, state
**trans
)
180 aver (!s
->transitions
);
181 s
->transitions
= transitions_new (num
, trans
);
185 /*--------------------------.
186 | Set the reductions of S. |
187 `--------------------------*/
190 state_reductions_set (state
*s
, int num
, rule
**reds
)
192 aver (!s
->reductions
);
193 s
->reductions
= reductions_new (num
, reds
);
198 state_reduction_find (state
*s
, rule
*r
)
201 reductions
*reds
= s
->reductions
;
202 for (i
= 0; i
< reds
->num
; ++i
)
203 if (reds
->rules
[i
] == r
)
209 /*--------------------.
210 | Set the errs of S. |
211 `--------------------*/
214 state_errs_set (state
*s
, int num
, symbol
**tokens
)
217 s
->errs
= errs_new (num
, tokens
);
222 /*--------------------------------------------------.
223 | Print on OUT all the lookahead tokens such that S |
224 | wants to reduce R. |
225 `--------------------------------------------------*/
228 state_rule_lookahead_tokens_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
->lookahead_tokens
&& red
!= -1)
237 bitset_iterator biter
;
239 char const *sep
= "";
241 BITSET_FOR_EACH (biter
, reds
->lookahead_tokens
[red
], k
, 0)
243 fprintf (out
, "%s%s", sep
, symbols
[k
]->tag
);
251 /*---------------------.
252 | A state hash table. |
253 `---------------------*/
255 /* Initial capacity of states hash table. */
256 #define HT_INITIAL_CAPACITY 257
258 static struct hash_table
*state_table
= NULL
;
260 /* Two states are equal if they have the same core items. */
262 state_compare (state
const *s1
, state
const *s2
)
266 if (s1
->nitems
!= s2
->nitems
)
269 for (i
= 0; i
< s1
->nitems
; ++i
)
270 if (s1
->items
[i
] != s2
->items
[i
])
277 state_comparator (void const *s1
, void const *s2
)
279 return state_compare (s1
, s2
);
283 state_hash (state
const *s
, size_t tablesize
)
285 /* Add up the state's item numbers to get a hash key. */
288 for (i
= 0; i
< s
->nitems
; ++i
)
290 return key
% tablesize
;
294 state_hasher (void const *s
, size_t tablesize
)
296 return state_hash (s
, tablesize
);
300 /*-------------------------------.
301 | Create the states hash table. |
302 `-------------------------------*/
305 state_hash_new (void)
307 state_table
= hash_initialize (HT_INITIAL_CAPACITY
,
315 /*---------------------------------------------.
316 | Free the states hash table, not the states. |
317 `---------------------------------------------*/
320 state_hash_free (void)
322 hash_free (state_table
);
326 /*-----------------------------------.
327 | Insert S in the state hash table. |
328 `-----------------------------------*/
331 state_hash_insert (state
*s
)
333 hash_insert (state_table
, s
);
337 /*------------------------------------------------------------------.
338 | Find the state associated to the CORE, and return it. If it does |
339 | not exist yet, return NULL. |
340 `------------------------------------------------------------------*/
343 state_hash_lookup (size_t nitems
, item_number
*core
)
345 size_t items_size
= nitems
* sizeof *core
;
346 state
*probe
= xmalloc (offsetof (state
, items
) + items_size
);
349 probe
->nitems
= nitems
;
350 memcpy (probe
->items
, core
, items_size
);
351 entry
= hash_lookup (state_table
, probe
);
357 /*------------------------------------------------------.
358 | Mark S and all states reachable from S as reachable. |
359 `------------------------------------------------------*/
362 state_mark_reachable_states (state
*s
)
367 for (int i
= 0; i
< s
->transitions
->num
; ++i
)
368 if (!TRANSITION_IS_DISABLED (s
->transitions
, i
))
369 state_mark_reachable_states (s
->transitions
->states
[i
]);
373 state_remove_unreachable_states (state_number old_to_new
[])
375 state_number nstates_reachable
= 0;
376 state_mark_reachable_states (states
[0]);
377 for (state_number i
= 0; i
< nstates
; ++i
)
379 if (states
[i
]->reachable
)
381 states
[nstates_reachable
] = states
[i
];
382 states
[nstates_reachable
]->number
= nstates_reachable
;
383 old_to_new
[i
] = nstates_reachable
++;
387 state_free (states
[i
]);
388 old_to_new
[i
] = nstates
;
391 nstates
= nstates_reachable
;
394 /* All the decorated states, indexed by the state number. */
395 state
**states
= NULL
;
398 /*----------------------.
399 | Free all the states. |
400 `----------------------*/
406 for (i
= 0; i
< nstates
; ++i
)
407 state_free (states
[i
]);