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(Num)                                          \ 
  39   (transitions_t *) xcalloc ((sizeof (transitions_t)                    \ 
  40                                   + (Num - 1) * sizeof (state_t *)), 1) 
  42 static transitions_t 
* 
  43 transitions_new (int num
, state_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 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_t *)), 1) 
  83 errs_new (int num
, symbol_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_t *)), 1) 
 107 static reductions_t 
* 
 108 reductions_new (int num
, rule_t 
**reductions
) 
 110   reductions_t 
*res 
= REDUCTIONS_ALLOC (num
); 
 112   memcpy (res
->rules
, reductions
, num 
* sizeof (reductions
[0])); 
 113   res
->lookaheads 
= NULL
; 
 124 state_number_t nstates 
= 0; 
 125 /* FINAL_STATE is properly set by new_state when it recognizes its 
 126    accessing symbol: $end.  */ 
 127 state_t 
*final_state 
= NULL
; 
 129 #define STATE_ALLOC(Nitems)                                             \ 
 130   (state_t *) xcalloc ((sizeof (state_t)                                \ 
 131                         + (Nitems - 1) * sizeof (item_number_t)), 1) 
 133 /*------------------------------------------------------------------. 
 134 | Create a new state with ACCESSING_SYMBOL, for those items.  Store | 
 135 | it in the state hash table.                                       | 
 136 `------------------------------------------------------------------*/ 
 139 state_new (symbol_number_t accessing_symbol
, 
 140            size_t core_size
, item_number_t 
*core
) 
 144   if (nstates 
>= STATE_NUMBER_MAX
) 
 145     fatal (_("too many states (max %d)"), STATE_NUMBER_MAX
); 
 147   res 
= STATE_ALLOC (core_size
); 
 148   res
->accessing_symbol 
= accessing_symbol
; 
 149   res
->number 
= nstates
; 
 151   res
->solved_conflicts 
= NULL
; 
 153   res
->nitems 
= core_size
; 
 154   memcpy (res
->items
, core
, core_size 
* sizeof (core
[0])); 
 156   state_hash_insert (res
); 
 167 state_free (state_t 
*state
) 
 169   free (state
->transitions
); 
 170   free (state
->reductions
); 
 176 /*-------------------------------. 
 177 | Set the transitions of STATE.  | 
 178 `-------------------------------*/ 
 181 state_transitions_set (state_t 
*state
, int num
, state_t 
**transitions
) 
 183   assert (!state
->transitions
); 
 184   state
->transitions 
= transitions_new (num
, transitions
); 
 188 /*------------------------------. 
 189 | Set the reductions of STATE.  | 
 190 `------------------------------*/ 
 193 state_reductions_set (state_t 
*state
, int num
, rule_t 
**reductions
) 
 195   assert (!state
->reductions
); 
 196   state
->reductions 
= reductions_new (num
, reductions
); 
 201 state_reduction_find (state_t 
*state
, rule_t 
*rule
) 
 204   reductions_t 
*reds 
= state
->reductions
; 
 205   for (i 
= 0; i 
< reds
->num
; ++i
) 
 206     if (reds
->rules
[i
] == rule
) 
 212 /*------------------------. 
 213 | Set the errs of STATE.  | 
 214 `------------------------*/ 
 217 state_errs_set (state_t 
*state
, int num
, symbol_t 
**tokens
) 
 219   assert (!state
->errs
); 
 220   state
->errs 
= errs_new (num
, tokens
); 
 225 /*--------------------------------------------------------------. 
 226 | Print on OUT all the lookaheads such that this STATE wants to | 
 227 | reduce this RULE.                                             | 
 228 `--------------------------------------------------------------*/ 
 231 state_rule_lookaheads_print (state_t 
*state
, rule_t 
*rule
, FILE *out
) 
 233   /* Find the reduction we are handling.  */ 
 234   reductions_t 
*reds 
= state
->reductions
; 
 235   int red 
= state_reduction_find (state
, rule
); 
 237   /* Print them if there are.  */ 
 238   if (reds
->lookaheads 
&& red 
!= -1) 
 240       bitset_iterator biter
; 
 244       BITSET_FOR_EACH (biter
, reds
->lookaheads
[red
], k
, 0) 
 245         fprintf (out
, "%s%s", 
 246                  not_first
++ ? ", " : "", 
 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 (const state_t 
*s1
, const state_t 
*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_hash (const state_t 
*state
, unsigned int tablesize
) 
 281   /* Add up the state's item numbers to get a hash key.  */ 
 284   for (i 
= 0; i 
< state
->nitems
; ++i
) 
 285     key 
+= state
->items
[i
]; 
 286   return key 
% tablesize
; 
 290 /*-------------------------------. 
 291 | Create the states hash table.  | 
 292 `-------------------------------*/ 
 295 state_hash_new (void) 
 297   state_table 
= hash_initialize (HT_INITIAL_CAPACITY
, 
 299                                  (Hash_hasher
) state_hash
, 
 300                                  (Hash_comparator
) state_compare
, 
 301                                  (Hash_data_freer
) NULL
); 
 305 /*---------------------------------------------. 
 306 | Free the states hash table, not the states.  | 
 307 `---------------------------------------------*/ 
 310 state_hash_free (void) 
 312   hash_free (state_table
); 
 316 /*---------------------------------------. 
 317 | Insert STATE in the state hash table.  | 
 318 `---------------------------------------*/ 
 321 state_hash_insert (state_t 
*state
) 
 323   hash_insert (state_table
, state
); 
 327 /*------------------------------------------------------------------. 
 328 | Find the state associated to the CORE, and return it.  If it does | 
 329 | not exist yet, return NULL.                                       | 
 330 `------------------------------------------------------------------*/ 
 333 state_hash_lookup (size_t core_size
, item_number_t 
*core
) 
 335   state_t 
*probe 
= STATE_ALLOC (core_size
); 
 338   probe
->nitems 
= core_size
; 
 339   memcpy (probe
->items
, core
, core_size 
* sizeof (core
[0])); 
 340   entry 
= hash_lookup (state_table
, probe
); 
 345 /* All the decorated states, indexed by the state number.  */ 
 346 state_t 
**states 
= NULL
; 
 349 /*----------------------. 
 350 | Free all the states.  | 
 351 `----------------------*/ 
 357   for (i 
= 0; i 
< nstates
; ++i
) 
 358     state_free (states
[i
]);