1 /* Type definitions for nondeterministic finite state machine for Bison.
3 Copyright (C) 2001-2007, 2009-2010 Free Software Foundation, Inc.
5 This file is part of Bison, the GNU Compiler Compiler.
7 This program 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 3 of the License, or
10 (at your option) any later version.
12 This program 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 this program. If not, see <http://www.gnu.org/licenses/>. */
28 #include "print-xml.h"
31 /*-------------------.
33 `-------------------*/
36 /*-----------------------------------------.
37 | Create a new array of NUM shifts/gotos. |
38 `-----------------------------------------*/
41 transitions_new (int num
, state
**the_states
)
43 size_t states_size
= num
* sizeof *the_states
;
44 transitions
*res
= xmalloc (offsetof (transitions
, states
) + states_size
);
46 memcpy (res
->states
, the_states
, states_size
);
51 /*-------------------------------------------------------.
52 | Return the state such that SHIFTS contain a shift/goto |
53 | to it on SYM. Abort if none found. |
54 `-------------------------------------------------------*/
57 transitions_to (transitions
*shifts
, symbol_number sym
)
62 aver (j
< shifts
->num
);
63 if (TRANSITION_SYMBOL (shifts
, j
) == sym
)
64 return shifts
->states
[j
];
69 /*--------------------.
70 | Error transitions. |
71 `--------------------*/
74 /*---------------------------------.
75 | Create a new array of NUM errs. |
76 `---------------------------------*/
79 errs_new (int num
, symbol
**tokens
)
81 size_t symbols_size
= num
* sizeof *tokens
;
82 errs
*res
= xmalloc (offsetof (errs
, symbols
) + symbols_size
);
84 memcpy (res
->symbols
, tokens
, symbols_size
);
96 /*---------------------------------------.
97 | Create a new array of NUM reductions. |
98 `---------------------------------------*/
101 reductions_new (int num
, rule
**reds
)
103 size_t rules_size
= num
* sizeof *reds
;
104 reductions
*res
= xmalloc (offsetof (reductions
, rules
) + rules_size
);
106 res
->lookahead_tokens
= NULL
;
107 memcpy (res
->rules
, reds
, rules_size
);
118 state_number nstates
= 0;
119 /* FINAL_STATE is properly set by new_state when it recognizes its
120 accessing symbol: $end. */
121 state
*final_state
= NULL
;
124 /*------------------------------------------------------------------.
125 | Create a new state with ACCESSING_SYMBOL, for those items. Store |
126 | it in the state hash table. |
127 `------------------------------------------------------------------*/
130 state_new (symbol_number accessing_symbol
,
131 size_t nitems
, item_number
*core
)
134 size_t items_size
= nitems
* sizeof *core
;
136 aver (nstates
< STATE_NUMBER_MAXIMUM
);
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
;
146 res
->solved_conflicts_xml
= NULL
;
148 res
->nitems
= nitems
;
149 memcpy (res
->items
, core
, items_size
);
151 state_hash_insert (res
);
162 state_free (state
*s
)
164 free (s
->transitions
);
165 free (s
->reductions
);
171 /*---------------------------.
172 | Set the transitions of S. |
173 `---------------------------*/
176 state_transitions_set (state
*s
, int num
, state
**trans
)
178 aver (!s
->transitions
);
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
)
190 aver (!s
->reductions
);
191 s
->reductions
= reductions_new (num
, reds
);
196 state_reduction_find (state
*s
, rule
*r
)
199 reductions
*reds
= s
->reductions
;
200 for (i
= 0; i
< reds
->num
; ++i
)
201 if (reds
->rules
[i
] == r
)
207 /*--------------------.
208 | Set the errs of S. |
209 `--------------------*/
212 state_errs_set (state
*s
, int num
, symbol
**tokens
)
215 s
->errs
= errs_new (num
, tokens
);
220 /*--------------------------------------------------.
221 | Print on OUT all the lookahead tokens such that S |
222 | wants to reduce R. |
223 `--------------------------------------------------*/
226 state_rule_lookahead_tokens_print (state
*s
, rule
*r
, FILE *out
)
228 /* Find the reduction we are handling. */
229 reductions
*reds
= s
->reductions
;
230 int red
= state_reduction_find (s
, r
);
232 /* Print them if there are. */
233 if (reds
->lookahead_tokens
&& red
!= -1)
235 bitset_iterator biter
;
237 char const *sep
= "";
239 BITSET_FOR_EACH (biter
, reds
->lookahead_tokens
[red
], k
, 0)
241 fprintf (out
, "%s%s", sep
, symbols
[k
]->tag
);
249 state_rule_lookahead_tokens_print_xml (state
*s
, rule
*r
,
250 FILE *out
, int level
)
252 /* Find the reduction we are handling. */
253 reductions
*reds
= s
->reductions
;
254 int red
= state_reduction_find (s
, r
);
256 /* Print them if there are. */
257 if (reds
->lookahead_tokens
&& red
!= -1)
259 bitset_iterator biter
;
261 xml_puts (out
, level
, "<lookaheads>");
262 BITSET_FOR_EACH (biter
, reds
->lookahead_tokens
[red
], k
, 0)
264 xml_printf (out
, level
+ 1, "<symbol>%s</symbol>",
265 xml_escape (symbols
[k
]->tag
));
267 xml_puts (out
, level
, "</lookaheads>");
272 /*---------------------.
273 | A state hash table. |
274 `---------------------*/
276 /* Initial capacity of states hash table. */
277 #define HT_INITIAL_CAPACITY 257
279 static struct hash_table
*state_table
= NULL
;
281 /* Two states are equal if they have the same core items. */
283 state_compare (state
const *s1
, state
const *s2
)
287 if (s1
->nitems
!= s2
->nitems
)
290 for (i
= 0; i
< s1
->nitems
; ++i
)
291 if (s1
->items
[i
] != s2
->items
[i
])
298 state_comparator (void const *s1
, void const *s2
)
300 return state_compare (s1
, s2
);
304 state_hash (state
const *s
, size_t tablesize
)
306 /* Add up the state's item numbers to get a hash key. */
309 for (i
= 0; i
< s
->nitems
; ++i
)
311 return key
% tablesize
;
315 state_hasher (void const *s
, size_t tablesize
)
317 return state_hash (s
, tablesize
);
321 /*-------------------------------.
322 | Create the states hash table. |
323 `-------------------------------*/
326 state_hash_new (void)
328 state_table
= hash_initialize (HT_INITIAL_CAPACITY
,
336 /*---------------------------------------------.
337 | Free the states hash table, not the states. |
338 `---------------------------------------------*/
341 state_hash_free (void)
343 hash_free (state_table
);
347 /*-----------------------------------.
348 | Insert S in the state hash table. |
349 `-----------------------------------*/
352 state_hash_insert (state
*s
)
354 if (!hash_insert (state_table
, s
))
359 /*------------------------------------------------------------------.
360 | Find the state associated to the CORE, and return it. If it does |
361 | not exist yet, return NULL. |
362 `------------------------------------------------------------------*/
365 state_hash_lookup (size_t nitems
, item_number
*core
)
367 size_t items_size
= nitems
* sizeof *core
;
368 state
*probe
= xmalloc (offsetof (state
, items
) + items_size
);
371 probe
->nitems
= nitems
;
372 memcpy (probe
->items
, core
, items_size
);
373 entry
= hash_lookup (state_table
, probe
);
379 /*--------------------------------------------------------.
380 | Record S and all states reachable from S in REACHABLE. |
381 `--------------------------------------------------------*/
384 state_record_reachable_states (state
*s
, bitset reachable
)
386 if (bitset_test (reachable
, s
->number
))
388 bitset_set (reachable
, s
->number
);
391 for (i
= 0; i
< s
->transitions
->num
; ++i
)
392 if (!TRANSITION_IS_DISABLED (s
->transitions
, i
))
393 state_record_reachable_states (s
->transitions
->states
[i
], reachable
);
398 state_remove_unreachable_states (state_number old_to_new
[])
400 state_number nstates_reachable
= 0;
401 bitset reachable
= bitset_create (nstates
, BITSET_FIXED
);
402 state_record_reachable_states (states
[0], reachable
);
405 for (i
= 0; i
< nstates
; ++i
)
407 if (bitset_test (reachable
, states
[i
]->number
))
409 states
[nstates_reachable
] = states
[i
];
410 states
[nstates_reachable
]->number
= nstates_reachable
;
411 old_to_new
[i
] = nstates_reachable
++;
415 state_free (states
[i
]);
416 old_to_new
[i
] = nstates
;
420 nstates
= nstates_reachable
;
421 bitset_free (reachable
);
424 /* All the decorated states, indexed by the state number. */
425 state
**states
= NULL
;
428 /*----------------------.
429 | Free all the states. |
430 `----------------------*/
436 for (i
= 0; i
< nstates
; ++i
)
437 state_free (states
[i
]);