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