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