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 SHIFTS_ALLOC(Nshifts) \
39 (shifts *) xcalloc ((unsigned) (sizeof (shifts) \
40 + (Nshifts - 1) * sizeof (short)), 1)
45 shifts
*res
= SHIFTS_ALLOC (n
);
53 /*--------------------.
54 | Error transitions. |
55 `--------------------*/
58 /*-------------------------------.
59 | Create a new array of N errs. |
60 `-------------------------------*/
62 #define ERRS_ALLOC(Nerrs) \
63 (errs *) xcalloc ((unsigned) (sizeof (errs) \
64 + (Nerrs - 1) * sizeof (short)), 1)
70 errs
*res
= ERRS_ALLOC (n
);
79 errs
*res
= errs_new (src
->nerrs
);
80 memcpy (res
->errs
, src
->errs
, src
->nerrs
* sizeof (src
->errs
[0]));
92 /*-------------------------------------.
93 | Create a new array of N reductions. |
94 `-------------------------------------*/
96 #define REDUCTIONS_ALLOC(Nreductions) \
97 (reductions *) xcalloc ((unsigned) (sizeof (reductions) \
98 + (Nreductions - 1) * sizeof (short)), 1)
101 reductions_new (int n
)
103 reductions
*res
= REDUCTIONS_ALLOC (n
);
115 state_number_t nstates
= 0;
116 /* FINAL_STATE is properly set by new_state when it recognizes its
117 accessing symbol: EOF. */
118 state_t
*final_state
= NULL
;
120 #define STATE_ALLOC(Nitems) \
121 (state_t *) xcalloc ((unsigned) (sizeof (state_t) \
122 + (Nitems - 1) * sizeof (item_number_t)), 1)
124 /*------------------------------------------------------------.
125 | Create a new state with ACCESSING_SYMBOL, for those items. |
126 `------------------------------------------------------------*/
129 state_new (symbol_number_t accessing_symbol
,
130 size_t core_size
, item_number_t
*core
)
134 if (nstates
>= STATE_NUMBER_MAX
)
135 fatal (_("too many states (max %d)"), STATE_NUMBER_MAX
);
137 res
= STATE_ALLOC (core_size
);
138 res
->accessing_symbol
= accessing_symbol
;
139 res
->number
= nstates
;
141 res
->solved_conflicts
= NULL
;
143 res
->nitems
= core_size
;
144 memcpy (res
->items
, core
, core_size
* sizeof (core
[0]));
150 /*--------------------------------------------------------------.
151 | Print on OUT all the lookaheads such that this STATE wants to |
152 | reduce this RULE. |
153 `--------------------------------------------------------------*/
156 state_rule_lookaheads_print (state_t
*state
, rule_t
*rule
, FILE *out
)
160 /* Count the number of lookaheads corresponding to this rule. */
161 for (j
= 0; j
< state
->nlookaheads
; ++j
)
162 for (k
= 0; k
< ntokens
; ++k
)
163 if (bitset_test (state
->lookaheads
[j
], k
)
164 && state
->lookaheads_rule
[j
]->number
== rule
->number
)
167 /* Print them if there are. */
171 for (j
= 0; j
< state
->nlookaheads
; ++j
)
172 for (k
= 0; k
< ntokens
; ++k
)
173 if (bitset_test (state
->lookaheads
[j
], k
)
174 && state
->lookaheads_rule
[j
]->number
== rule
->number
)
175 fprintf (out
, "%s%s",
176 symbol_tag_get (symbols
[k
]),
177 --nlookaheads
? ", " : "");
183 /*----------------------.
184 | A state hash table. |
185 `----------------------*/
187 /* Initial capacity of states hash table. */
188 #define HT_INITIAL_CAPACITY 257
190 static struct hash_table
*state_table
= NULL
;
192 /* Two states are equal if they have the same core items. */
194 state_compare (const state_t
*s1
, const state_t
*s2
)
198 if (s1
->nitems
!= s2
->nitems
)
201 for (i
= 0; i
< s1
->nitems
; ++i
)
202 if (s1
->items
[i
] != s2
->items
[i
])
209 state_hash (const state_t
*state
, unsigned int tablesize
)
211 /* Add up the state's item numbers to get a hash key. */
214 for (i
= 0; i
< state
->nitems
; ++i
)
215 key
+= state
->items
[i
];
216 return key
% tablesize
;
220 /*-------------------------------.
221 | Create the states hash table. |
222 `-------------------------------*/
225 state_hash_new (void)
227 state_table
= hash_initialize (HT_INITIAL_CAPACITY
,
229 (Hash_hasher
) state_hash
,
230 (Hash_comparator
) state_compare
,
231 (Hash_data_freer
) NULL
);
235 /*---------------------------------------------.
236 | Free the states hash table, not the states. |
237 `---------------------------------------------*/
240 state_hash_free (void)
242 hash_free (state_table
);
246 /*---------------------------------------.
247 | Insert STATE in the state hash table. |
248 `---------------------------------------*/
251 state_hash_insert (state_t
*state
)
253 hash_insert (state_table
, state
);
257 /*------------------------------------------------------------------.
258 | Find the state associated to the CORE, and return it. If it does |
259 | not exist yet, return NULL. |
260 `------------------------------------------------------------------*/
263 state_hash_lookup (size_t core_size
, item_number_t
*core
)
265 state_t
*probe
= STATE_ALLOC (core_size
);
268 probe
->nitems
= core_size
;
269 memcpy (probe
->items
, core
, core_size
* sizeof (core
[0]));
270 entry
= hash_lookup (state_table
, probe
);
275 /* All the decorated states, indexed by the state number. */
276 state_t
**states
= NULL
;
279 /*----------------------.
280 | Free all the states. |
281 `----------------------*/
288 for (i
= 0; i
< nstates
; ++i
)
290 free (states
[i
]->shifts
);
291 XFREE (states
[i
]->reductions
);
292 free (states
[i
]->errs
);