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