1 | /* Output the generated parsing program for Bison. |
2 | |
3 | Copyright (C) 1984, 1986, 1989, 1992, 2000-2006, 2009-2015, 2018-2019 |
4 | 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 <bitset.h> |
25 | #include <bitsetv.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 **conflict_tos; |
88 | static size_t *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 | /* Bitset representing an integer set in the range |
115 | -nstates..table_size (as an upper bound) */ |
116 | static bitset pos_set = NULL; |
117 | |
118 | static unsigned *conflrow; |
119 | unsigned *conflict_table; |
120 | unsigned *conflict_list; |
121 | int conflict_list_cnt; |
122 | static int conflict_list_free; |
123 | |
124 | /* TABLE_SIZE is the allocated size of both TABLE and CHECK. We start |
125 | with more or less the original hard-coded value (which was |
126 | SHRT_MAX). */ |
127 | static int table_size = 32768; |
128 | base_number *table; |
129 | base_number *check; |
130 | /* The value used in TABLE to denote explicit syntax errors |
131 | (%nonassoc), a negative infinite. First defaults to ACTION_NUMBER_MINIMUM, |
132 | but in order to keep small tables, renumbered as TABLE_ERROR, which |
133 | is the smallest (non error) value minus 1. */ |
134 | base_number table_ninf = 0; |
135 | static int lowzero; |
136 | int high; |
137 | |
138 | state_number *yydefgoto; |
139 | rule_number *yydefact; |
140 | |
141 | /*-------------------------------------------------------------------. |
142 | | If TABLE, CONFLICT_TABLE, and CHECK are too small to be addressed | |
143 | | at DESIRED, grow them. TABLE[DESIRED] can be used, so the desired | |
144 | | size is at least DESIRED + 1. | |
145 | `-------------------------------------------------------------------*/ |
146 | |
147 | static void |
148 | table_grow (int desired) |
149 | { |
150 | int old_size = table_size; |
151 | |
152 | while (table_size <= desired) |
153 | table_size *= 2; |
154 | |
155 | if (trace_flag & trace_resource) |
156 | fprintf (stderr, "growing tables from %d to %d\n" , |
157 | old_size, table_size); |
158 | |
159 | table = xnrealloc (table, table_size, sizeof *table); |
160 | memset (table + old_size, 0, |
161 | sizeof *table * (table_size - old_size)); |
162 | |
163 | conflict_table = xnrealloc (conflict_table, table_size, |
164 | sizeof *conflict_table); |
165 | memset (conflict_table + old_size, 0, |
166 | sizeof *conflict_table * (table_size - old_size)); |
167 | |
168 | check = xnrealloc (check, table_size, sizeof *check); |
169 | for (int i = old_size; i < table_size; ++i) |
170 | check[i] = -1; |
171 | |
172 | bitset_resize (pos_set, table_size + nstates); |
173 | } |
174 | |
175 | |
176 | |
177 | |
178 | /*-------------------------------------------------------------------. |
179 | | For GLR parsers, for each conflicted token in S, as indicated | |
180 | | by non-zero entries in CONFLROW, create a list of possible | |
181 | | reductions that are alternatives to the shift or reduction | |
182 | | currently recorded for that token in S. Store the alternative | |
183 | | reductions followed by a 0 in CONFLICT_LIST, updating | |
184 | | CONFLICT_LIST_CNT, and storing an index to the start of the list | |
185 | | back into CONFLROW. | |
186 | `-------------------------------------------------------------------*/ |
187 | |
188 | static void |
189 | conflict_row (state *s) |
190 | { |
191 | if (!nondeterministic_parser) |
192 | return; |
193 | |
194 | const reductions *reds = s->reductions; |
195 | for (state_number j = 0; j < ntokens; j += 1) |
196 | if (conflrow[j]) |
197 | { |
198 | conflrow[j] = conflict_list_cnt; |
199 | |
200 | /* Find all reductions for token J, and record all that do not |
201 | match ACTROW[J]. */ |
202 | for (int i = 0; i < reds->num; i += 1) |
203 | if (bitset_test (reds->lookahead_tokens[i], j) |
204 | && (actrow[j] |
205 | != rule_number_as_item_number (reds->rules[i]->number))) |
206 | { |
207 | aver (0 < conflict_list_free); |
208 | conflict_list[conflict_list_cnt] = reds->rules[i]->number + 1; |
209 | conflict_list_cnt += 1; |
210 | conflict_list_free -= 1; |
211 | } |
212 | |
213 | /* Leave a 0 at the end. */ |
214 | aver (0 < conflict_list_free); |
215 | conflict_list[conflict_list_cnt] = 0; |
216 | conflict_list_cnt += 1; |
217 | conflict_list_free -= 1; |
218 | } |
219 | } |
220 | |
221 | |
222 | /*------------------------------------------------------------------. |
223 | | Decide what to do for each type of token if seen as the | |
224 | | lookahead in specified state. The value returned is used as the | |
225 | | default action (yydefact) for the state. In addition, ACTROW is | |
226 | | filled with what to do for each kind of token, index by symbol | |
227 | | number, with zero meaning do the default action. The value | |
228 | | ACTION_NUMBER_MINIMUM, a very negative number, means this | |
229 | | situation is an error. The parser recognizes this value | |
230 | | specially. | |
231 | | | |
232 | | This is where conflicts are resolved. The loop over lookahead | |
233 | | rules considered lower-numbered rules last, and the last rule | |
234 | | considered that likes a token gets to handle it. | |
235 | | | |
236 | | For GLR parsers, also sets CONFLROW[SYM] to an index into | |
237 | | CONFLICT_LIST iff there is an unresolved conflict (s/r or r/r) | |
238 | | with symbol SYM. The default reduction is not used for a symbol | |
239 | | that has any such conflicts. | |
240 | `------------------------------------------------------------------*/ |
241 | |
242 | static rule * |
243 | action_row (state *s) |
244 | { |
245 | for (state_number i = 0; i < ntokens; i++) |
246 | actrow[i] = conflrow[i] = 0; |
247 | |
248 | reductions *reds = s->reductions; |
249 | bool conflicted = false; |
250 | if (reds->lookahead_tokens) |
251 | /* loop over all the rules available here which require |
252 | lookahead (in reverse order to give precedence to the first |
253 | rule) */ |
254 | for (int i = reds->num - 1; 0 <= i; --i) |
255 | /* and find each token which the rule finds acceptable |
256 | to come next */ |
257 | { |
258 | bitset_iterator biter; |
259 | int j; |
260 | BITSET_FOR_EACH (biter, reds->lookahead_tokens[i], j, 0) |
261 | { |
262 | /* and record this rule as the rule to use if that |
263 | token follows. */ |
264 | if (actrow[j] != 0) |
265 | { |
266 | conflicted = true; |
267 | conflrow[j] = 1; |
268 | } |
269 | actrow[j] = rule_number_as_item_number (reds->rules[i]->number); |
270 | } |
271 | } |
272 | |
273 | /* Now see which tokens are allowed for shifts in this state. For |
274 | them, record the shift as the thing to do. So shift is preferred |
275 | to reduce. */ |
276 | transitions *trans = s->transitions; |
277 | /* Set to nonzero to inhibit having any default reduction. */ |
278 | bool nodefault = false; |
279 | { |
280 | int i; |
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->content->number) |
296 | nodefault = true; |
297 | } |
298 | } |
299 | |
300 | /* See which tokens are an explicit error in this state (due to |
301 | %nonassoc). For them, record ACTION_NUMBER_MINIMUM as the |
302 | action. */ |
303 | errs *errp = s->errs; |
304 | for (int i = 0; i < errp->num; i++) |
305 | { |
306 | symbol *sym = errp->symbols[i]; |
307 | actrow[sym->content->number] = ACTION_NUMBER_MINIMUM; |
308 | } |
309 | |
310 | /* Turn off default reductions where requested by the user. See |
311 | state_lookahead_tokens_count in lalr.c to understand when states are |
312 | labeled as consistent. */ |
313 | { |
314 | char *default_reductions = |
315 | muscle_percent_define_get ("lr.default-reduction" ); |
316 | if (STRNEQ (default_reductions, "most" ) && !s->consistent) |
317 | nodefault = true; |
318 | free (default_reductions); |
319 | } |
320 | |
321 | /* Now find the most common reduction and make it the default action |
322 | for this state. */ |
323 | rule *default_reduction = NULL; |
324 | if (reds->num >= 1 && !nodefault) |
325 | { |
326 | if (s->consistent) |
327 | default_reduction = reds->rules[0]; |
328 | else |
329 | { |
330 | int max = 0; |
331 | for (int i = 0; i < reds->num; i++) |
332 | { |
333 | int count = 0; |
334 | rule *r = reds->rules[i]; |
335 | for (symbol_number j = 0; j < ntokens; j++) |
336 | if (actrow[j] == rule_number_as_item_number (r->number)) |
337 | count++; |
338 | |
339 | if (count > max) |
340 | { |
341 | max = count; |
342 | default_reduction = r; |
343 | } |
344 | } |
345 | |
346 | /* GLR parsers need space for conflict lists, so we can't |
347 | default conflicted entries. For non-conflicted entries |
348 | or as long as we are not building a GLR parser, |
349 | actions that match the default are replaced with zero, |
350 | which means "use the default". */ |
351 | |
352 | if (0 < max) |
353 | for (symbol_number 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 | /* If have no default reduction, the default is an error. |
362 | So replace any action which says "error" with "use default". */ |
363 | |
364 | if (!default_reduction) |
365 | for (symbol_number 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_reduction; |
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 | /* Number of non default actions in S. */ |
384 | size_t count = 0; |
385 | for (symbol_number i = 0; i < ntokens; i++) |
386 | if (actrow[i] != 0) |
387 | count++; |
388 | |
389 | if (count) |
390 | { |
391 | /* Allocate non defaulted actions. */ |
392 | base_number *sp1 = froms[s] = xnmalloc (count, sizeof *sp1); |
393 | base_number *sp2 = tos[s] = xnmalloc (count, sizeof *sp2); |
394 | unsigned *sp3 = conflict_tos[s] = |
395 | nondeterministic_parser ? xnmalloc (count, sizeof *sp3) : NULL; |
396 | |
397 | /* Store non defaulted actions. */ |
398 | for (symbol_number i = 0; i < ntokens; i++) |
399 | if (actrow[i] != 0) |
400 | { |
401 | *sp1++ = i; |
402 | *sp2++ = actrow[i]; |
403 | if (nondeterministic_parser) |
404 | *sp3++ = conflrow[i]; |
405 | } |
406 | |
407 | tally[s] = count; |
408 | width[s] = sp1[-1] - froms[s][0] + 1; |
409 | } |
410 | } |
411 | |
412 | |
413 | /*------------------------------------------------------------------. |
414 | | Figure out the actions for the specified state, indexed by | |
415 | | lookahead token type. | |
416 | | | |
417 | | The YYDEFACT table is output now. The detailed info is saved for | |
418 | | putting into YYTABLE later. | |
419 | `------------------------------------------------------------------*/ |
420 | |
421 | static void |
422 | token_actions (void) |
423 | { |
424 | int nconflict = nondeterministic_parser ? conflicts_total_count () : 0; |
425 | |
426 | yydefact = xnmalloc (nstates, sizeof *yydefact); |
427 | |
428 | actrow = xnmalloc (ntokens, sizeof *actrow); |
429 | conflrow = xnmalloc (ntokens, sizeof *conflrow); |
430 | |
431 | conflict_list = xnmalloc (1 + 2 * nconflict, sizeof *conflict_list); |
432 | conflict_list_free = 2 * nconflict; |
433 | conflict_list_cnt = 1; |
434 | |
435 | /* Find the rules which are reduced. */ |
436 | if (!nondeterministic_parser) |
437 | for (rule_number r = 0; r < nrules; ++r) |
438 | rules[r].useful = false; |
439 | |
440 | for (state_number i = 0; i < nstates; ++i) |
441 | { |
442 | rule *default_reduction = action_row (states[i]); |
443 | yydefact[i] = default_reduction ? default_reduction->number + 1 : 0; |
444 | save_row (i); |
445 | |
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. */ |
449 | if (!nondeterministic_parser) |
450 | { |
451 | for (symbol_number j = 0; j < ntokens; ++j) |
452 | if (actrow[j] < 0 && actrow[j] != ACTION_NUMBER_MINIMUM) |
453 | rules[item_number_as_rule_number (actrow[j])].useful = true; |
454 | if (yydefact[i]) |
455 | rules[yydefact[i] - 1].useful = true; |
456 | } |
457 | } |
458 | free (actrow); |
459 | free (conflrow); |
460 | } |
461 | |
462 | |
463 | /*------------------------------------------------------------------. |
464 | | Compute FROMS[VECTOR], TOS[VECTOR], TALLY[VECTOR], WIDTH[VECTOR], | |
465 | | i.e., the information related to non defaulted GOTO on the nterm | |
466 | | SYM. | |
467 | | | |
468 | | DEFAULT_STATE is the principal destination on SYM, i.e., the | |
469 | | default GOTO destination on SYM. | |
470 | `------------------------------------------------------------------*/ |
471 | |
472 | static void |
473 | save_column (symbol_number sym, state_number default_state) |
474 | { |
475 | const goto_number begin = goto_map[sym - ntokens]; |
476 | const goto_number end = goto_map[sym - ntokens + 1]; |
477 | |
478 | /* Number of non default GOTO. */ |
479 | size_t count = 0; |
480 | for (goto_number i = begin; i < end; i++) |
481 | if (to_state[i] != default_state) |
482 | count++; |
483 | |
484 | if (count) |
485 | { |
486 | /* Allocate room for non defaulted gotos. */ |
487 | vector_number symno = symbol_number_to_vector_number (sym); |
488 | base_number *sp1 = froms[symno] = xnmalloc (count, sizeof *sp1); |
489 | base_number *sp2 = tos[symno] = xnmalloc (count, sizeof *sp2); |
490 | |
491 | /* Store the state numbers of the non defaulted gotos. */ |
492 | for (goto_number i = begin; i < end; i++) |
493 | if (to_state[i] != default_state) |
494 | { |
495 | *sp1++ = from_state[i]; |
496 | *sp2++ = to_state[i]; |
497 | } |
498 | |
499 | tally[symno] = count; |
500 | width[symno] = sp1[-1] - froms[symno][0] + 1; |
501 | } |
502 | } |
503 | |
504 | |
505 | /*----------------------------------------------------------------. |
506 | | The default state for SYM: the state which is 'the' most common | |
507 | | GOTO destination on SYM (an nterm). | |
508 | `----------------------------------------------------------------*/ |
509 | |
510 | static state_number |
511 | default_goto (symbol_number sym, size_t state_count[]) |
512 | { |
513 | const goto_number begin = goto_map[sym - ntokens]; |
514 | const goto_number end = goto_map[sym - ntokens + 1]; |
515 | state_number res = -1; |
516 | |
517 | if (begin != end) |
518 | { |
519 | for (state_number s = 0; s < nstates; s++) |
520 | state_count[s] = 0; |
521 | |
522 | for (goto_number i = begin; i < end; i++) |
523 | state_count[to_state[i]]++; |
524 | |
525 | size_t max = 0; |
526 | for (state_number s = 0; s < nstates; s++) |
527 | if (max < state_count[s]) |
528 | { |
529 | max = state_count[s]; |
530 | res = s; |
531 | } |
532 | } |
533 | return res; |
534 | } |
535 | |
536 | |
537 | /*-------------------------------------------------------------------. |
538 | | Figure out what to do after reducing with each rule, depending on | |
539 | | the saved state from before the beginning of parsing the data that | |
540 | | matched this rule. | |
541 | | | |
542 | | The YYDEFGOTO table is output now. The detailed info is saved for | |
543 | | putting into YYTABLE later. | |
544 | `-------------------------------------------------------------------*/ |
545 | |
546 | static void |
547 | goto_actions (void) |
548 | { |
549 | size_t *state_count = xnmalloc (nstates, sizeof *state_count); |
550 | yydefgoto = xnmalloc (nvars, sizeof *yydefgoto); |
551 | |
552 | /* For a given nterm I, STATE_COUNT[S] is the number of times there |
553 | is a GOTO to S on I. */ |
554 | for (symbol_number i = ntokens; i < nsyms; ++i) |
555 | { |
556 | state_number default_state = default_goto (i, state_count); |
557 | save_column (i, default_state); |
558 | yydefgoto[i - ntokens] = default_state; |
559 | } |
560 | free (state_count); |
561 | } |
562 | |
563 | |
564 | /*------------------------------------------------------------------. |
565 | | Compute ORDER, a reordering of vectors, in order to decide how to | |
566 | | pack the actions and gotos information into yytable. | |
567 | `------------------------------------------------------------------*/ |
568 | |
569 | static void |
570 | sort_actions (void) |
571 | { |
572 | nentries = 0; |
573 | |
574 | for (int i = 0; i < nvectors; i++) |
575 | if (0 < tally[i]) |
576 | { |
577 | const size_t t = tally[i]; |
578 | const int w = width[i]; |
579 | int j = nentries - 1; |
580 | |
581 | while (0 <= j && width[order[j]] < w) |
582 | j--; |
583 | |
584 | while (0 <= j && width[order[j]] == w && tally[order[j]] < t) |
585 | j--; |
586 | |
587 | for (int k = nentries - 1; k > j; k--) |
588 | order[k + 1] = order[k]; |
589 | |
590 | order[j + 1] = i; |
591 | nentries++; |
592 | } |
593 | } |
594 | |
595 | |
596 | /* If VECTOR is a state whose actions (reflected by FROMS, TOS, TALLY |
597 | and WIDTH of VECTOR) are common to a previous state, return this |
598 | state number. |
599 | |
600 | In any other case, return -1. */ |
601 | |
602 | static state_number |
603 | matching_state (vector_number vector) |
604 | { |
605 | vector_number i = order[vector]; |
606 | /* If VECTOR is a nterm, return -1. */ |
607 | if (i < nstates) |
608 | { |
609 | size_t t = tally[i]; |
610 | int w = width[i]; |
611 | |
612 | /* If VECTOR has GLR conflicts, return -1 */ |
613 | if (conflict_tos[i] != NULL) |
614 | for (int j = 0; j < t; j += 1) |
615 | if (conflict_tos[i][j] != 0) |
616 | return -1; |
617 | |
618 | for (int prev = vector - 1; 0 <= prev; prev--) |
619 | { |
620 | vector_number j = order[prev]; |
621 | /* Given how ORDER was computed, if the WIDTH or TALLY is |
622 | different, there cannot be a matching state. */ |
623 | if (width[j] != w || tally[j] != t) |
624 | return -1; |
625 | else |
626 | { |
627 | bool match = true; |
628 | for (int k = 0; match && k < t; k++) |
629 | if (tos[j][k] != tos[i][k] |
630 | || froms[j][k] != froms[i][k] |
631 | || (conflict_tos[j] != NULL && conflict_tos[j][k] != 0)) |
632 | match = false; |
633 | if (match) |
634 | return j; |
635 | } |
636 | } |
637 | } |
638 | return -1; |
639 | } |
640 | |
641 | |
642 | static base_number |
643 | pack_vector (vector_number vector) |
644 | { |
645 | vector_number i = order[vector]; |
646 | size_t t = tally[i]; |
647 | base_number *from = froms[i]; |
648 | base_number *to = tos[i]; |
649 | unsigned *conflict_to = conflict_tos[i]; |
650 | |
651 | aver (t != 0); |
652 | |
653 | for (base_number res = lowzero - from[0]; ; res++) |
654 | { |
655 | bool ok = true; |
656 | aver (res < table_size); |
657 | { |
658 | for (int k = 0; ok && k < t; k++) |
659 | { |
660 | int loc = res + state_number_as_int (from[k]); |
661 | if (table_size <= loc) |
662 | table_grow (loc); |
663 | |
664 | if (table[loc] != 0) |
665 | ok = false; |
666 | } |
667 | |
668 | if (ok && bitset_test (pos_set, nstates + res)) |
669 | ok = false; |
670 | } |
671 | |
672 | if (ok) |
673 | { |
674 | int loc PACIFY_CC (= -1); |
675 | for (int k = 0; k < t; k++) |
676 | { |
677 | loc = res + state_number_as_int (from[k]); |
678 | table[loc] = to[k]; |
679 | if (nondeterministic_parser && conflict_to != NULL) |
680 | conflict_table[loc] = conflict_to[k]; |
681 | check[loc] = from[k]; |
682 | } |
683 | |
684 | while (table[lowzero] != 0) |
685 | lowzero++; |
686 | |
687 | if (high < loc) |
688 | high = loc; |
689 | |
690 | aver (BASE_MINIMUM <= res && res <= BASE_MAXIMUM); |
691 | return res; |
692 | } |
693 | } |
694 | } |
695 | |
696 | |
697 | /*-------------------------------------------------------------. |
698 | | Remap the negative infinite in TAB from NINF to the greatest | |
699 | | possible smallest value. Return it. | |
700 | | | |
701 | | In most case this allows us to use shorts instead of ints in | |
702 | | parsers. | |
703 | `-------------------------------------------------------------*/ |
704 | |
705 | static base_number |
706 | table_ninf_remap (base_number tab[], int size, base_number ninf) |
707 | { |
708 | base_number res = 0; |
709 | |
710 | for (int i = 0; i < size; i++) |
711 | if (tab[i] < res && tab[i] != ninf) |
712 | res = tab[i]; |
713 | |
714 | --res; |
715 | |
716 | for (int i = 0; i < size; i++) |
717 | if (tab[i] == ninf) |
718 | tab[i] = res; |
719 | |
720 | return res; |
721 | } |
722 | |
723 | static void |
724 | pack_table (void) |
725 | { |
726 | base = xnmalloc (nvectors, sizeof *base); |
727 | pos_set = bitset_create (table_size + nstates, BITSET_FRUGAL); |
728 | table = xcalloc (table_size, sizeof *table); |
729 | conflict_table = xcalloc (table_size, sizeof *conflict_table); |
730 | check = xnmalloc (table_size, sizeof *check); |
731 | |
732 | lowzero = 0; |
733 | high = 0; |
734 | |
735 | for (int i = 0; i < nvectors; i++) |
736 | base[i] = BASE_MINIMUM; |
737 | |
738 | for (int i = 0; i < table_size; i++) |
739 | check[i] = -1; |
740 | |
741 | for (int i = 0; i < nentries; i++) |
742 | { |
743 | state_number s = matching_state (i); |
744 | base_number place; |
745 | |
746 | if (s < 0) |
747 | /* A new set of state actions, or a nonterminal. */ |
748 | place = pack_vector (i); |
749 | else |
750 | /* Action of I were already coded for S. */ |
751 | place = base[s]; |
752 | |
753 | /* Store PLACE into POS_SET. PLACE might not belong to the set |
754 | of possible values for instance with useless tokens. It |
755 | would be more satisfying to eliminate the need for this |
756 | 'if'. */ |
757 | if (0 <= nstates + place) |
758 | bitset_set (pos_set, nstates + place); |
759 | base[order[i]] = place; |
760 | } |
761 | |
762 | /* Use the greatest possible negative infinites. */ |
763 | base_ninf = table_ninf_remap (base, nvectors, BASE_MINIMUM); |
764 | table_ninf = table_ninf_remap (table, high + 1, ACTION_NUMBER_MINIMUM); |
765 | |
766 | bitset_free (pos_set); |
767 | } |
768 | |
769 | |
770 | |
771 | /*-----------------------------------------------------------------. |
772 | | Compute and output yydefact, yydefgoto, yypact, yypgoto, yytable | |
773 | | and yycheck. | |
774 | `-----------------------------------------------------------------*/ |
775 | |
776 | void |
777 | tables_generate (void) |
778 | { |
779 | /* This is a poor way to make sure the sizes are properly |
780 | correlated. In particular the signedness is not taken into |
781 | account. But it's not useless. */ |
782 | verify (sizeof nstates <= sizeof nvectors); |
783 | verify (sizeof nvars <= sizeof nvectors); |
784 | |
785 | nvectors = state_number_as_int (nstates) + nvars; |
786 | |
787 | froms = xcalloc (nvectors, sizeof *froms); |
788 | tos = xcalloc (nvectors, sizeof *tos); |
789 | conflict_tos = xcalloc (nvectors, sizeof *conflict_tos); |
790 | tally = xcalloc (nvectors, sizeof *tally); |
791 | width = xnmalloc (nvectors, sizeof *width); |
792 | |
793 | token_actions (); |
794 | |
795 | goto_actions (); |
796 | free (goto_map); |
797 | free (from_state); |
798 | free (to_state); |
799 | |
800 | order = xcalloc (nvectors, sizeof *order); |
801 | sort_actions (); |
802 | pack_table (); |
803 | free (order); |
804 | |
805 | free (tally); |
806 | free (width); |
807 | |
808 | for (int i = 0; i < nvectors; i++) |
809 | { |
810 | free (froms[i]); |
811 | free (tos[i]); |
812 | free (conflict_tos[i]); |
813 | } |
814 | |
815 | free (froms); |
816 | free (tos); |
817 | free (conflict_tos); |
818 | } |
819 | |
820 | |
821 | /*-------------------------. |
822 | | Free the parser tables. | |
823 | `-------------------------*/ |
824 | |
825 | void |
826 | tables_free (void) |
827 | { |
828 | free (base); |
829 | free (conflict_table); |
830 | free (conflict_list); |
831 | free (table); |
832 | free (check); |
833 | free (yydefgoto); |
834 | free (yydefact); |
835 | } |
836 | |