]> git.saurik.com Git - bison.git/blob - src/tables.c
fa2a90c86cf5c344e35b28a7049e6e9291aa13f7
[bison.git] / src / tables.c
1 /* Output the generated parsing program for Bison.
2
3 Copyright (C) 1984, 1986, 1989, 1992, 2000-2006, 2009-2012 Free
4 Software Foundation, Inc.
5
6 This file is part of Bison, the GNU Compiler Compiler.
7
8 This program 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 3 of the License, or
11 (at your option) any later version.
12
13 This program 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.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include <config.h>
22 #include "system.h"
23
24 #include <bitsetv.h>
25
26 #include "complain.h"
27 #include "conflicts.h"
28 #include "files.h"
29 #include "getargs.h"
30 #include "gram.h"
31 #include "lalr.h"
32 #include "muscle-tab.h"
33 #include "reader.h"
34 #include "symtab.h"
35 #include "tables.h"
36
37 /* Several tables are indexed both by state and nonterminal numbers.
38 We call such an index a `vector'; i.e., a vector is either a state
39 or a nonterminal number.
40
41 Of course vector_number_t ought to be wide enough to contain
42 state_number and symbol_number. */
43 typedef int vector_number;
44
45 #if 0 /* Not currently used. */
46 static inline vector_number
47 state_number_to_vector_number (state_number s)
48 {
49 return s;
50 }
51 #endif
52
53 static inline vector_number
54 symbol_number_to_vector_number (symbol_number sym)
55 {
56 return state_number_as_int (nstates) + sym - ntokens;
57 }
58
59 int nvectors;
60
61
62 /* FROMS and TOS are indexed by vector_number.
63
64 If VECTOR is a nonterminal, (FROMS[VECTOR], TOS[VECTOR]) form an
65 array of state numbers of the non defaulted GOTO on VECTOR.
66
67 If VECTOR is a state, TOS[VECTOR] is the array of actions to do on
68 the (array of) symbols FROMS[VECTOR].
69
70 In both cases, TALLY[VECTOR] is the size of the arrays
71 FROMS[VECTOR], TOS[VECTOR]; and WIDTH[VECTOR] =
72 (FROMS[VECTOR][SIZE] - FROMS[VECTOR][0] + 1) where SIZE =
73 TALLY[VECTOR].
74
75 FROMS therefore contains symbol_number and action_number,
76 TOS state_number and action_number,
77 TALLY sizes,
78 WIDTH differences of FROMS.
79
80 Let base_number be the type of FROMS, TOS, and WIDTH. */
81 #define BASE_MAXIMUM INT_MAX
82 #define BASE_MINIMUM INT_MIN
83
84 static base_number **froms;
85 static base_number **tos;
86 static unsigned int **conflict_tos;
87 static size_t *tally;
88 static base_number *width;
89
90
91 /* For a given state, N = ACTROW[SYMBOL]:
92
93 If N = 0, stands for `run the default action'.
94 If N = MIN, stands for `raise a syntax error'.
95 If N > 0, stands for `shift SYMBOL and go to n'.
96 If N < 0, stands for `reduce -N'. */
97 typedef int action_number;
98 #define ACTION_NUMBER_MINIMUM INT_MIN
99
100 static action_number *actrow;
101
102 /* FROMS and TOS are reordered to be compressed. ORDER[VECTOR] is the
103 new vector number of VECTOR. We skip `empty' vectors (i.e.,
104 TALLY[VECTOR] = 0), and call these `entries'. */
105 static vector_number *order;
106 static int nentries;
107
108 base_number *base = NULL;
109 /* A distinguished value of BASE, negative infinite. During the
110 computation equals to BASE_MINIMUM, later mapped to BASE_NINF to
111 keep parser tables small. */
112 base_number base_ninf = 0;
113 static base_number *pos = NULL;
114
115 static unsigned int *conflrow;
116 unsigned int *conflict_table;
117 unsigned int *conflict_list;
118 int conflict_list_cnt;
119 static int conflict_list_free;
120
121 /* TABLE_SIZE is the allocated size of both TABLE and CHECK. We start
122 with more or less the original hard-coded value (which was
123 SHRT_MAX). */
124 static int table_size = 32768;
125 base_number *table;
126 base_number *check;
127 /* The value used in TABLE to denote explicit syntax errors
128 (%nonassoc), a negative infinite. First defaults to ACTION_NUMBER_MINIMUM,
129 but in order to keep small tables, renumbered as TABLE_ERROR, which
130 is the smallest (non error) value minus 1. */
131 base_number table_ninf = 0;
132 static int lowzero;
133 int high;
134
135 state_number *yydefgoto;
136 rule_number *yydefact;
137
138 /*-------------------------------------------------------------------.
139 | If TABLE, CONFLICT_TABLE, and CHECK are too small to be addressed |
140 | at DESIRED, grow them. TABLE[DESIRED] can be used, so the desired |
141 | size is at least DESIRED + 1. |
142 `-------------------------------------------------------------------*/
143
144 static void
145 table_grow (int desired)
146 {
147 int old_size = table_size;
148
149 while (table_size <= desired)
150 table_size *= 2;
151
152 if (trace_flag & trace_resource)
153 fprintf (stderr, "growing table and check from: %d to %d\n",
154 old_size, table_size);
155
156 table = xnrealloc (table, table_size, sizeof *table);
157 conflict_table = xnrealloc (conflict_table, table_size,
158 sizeof *conflict_table);
159 check = xnrealloc (check, table_size, sizeof *check);
160
161 for (/* Nothing. */; old_size < table_size; ++old_size)
162 {
163 table[old_size] = 0;
164 conflict_table[old_size] = 0;
165 check[old_size] = -1;
166 }
167 }
168
169
170
171
172 /*-------------------------------------------------------------------.
173 | For GLR parsers, for each conflicted token in S, as indicated |
174 | by non-zero entries in CONFLROW, create a list of possible |
175 | reductions that are alternatives to the shift or reduction |
176 | currently recorded for that token in S. Store the alternative |
177 | reductions followed by a 0 in CONFLICT_LIST, updating |
178 | CONFLICT_LIST_CNT, and storing an index to the start of the list |
179 | back into CONFLROW. |
180 `-------------------------------------------------------------------*/
181
182 static void
183 conflict_row (state *s)
184 {
185 int i, j;
186 reductions *reds = s->reductions;
187
188 if (!nondeterministic_parser)
189 return;
190
191 for (j = 0; j < ntokens; j += 1)
192 if (conflrow[j])
193 {
194 conflrow[j] = conflict_list_cnt;
195
196 /* Find all reductions for token J, and record all that do not
197 match ACTROW[J]. */
198 for (i = 0; i < reds->num; i += 1)
199 if (bitset_test (reds->lookahead_tokens[i], j)
200 && (actrow[j]
201 != rule_number_as_item_number (reds->rules[i]->number)))
202 {
203 aver (0 < conflict_list_free);
204 conflict_list[conflict_list_cnt] = reds->rules[i]->number + 1;
205 conflict_list_cnt += 1;
206 conflict_list_free -= 1;
207 }
208
209 /* Leave a 0 at the end. */
210 aver (0 < conflict_list_free);
211 conflict_list[conflict_list_cnt] = 0;
212 conflict_list_cnt += 1;
213 conflict_list_free -= 1;
214 }
215 }
216
217
218 /*------------------------------------------------------------------.
219 | Decide what to do for each type of token if seen as the |
220 | lookahead in specified state. The value returned is used as the |
221 | default action (yydefact) for the state. In addition, ACTROW is |
222 | filled with what to do for each kind of token, index by symbol |
223 | number, with zero meaning do the default action. The value |
224 | ACTION_NUMBER_MINIMUM, a very negative number, means this |
225 | situation is an error. The parser recognizes this value |
226 | specially. |
227 | |
228 | This is where conflicts are resolved. The loop over lookahead |
229 | rules considered lower-numbered rules last, and the last rule |
230 | considered that likes a token gets to handle it. |
231 | |
232 | For GLR parsers, also sets CONFLROW[SYM] to an index into |
233 | CONFLICT_LIST iff there is an unresolved conflict (s/r or r/r) |
234 | with symbol SYM. The default reduction is not used for a symbol |
235 | that has any such conflicts. |
236 `------------------------------------------------------------------*/
237
238 static rule *
239 action_row (state *s)
240 {
241 int i;
242 rule *default_reduction = NULL;
243 reductions *reds = s->reductions;
244 transitions *trans = s->transitions;
245 errs *errp = s->errs;
246 /* Set to nonzero to inhibit having any default reduction. */
247 bool nodefault = false;
248 bool conflicted = false;
249
250 for (i = 0; i < ntokens; i++)
251 actrow[i] = conflrow[i] = 0;
252
253 if (reds->lookahead_tokens)
254 {
255 int j;
256 bitset_iterator biter;
257 /* loop over all the rules available here which require
258 lookahead (in reverse order to give precedence to the first
259 rule) */
260 for (i = reds->num - 1; i >= 0; --i)
261 /* and find each token which the rule finds acceptable
262 to come next */
263 BITSET_FOR_EACH (biter, reds->lookahead_tokens[i], j, 0)
264 {
265 /* and record this rule as the rule to use if that
266 token follows. */
267 if (actrow[j] != 0)
268 {
269 conflicted = true;
270 conflrow[j] = 1;
271 }
272 actrow[j] = rule_number_as_item_number (reds->rules[i]->number);
273 }
274 }
275
276 /* Now see which tokens are allowed for shifts in this state. For
277 them, record the shift as the thing to do. So shift is preferred
278 to reduce. */
279 FOR_EACH_SHIFT (trans, i)
280 {
281 symbol_number sym = TRANSITION_SYMBOL (trans, i);
282 state *shift_state = trans->states[i];
283
284 if (actrow[sym] != 0)
285 {
286 conflicted = true;
287 conflrow[sym] = 1;
288 }
289 actrow[sym] = state_number_as_int (shift_state->number);
290
291 /* Do not use any default reduction if there is a shift for
292 error */
293 if (sym == errtoken->number)
294 nodefault = true;
295 }
296
297 /* See which tokens are an explicit error in this state (due to
298 %nonassoc). For them, record ACTION_NUMBER_MINIMUM as the
299 action. */
300 for (i = 0; i < errp->num; i++)
301 {
302 symbol *sym = errp->symbols[i];
303 actrow[sym->number] = ACTION_NUMBER_MINIMUM;
304 }
305
306 /* Turn off default reductions where requested by the user. See
307 state_lookahead_tokens_count in lalr.c to understand when states are
308 labeled as consistent. */
309 {
310 char *default_reductions =
311 muscle_percent_define_get ("lr.default-reduction");
312 if (STRNEQ (default_reductions, "most") && !s->consistent)
313 nodefault = true;
314 free (default_reductions);
315 }
316
317 /* Now find the most common reduction and make it the default action
318 for this state. */
319
320 if (reds->num >= 1 && !nodefault)
321 {
322 if (s->consistent)
323 default_reduction = reds->rules[0];
324 else
325 {
326 int max = 0;
327 for (i = 0; i < reds->num; i++)
328 {
329 int count = 0;
330 rule *r = reds->rules[i];
331 symbol_number j;
332
333 for (j = 0; j < ntokens; j++)
334 if (actrow[j] == rule_number_as_item_number (r->number))
335 count++;
336
337 if (count > max)
338 {
339 max = count;
340 default_reduction = r;
341 }
342 }
343
344 /* GLR parsers need space for conflict lists, so we can't
345 default conflicted entries. For non-conflicted entries
346 or as long as we are not building a GLR parser,
347 actions that match the default are replaced with zero,
348 which means "use the default". */
349
350 if (max > 0)
351 {
352 int j;
353 for (j = 0; j < ntokens; j++)
354 if (actrow[j]
355 == rule_number_as_item_number (default_reduction->number)
356 && ! (nondeterministic_parser && conflrow[j]))
357 actrow[j] = 0;
358 }
359 }
360 }
361
362 /* If have no default reduction, the default is an error.
363 So replace any action which says "error" with "use default". */
364
365 if (!default_reduction)
366 for (i = 0; i < ntokens; i++)
367 if (actrow[i] == ACTION_NUMBER_MINIMUM)
368 actrow[i] = 0;
369
370 if (conflicted)
371 conflict_row (s);
372
373 return default_reduction;
374 }
375
376
377 /*----------------------------------------.
378 | Set FROMS, TOS, TALLY and WIDTH for S. |
379 `----------------------------------------*/
380
381 static void
382 save_row (state_number s)
383 {
384 symbol_number i;
385 size_t count;
386 base_number *sp;
387 base_number *sp1;
388 base_number *sp2;
389 unsigned int *sp3;
390
391 /* Number of non default actions in S. */
392 count = 0;
393 for (i = 0; i < ntokens; i++)
394 if (actrow[i] != 0)
395 count++;
396
397 if (count == 0)
398 return;
399
400 /* Allocate non defaulted actions. */
401 froms[s] = sp = sp1 = xnmalloc (count, sizeof *sp1);
402 tos[s] = sp2 = xnmalloc (count, sizeof *sp2);
403 conflict_tos[s] = sp3 =
404 nondeterministic_parser ? xnmalloc (count, sizeof *sp3) : NULL;
405
406 /* Store non defaulted actions. */
407 for (i = 0; i < ntokens; i++)
408 if (actrow[i] != 0)
409 {
410 *sp1++ = i;
411 *sp2++ = actrow[i];
412 if (nondeterministic_parser)
413 *sp3++ = conflrow[i];
414 }
415
416 tally[s] = count;
417 width[s] = sp1[-1] - sp[0] + 1;
418 }
419
420
421 /*------------------------------------------------------------------.
422 | Figure out the actions for the specified state, indexed by |
423 | lookahead token type. |
424 | |
425 | The YYDEFACT table is output now. The detailed info is saved for |
426 | putting into YYTABLE later. |
427 `------------------------------------------------------------------*/
428
429 static void
430 token_actions (void)
431 {
432 state_number i;
433 symbol_number j;
434 rule_number r;
435
436 int nconflict = nondeterministic_parser ? conflicts_total_count () : 0;
437
438 yydefact = xnmalloc (nstates, sizeof *yydefact);
439
440 actrow = xnmalloc (ntokens, sizeof *actrow);
441 conflrow = xnmalloc (ntokens, sizeof *conflrow);
442
443 conflict_list = xnmalloc (1 + 2 * nconflict, sizeof *conflict_list);
444 conflict_list_free = 2 * nconflict;
445 conflict_list_cnt = 1;
446
447 /* Find the rules which are reduced. */
448 if (!nondeterministic_parser)
449 for (r = 0; r < nrules; ++r)
450 rules[r].useful = false;
451
452 for (i = 0; i < nstates; ++i)
453 {
454 rule *default_reduction = action_row (states[i]);
455 yydefact[i] = default_reduction ? default_reduction->number + 1 : 0;
456 save_row (i);
457
458 /* Now that the parser was computed, we can find which rules are
459 really reduced, and which are not because of SR or RR
460 conflicts. */
461 if (!nondeterministic_parser)
462 {
463 for (j = 0; j < ntokens; ++j)
464 if (actrow[j] < 0 && actrow[j] != ACTION_NUMBER_MINIMUM)
465 rules[item_number_as_rule_number (actrow[j])].useful = true;
466 if (yydefact[i])
467 rules[yydefact[i] - 1].useful = true;
468 }
469 }
470
471 free (actrow);
472 free (conflrow);
473 }
474
475
476 /*------------------------------------------------------------------.
477 | Compute FROMS[VECTOR], TOS[VECTOR], TALLY[VECTOR], WIDTH[VECTOR], |
478 | i.e., the information related to non defaulted GOTO on the nterm |
479 | SYM. |
480 | |
481 | DEFAULT_STATE is the principal destination on SYM, i.e., the |
482 | default GOTO destination on SYM. |
483 `------------------------------------------------------------------*/
484
485 static void
486 save_column (symbol_number sym, state_number default_state)
487 {
488 goto_number i;
489 goto_number begin = goto_map[sym - ntokens];
490 goto_number end = goto_map[sym - ntokens + 1];
491
492 /* Number of non default GOTO. */
493 size_t count = 0;
494 for (i = begin; i < end; i++)
495 if (to_state[i] != default_state)
496 count++;
497
498 if (count)
499 {
500 /* Allocate room for non defaulted gotos. */
501 vector_number symno = symbol_number_to_vector_number (sym);
502 base_number *sp1 = froms[symno] = xnmalloc (count, sizeof *sp1);
503 base_number *sp2 = tos[symno] = xnmalloc (count, sizeof *sp2);
504
505 /* Store the state numbers of the non defaulted gotos. */
506 for (i = begin; i < end; i++)
507 if (to_state[i] != default_state)
508 {
509 *sp1++ = from_state[i];
510 *sp2++ = to_state[i];
511 }
512
513 tally[symno] = count;
514 width[symno] = sp1[-1] - froms[symno][0] + 1;
515 }
516 }
517
518
519 /*-------------------------------------------------------------.
520 | Return `the' most common destination GOTO on SYM (a nterm). |
521 `-------------------------------------------------------------*/
522
523 static state_number
524 default_goto (symbol_number sym, size_t state_count[])
525 {
526 state_number s;
527 goto_number i;
528 goto_number m = goto_map[sym - ntokens];
529 goto_number n = goto_map[sym - ntokens + 1];
530 state_number default_state = -1;
531 size_t max = 0;
532
533 if (m == n)
534 return -1;
535
536 for (s = 0; s < nstates; s++)
537 state_count[s] = 0;
538
539 for (i = m; i < n; i++)
540 state_count[to_state[i]]++;
541
542 for (s = 0; s < nstates; s++)
543 if (state_count[s] > max)
544 {
545 max = state_count[s];
546 default_state = s;
547 }
548
549 return default_state;
550 }
551
552
553 /*-------------------------------------------------------------------.
554 | Figure out what to do after reducing with each rule, depending on |
555 | the saved state from before the beginning of parsing the data that |
556 | matched this rule. |
557 | |
558 | The YYDEFGOTO table is output now. The detailed info is saved for |
559 | putting into YYTABLE later. |
560 `-------------------------------------------------------------------*/
561
562 static void
563 goto_actions (void)
564 {
565 symbol_number i;
566 size_t *state_count = xnmalloc (nstates, sizeof *state_count);
567 yydefgoto = xnmalloc (nvars, sizeof *yydefgoto);
568
569 /* For a given nterm I, STATE_COUNT[S] is the number of times there
570 is a GOTO to S on I. */
571 for (i = ntokens; i < nsyms; ++i)
572 {
573 state_number default_state = default_goto (i, state_count);
574 save_column (i, default_state);
575 yydefgoto[i - ntokens] = default_state;
576 }
577 free (state_count);
578 }
579
580
581 /*------------------------------------------------------------------.
582 | Compute ORDER, a reordering of vectors, in order to decide how to |
583 | pack the actions and gotos information into yytable. |
584 `------------------------------------------------------------------*/
585
586 static void
587 sort_actions (void)
588 {
589 int i;
590
591 nentries = 0;
592
593 for (i = 0; i < nvectors; i++)
594 if (0 < tally[i])
595 {
596 int k;
597 size_t t = tally[i];
598 int w = width[i];
599 int j = nentries - 1;
600
601 while (0 <= j && width[order[j]] < w)
602 j--;
603
604 while (0 <= j && width[order[j]] == w && tally[order[j]] < t)
605 j--;
606
607 for (k = nentries - 1; k > j; k--)
608 order[k + 1] = order[k];
609
610 order[j + 1] = i;
611 nentries++;
612 }
613 }
614
615
616 /* If VECTOR is a state whose actions (reflected by FROMS, TOS, TALLY
617 and WIDTH of VECTOR) are common to a previous state, return this
618 state number.
619
620 In any other case, return -1. */
621
622 static state_number
623 matching_state (vector_number vector)
624 {
625 vector_number i = order[vector];
626 size_t t;
627 int w;
628 int prev;
629
630 /* If VECTOR is a nterm, return -1. */
631 if (nstates <= i)
632 return -1;
633
634 t = tally[i];
635 w = width[i];
636
637 /* If VECTOR has GLR conflicts, return -1 */
638 if (conflict_tos[i] != NULL)
639 {
640 int j;
641 for (j = 0; j < t; j += 1)
642 if (conflict_tos[i][j] != 0)
643 return -1;
644 }
645
646 for (prev = vector - 1; prev >= 0; prev--)
647 {
648 vector_number j = order[prev];
649 int k;
650 int match = 1;
651
652 /* Given how ORDER was computed, if the WIDTH or TALLY is
653 different, there cannot be a matching state. */
654 if (width[j] != w || tally[j] != t)
655 return -1;
656
657 for (k = 0; match && k < t; k++)
658 if (tos[j][k] != tos[i][k]
659 || froms[j][k] != froms[i][k]
660 || (conflict_tos[j] != NULL && conflict_tos[j][k] != 0))
661 match = 0;
662
663 if (match)
664 return j;
665 }
666
667 return -1;
668 }
669
670
671 static base_number
672 pack_vector (vector_number vector)
673 {
674 base_number res;
675 vector_number i = order[vector];
676 size_t t = tally[i];
677 base_number *from = froms[i];
678 base_number *to = tos[i];
679 unsigned int *conflict_to = conflict_tos[i];
680
681 aver (t != 0);
682
683 for (res = lowzero - from[0]; ; res++)
684 {
685 bool ok = true;
686 aver (res < table_size);
687 {
688 int k;
689 for (k = 0; ok && k < t; k++)
690 {
691 int loc = res + state_number_as_int (from[k]);
692 if (table_size <= loc)
693 table_grow (loc);
694
695 if (table[loc] != 0)
696 ok = false;
697 }
698
699 if (ok)
700 for (k = 0; k < vector; k++)
701 if (pos[k] == res)
702 ok = false;
703 }
704
705 if (ok)
706 {
707 int loc;
708 int k;
709 for (k = 0; k < t; k++)
710 {
711 loc = res + state_number_as_int (from[k]);
712 table[loc] = to[k];
713 if (nondeterministic_parser && conflict_to != NULL)
714 conflict_table[loc] = conflict_to[k];
715 check[loc] = from[k];
716 }
717
718 while (table[lowzero] != 0)
719 lowzero++;
720
721 if (high < loc)
722 high = loc;
723
724 aver (BASE_MINIMUM <= res && res <= BASE_MAXIMUM);
725 return res;
726 }
727 }
728 }
729
730
731 /*-------------------------------------------------------------.
732 | Remap the negative infinite in TAB from NINF to the greatest |
733 | possible smallest value. Return it. |
734 | |
735 | In most case this allows us to use shorts instead of ints in |
736 | parsers. |
737 `-------------------------------------------------------------*/
738
739 static base_number
740 table_ninf_remap (base_number tab[], int size, base_number ninf)
741 {
742 base_number res = 0;
743 int i;
744
745 for (i = 0; i < size; i++)
746 if (tab[i] < res && tab[i] != ninf)
747 res = tab[i];
748
749 --res;
750
751 for (i = 0; i < size; i++)
752 if (tab[i] == ninf)
753 tab[i] = res;
754
755 return res;
756 }
757
758 static void
759 pack_table (void)
760 {
761 int i;
762
763 base = xnmalloc (nvectors, sizeof *base);
764 pos = xnmalloc (nentries, sizeof *pos);
765 table = xcalloc (table_size, sizeof *table);
766 conflict_table = xcalloc (table_size, sizeof *conflict_table);
767 check = xnmalloc (table_size, sizeof *check);
768
769 lowzero = 0;
770 high = 0;
771
772 for (i = 0; i < nvectors; i++)
773 base[i] = BASE_MINIMUM;
774
775 for (i = 0; i < table_size; i++)
776 check[i] = -1;
777
778 for (i = 0; i < nentries; i++)
779 {
780 state_number s = matching_state (i);
781 base_number place;
782
783 if (s < 0)
784 /* A new set of state actions, or a nonterminal. */
785 place = pack_vector (i);
786 else
787 /* Action of I were already coded for S. */
788 place = base[s];
789
790 pos[i] = place;
791 base[order[i]] = place;
792 }
793
794 /* Use the greatest possible negative infinites. */
795 base_ninf = table_ninf_remap (base, nvectors, BASE_MINIMUM);
796 table_ninf = table_ninf_remap (table, high + 1, ACTION_NUMBER_MINIMUM);
797
798 free (pos);
799 }
800
801 \f
802
803 /*-----------------------------------------------------------------.
804 | Compute and output yydefact, yydefgoto, yypact, yypgoto, yytable |
805 | and yycheck. |
806 `-----------------------------------------------------------------*/
807
808 void
809 tables_generate (void)
810 {
811 int i;
812
813 /* This is a poor way to make sure the sizes are properly
814 correlated. In particular the signedness is not taken into
815 account. But it's not useless. */
816 verify (sizeof nstates <= sizeof nvectors
817 && sizeof nvars <= sizeof nvectors);
818
819 nvectors = state_number_as_int (nstates) + nvars;
820
821 froms = xcalloc (nvectors, sizeof *froms);
822 tos = xcalloc (nvectors, sizeof *tos);
823 conflict_tos = xcalloc (nvectors, sizeof *conflict_tos);
824 tally = xcalloc (nvectors, sizeof *tally);
825 width = xnmalloc (nvectors, sizeof *width);
826
827 token_actions ();
828
829 goto_actions ();
830 free (goto_map);
831 free (from_state);
832 free (to_state);
833
834 order = xcalloc (nvectors, sizeof *order);
835 sort_actions ();
836 pack_table ();
837 free (order);
838
839 free (tally);
840 free (width);
841
842 for (i = 0; i < nvectors; i++)
843 {
844 free (froms[i]);
845 free (tos[i]);
846 free (conflict_tos[i]);
847 }
848
849 free (froms);
850 free (tos);
851 free (conflict_tos);
852 }
853
854
855 /*-------------------------.
856 | Free the parser tables. |
857 `-------------------------*/
858
859 void
860 tables_free (void)
861 {
862 free (base);
863 free (conflict_table);
864 free (conflict_list);
865 free (table);
866 free (check);
867 free (yydefgoto);
868 free (yydefact);
869 }