]>
Commit | Line | Data |
---|---|---|
443594d0 | 1 | /* Output the generated parsing program for Bison. |
ff5c8b85 | 2 | |
3209eb1c | 3 | Copyright (C) 1984, 1986, 1989, 1992, 2000-2006, 2009-2015 Free |
575619af | 4 | 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> | |
c801dbf7 PE |
25 | |
26 | #include "complain.h" | |
27 | #include "conflicts.h" | |
c6f1a33c | 28 | #include "files.h" |
c801dbf7 | 29 | #include "getargs.h" |
c6f1a33c | 30 | #include "gram.h" |
c6f1a33c | 31 | #include "lalr.h" |
7254f6a8 | 32 | #include "muscle-tab.h" |
c6f1a33c AD |
33 | #include "reader.h" |
34 | #include "symtab.h" | |
c6f1a33c AD |
35 | #include "tables.h" |
36 | ||
443594d0 | 37 | /* Several tables are indexed both by state and nonterminal numbers. |
45eebca4 | 38 | We call such an index a 'vector'; i.e., a vector is either a state |
443594d0 | 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 | 43 | typedef int vector_number; |
ff5c8b85 | 44 | |
7cf96b1a | 45 | #if 0 /* Not currently used. */ |
ff5c8b85 PE |
46 | static inline vector_number |
47 | state_number_to_vector_number (state_number s) | |
48 | { | |
49 | return s; | |
50 | } | |
7cf96b1a | 51 | #endif |
ff5c8b85 PE |
52 | |
53 | static inline vector_number | |
a737b216 | 54 | symbol_number_to_vector_number (symbol_number sym) |
ff5c8b85 | 55 | { |
a737b216 | 56 | return state_number_as_int (nstates) + sym - ntokens; |
ff5c8b85 | 57 | } |
c6f1a33c AD |
58 | |
59 | int 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 |
84 | static base_number **froms; |
85 | static base_number **tos; | |
86 | static unsigned int **conflict_tos; | |
e697f05b | 87 | static size_t *tally; |
da2a7671 | 88 | static base_number *width; |
c6f1a33c AD |
89 | |
90 | ||
91 | /* For a given state, N = ACTROW[SYMBOL]: | |
92 | ||
45eebca4 AD |
93 | If N = 0, stands for 'run the default action'. |
94 | If N = MIN, stands for 'raise a syntax error'. | |
95 | If N > 0, stands for 'shift SYMBOL and go to n'. | |
96 | If N < 0, stands for 'reduce -N'. */ | |
f6fbd3da PE |
97 | typedef int action_number; |
98 | #define ACTION_NUMBER_MINIMUM INT_MIN | |
c6f1a33c | 99 | |
da2a7671 | 100 | static action_number *actrow; |
c6f1a33c AD |
101 | |
102 | /* FROMS and TOS are reordered to be compressed. ORDER[VECTOR] is the | |
45eebca4 AD |
103 | new vector number of VECTOR. We skip 'empty' vectors (i.e., |
104 | TALLY[VECTOR] = 0), and call these 'entries'. */ | |
da2a7671 | 105 | static vector_number *order; |
c6f1a33c AD |
106 | static int nentries; |
107 | ||
c801dbf7 | 108 | base_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 |
112 | base_number base_ninf = 0; |
113 | static base_number *pos = NULL; | |
c6f1a33c | 114 | |
da2a7671 PE |
115 | static unsigned int *conflrow; |
116 | unsigned int *conflict_table; | |
117 | unsigned int *conflict_list; | |
c6f1a33c AD |
118 | int conflict_list_cnt; |
119 | static int conflict_list_free; | |
120 | ||
121 | /* TABLE_SIZE is the allocated size of both TABLE and CHECK. We start | |
122 | with more or less the original hard-coded value (which was | |
123 | SHRT_MAX). */ | |
ff5c8b85 | 124 | static int table_size = 32768; |
da2a7671 PE |
125 | base_number *table; |
126 | base_number *check; | |
6e649e65 | 127 | /* The value used in TABLE to denote explicit syntax errors |
aa0cb40d | 128 | (%nonassoc), a negative infinite. First defaults to ACTION_NUMBER_MINIMUM, |
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 | 131 | base_number table_ninf = 0; |
c6f1a33c AD |
132 | static int lowzero; |
133 | int high; | |
134 | ||
c801dbf7 PE |
135 | state_number *yydefgoto; |
136 | rule_number *yydefact; | |
c6f1a33c | 137 | |
db598939 AD |
138 | /*-------------------------------------------------------------------. |
139 | | If TABLE, CONFLICT_TABLE, and CHECK are too small to be addressed | | |
140 | | at DESIRED, grow them. TABLE[DESIRED] can be used, so the desired | | |
141 | | size is at least DESIRED + 1. | | |
142 | `-------------------------------------------------------------------*/ | |
c6f1a33c AD |
143 | |
144 | static void | |
ff5c8b85 | 145 | table_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 | 153 | fprintf (stderr, "growing table and check from: %d to %d\n", |
e9690142 | 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, | |
e9690142 | 158 | sizeof *conflict_table); |
da2a7671 | 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 | |
e9690142 JD |
174 | | by non-zero entries in CONFLROW, create a list of possible | |
175 | | reductions that are alternatives to the shift or reduction | | |
c801dbf7 | 176 | | currently recorded for that token in S. Store the alternative | |
e9690142 | 177 | | reductions followed by a 0 in CONFLICT_LIST, updating | |
c6f1a33c | 178 | | CONFLICT_LIST_CNT, and storing an index to the start of the list | |
e9690142 | 179 | | back into CONFLROW. | |
c6f1a33c AD |
180 | `-------------------------------------------------------------------*/ |
181 | ||
182 | static void | |
c801dbf7 | 183 | conflict_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 | { | |
e9690142 JD |
194 | conflrow[j] = conflict_list_cnt; |
195 | ||
196 | /* Find all reductions for token J, and record all that do not | |
197 | match ACTROW[J]. */ | |
198 | for (i = 0; i < reds->num; i += 1) | |
199 | if (bitset_test (reds->lookahead_tokens[i], j) | |
200 | && (actrow[j] | |
201 | != rule_number_as_item_number (reds->rules[i]->number))) | |
202 | { | |
203 | aver (0 < conflict_list_free); | |
204 | conflict_list[conflict_list_cnt] = reds->rules[i]->number + 1; | |
205 | conflict_list_cnt += 1; | |
206 | conflict_list_free -= 1; | |
207 | } | |
208 | ||
209 | /* Leave a 0 at the end. */ | |
210 | aver (0 < conflict_list_free); | |
211 | conflict_list[conflict_list_cnt] = 0; | |
212 | conflict_list_cnt += 1; | |
213 | conflict_list_free -= 1; | |
c6f1a33c AD |
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 | | |
e9690142 JD |
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 |
238 | static rule * |
239 | action_row (state *s) | |
c6f1a33c AD |
240 | { |
241 | int i; | |
110ef36a | 242 | rule *default_reduction = NULL; |
c801dbf7 PE |
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 | |
e9690142 JD |
258 | lookahead (in reverse order to give precedence to the first |
259 | rule) */ | |
c801dbf7 | 260 | for (i = reds->num - 1; i >= 0; --i) |
e9690142 JD |
261 | /* and find each token which the rule finds acceptable |
262 | to come next */ | |
263 | BITSET_FOR_EACH (biter, reds->lookahead_tokens[i], j, 0) | |
264 | { | |
265 | /* and record this rule as the rule to use if that | |
266 | token follows. */ | |
267 | if (actrow[j] != 0) | |
268 | { | |
269 | conflicted = true; | |
270 | conflrow[j] = 1; | |
271 | } | |
272 | actrow[j] = rule_number_as_item_number (reds->rules[i]->number); | |
273 | } | |
c6f1a33c AD |
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) |
e9690142 JD |
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 | |
e9690142 | 292 | error */ |
c801dbf7 | 293 | if (sym == errtoken->number) |
e9690142 | 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 | ||
110ef36a | 306 | /* Turn off default reductions where requested by the user. See |
7254f6a8 JD |
307 | state_lookahead_tokens_count in lalr.c to understand when states are |
308 | labeled as consistent. */ | |
309 | { | |
110ef36a | 310 | char *default_reductions = |
f3bc3386 | 311 | muscle_percent_define_get ("lr.default-reduction"); |
f518dbaf | 312 | if (STRNEQ (default_reductions, "most") && !s->consistent) |
7254f6a8 | 313 | nodefault = true; |
110ef36a | 314 | free (default_reductions); |
7254f6a8 JD |
315 | } |
316 | ||
c6f1a33c AD |
317 | /* Now find the most common reduction and make it the default action |
318 | for this state. */ | |
319 | ||
c801dbf7 | 320 | if (reds->num >= 1 && !nodefault) |
c6f1a33c | 321 | { |
c801dbf7 | 322 | if (s->consistent) |
e9690142 | 323 | default_reduction = reds->rules[0]; |
c6f1a33c | 324 | else |
e9690142 JD |
325 | { |
326 | int max = 0; | |
327 | for (i = 0; i < reds->num; i++) | |
328 | { | |
329 | int count = 0; | |
330 | rule *r = reds->rules[i]; | |
331 | symbol_number j; | |
332 | ||
333 | for (j = 0; j < ntokens; j++) | |
334 | if (actrow[j] == rule_number_as_item_number (r->number)) | |
335 | count++; | |
336 | ||
337 | if (count > max) | |
338 | { | |
339 | max = count; | |
340 | default_reduction = r; | |
341 | } | |
342 | } | |
343 | ||
344 | /* GLR parsers need space for conflict lists, so we can't | |
345 | default conflicted entries. For non-conflicted entries | |
346 | or as long as we are not building a GLR parser, | |
347 | actions that match the default are replaced with zero, | |
348 | which means "use the default". */ | |
349 | ||
350 | if (max > 0) | |
351 | { | |
352 | int j; | |
353 | for (j = 0; j < ntokens; j++) | |
354 | if (actrow[j] | |
110ef36a | 355 | == rule_number_as_item_number (default_reduction->number) |
e9690142 JD |
356 | && ! (nondeterministic_parser && conflrow[j])) |
357 | actrow[j] = 0; | |
358 | } | |
359 | } | |
c6f1a33c AD |
360 | } |
361 | ||
110ef36a | 362 | /* If have no default reduction, the default is an error. |
c6f1a33c AD |
363 | So replace any action which says "error" with "use default". */ |
364 | ||
110ef36a | 365 | if (!default_reduction) |
c6f1a33c | 366 | for (i = 0; i < ntokens; i++) |
c801dbf7 | 367 | if (actrow[i] == ACTION_NUMBER_MINIMUM) |
e9690142 | 368 | actrow[i] = 0; |
c6f1a33c AD |
369 | |
370 | if (conflicted) | |
c801dbf7 | 371 | conflict_row (s); |
c6f1a33c | 372 | |
110ef36a | 373 | return default_reduction; |
c6f1a33c AD |
374 | } |
375 | ||
376 | ||
c801dbf7 PE |
377 | /*----------------------------------------. |
378 | | Set FROMS, TOS, TALLY and WIDTH for S. | | |
379 | `----------------------------------------*/ | |
c6f1a33c AD |
380 | |
381 | static void | |
c801dbf7 | 382 | save_row (state_number s) |
c6f1a33c | 383 | { |
c801dbf7 | 384 | symbol_number i; |
c6f1a33c | 385 | |
c801dbf7 | 386 | /* Number of non default actions in S. */ |
c95f5dad | 387 | size_t count = 0; |
c6f1a33c AD |
388 | for (i = 0; i < ntokens; i++) |
389 | if (actrow[i] != 0) | |
390 | count++; | |
391 | ||
c95f5dad AD |
392 | if (count) |
393 | { | |
394 | /* Allocate non defaulted actions. */ | |
395 | base_number *sp1 = froms[s] = xnmalloc (count, sizeof *sp1); | |
396 | base_number *sp2 = tos[s] = xnmalloc (count, sizeof *sp2); | |
397 | unsigned int *sp3 = conflict_tos[s] = | |
11aef5e9 | 398 | nondeterministic_parser ? xnmalloc (count, sizeof *sp3) : NULL; |
c95f5dad AD |
399 | |
400 | /* Store non defaulted actions. */ | |
401 | for (i = 0; i < ntokens; i++) | |
402 | if (actrow[i] != 0) | |
403 | { | |
404 | *sp1++ = i; | |
405 | *sp2++ = actrow[i]; | |
406 | if (nondeterministic_parser) | |
407 | *sp3++ = conflrow[i]; | |
408 | } | |
409 | ||
410 | tally[s] = count; | |
411 | width[s] = sp1[-1] - froms[s][0] + 1; | |
412 | } | |
c6f1a33c AD |
413 | } |
414 | ||
415 | ||
416 | /*------------------------------------------------------------------. | |
417 | | Figure out the actions for the specified state, indexed by | | |
742e4900 | 418 | | lookahead token type. | |
c6f1a33c AD |
419 | | | |
420 | | The YYDEFACT table is output now. The detailed info is saved for | | |
421 | | putting into YYTABLE later. | | |
422 | `------------------------------------------------------------------*/ | |
423 | ||
424 | static void | |
425 | token_actions (void) | |
426 | { | |
916708d5 | 427 | int nconflict = nondeterministic_parser ? conflicts_total_count () : 0; |
c6f1a33c | 428 | |
da2a7671 | 429 | yydefact = xnmalloc (nstates, sizeof *yydefact); |
c6f1a33c | 430 | |
da2a7671 PE |
431 | actrow = xnmalloc (ntokens, sizeof *actrow); |
432 | conflrow = xnmalloc (ntokens, sizeof *conflrow); | |
c6f1a33c | 433 | |
da2a7671 | 434 | conflict_list = xnmalloc (1 + 2 * nconflict, sizeof *conflict_list); |
ea99527d AD |
435 | conflict_list_free = 2 * nconflict; |
436 | conflict_list_cnt = 1; | |
437 | ||
c8f002c7 | 438 | /* Find the rules which are reduced. */ |
916708d5 | 439 | if (!nondeterministic_parser) |
c6f1a33c | 440 | { |
33f7f342 AD |
441 | rule_number r; |
442 | for (r = 0; r < nrules; ++r) | |
443 | rules[r].useful = false; | |
c8f002c7 | 444 | } |
c6f1a33c | 445 | |
33f7f342 AD |
446 | { |
447 | state_number i; | |
448 | for (i = 0; i < nstates; ++i) | |
449 | { | |
450 | rule *default_reduction = action_row (states[i]); | |
451 | yydefact[i] = default_reduction ? default_reduction->number + 1 : 0; | |
452 | save_row (i); | |
453 | ||
454 | /* Now that the parser was computed, we can find which rules are | |
455 | really reduced, and which are not because of SR or RR | |
456 | conflicts. */ | |
457 | if (!nondeterministic_parser) | |
458 | { | |
459 | symbol_number j; | |
460 | for (j = 0; j < ntokens; ++j) | |
461 | if (actrow[j] < 0 && actrow[j] != ACTION_NUMBER_MINIMUM) | |
462 | rules[item_number_as_rule_number (actrow[j])].useful = true; | |
463 | if (yydefact[i]) | |
464 | rules[yydefact[i] - 1].useful = true; | |
465 | } | |
466 | } | |
467 | } | |
c6f1a33c AD |
468 | free (actrow); |
469 | free (conflrow); | |
470 | } | |
471 | ||
472 | ||
473 | /*------------------------------------------------------------------. | |
474 | | Compute FROMS[VECTOR], TOS[VECTOR], TALLY[VECTOR], WIDTH[VECTOR], | | |
475 | | i.e., the information related to non defaulted GOTO on the nterm | | |
c801dbf7 | 476 | | SYM. | |
c6f1a33c | 477 | | | |
c801dbf7 PE |
478 | | DEFAULT_STATE is the principal destination on SYM, i.e., the | |
479 | | default GOTO destination on SYM. | | |
c6f1a33c AD |
480 | `------------------------------------------------------------------*/ |
481 | ||
482 | static void | |
c801dbf7 | 483 | save_column (symbol_number sym, state_number default_state) |
c6f1a33c | 484 | { |
78143b92 | 485 | goto_number i; |
ff5c8b85 PE |
486 | goto_number begin = goto_map[sym - ntokens]; |
487 | goto_number end = goto_map[sym - ntokens + 1]; | |
c6f1a33c AD |
488 | |
489 | /* Number of non default GOTO. */ | |
e697f05b | 490 | size_t count = 0; |
c6f1a33c AD |
491 | for (i = begin; i < end; i++) |
492 | if (to_state[i] != default_state) | |
493 | count++; | |
494 | ||
28be3b8e AD |
495 | if (count) |
496 | { | |
497 | /* Allocate room for non defaulted gotos. */ | |
498 | vector_number symno = symbol_number_to_vector_number (sym); | |
499 | base_number *sp1 = froms[symno] = xnmalloc (count, sizeof *sp1); | |
500 | base_number *sp2 = tos[symno] = xnmalloc (count, sizeof *sp2); | |
501 | ||
502 | /* Store the state numbers of the non defaulted gotos. */ | |
503 | for (i = begin; i < end; i++) | |
504 | if (to_state[i] != default_state) | |
505 | { | |
506 | *sp1++ = from_state[i]; | |
507 | *sp2++ = to_state[i]; | |
508 | } | |
509 | ||
510 | tally[symno] = count; | |
511 | width[symno] = sp1[-1] - froms[symno][0] + 1; | |
512 | } | |
c6f1a33c AD |
513 | } |
514 | ||
515 | ||
11aef5e9 AD |
516 | /*----------------------------------------------------------------. |
517 | | The default state for SYM: the state which is 'the' most common | | |
518 | | GOTO destination on SYM (an nterm). | | |
519 | `----------------------------------------------------------------*/ | |
c6f1a33c | 520 | |
c801dbf7 | 521 | static state_number |
f6fbd3da | 522 | default_goto (symbol_number sym, size_t state_count[]) |
c6f1a33c | 523 | { |
11aef5e9 AD |
524 | goto_number begin = goto_map[sym - ntokens]; |
525 | goto_number end = goto_map[sym - ntokens + 1]; | |
526 | state_number res = -1; | |
c6f1a33c | 527 | |
11aef5e9 AD |
528 | if (begin != end) |
529 | { | |
530 | size_t max = 0; | |
531 | goto_number i; | |
532 | state_number s; | |
c6f1a33c | 533 | |
11aef5e9 AD |
534 | for (s = 0; s < nstates; s++) |
535 | state_count[s] = 0; | |
c6f1a33c | 536 | |
11aef5e9 AD |
537 | for (i = begin; i < end; i++) |
538 | state_count[to_state[i]]++; | |
c6f1a33c | 539 | |
11aef5e9 AD |
540 | for (s = 0; s < nstates; s++) |
541 | if (max < state_count[s]) | |
542 | { | |
543 | max = state_count[s]; | |
544 | res = s; | |
545 | } | |
546 | } | |
547 | return res; | |
c6f1a33c AD |
548 | } |
549 | ||
550 | ||
551 | /*-------------------------------------------------------------------. | |
552 | | Figure out what to do after reducing with each rule, depending on | | |
553 | | the saved state from before the beginning of parsing the data that | | |
554 | | matched this rule. | | |
555 | | | | |
556 | | The YYDEFGOTO table is output now. The detailed info is saved for | | |
557 | | putting into YYTABLE later. | | |
558 | `-------------------------------------------------------------------*/ | |
559 | ||
560 | static void | |
561 | goto_actions (void) | |
562 | { | |
c801dbf7 | 563 | symbol_number i; |
f6fbd3da | 564 | size_t *state_count = xnmalloc (nstates, sizeof *state_count); |
da2a7671 | 565 | yydefgoto = xnmalloc (nvars, sizeof *yydefgoto); |
c6f1a33c AD |
566 | |
567 | /* For a given nterm I, STATE_COUNT[S] is the number of times there | |
568 | is a GOTO to S on I. */ | |
569 | for (i = ntokens; i < nsyms; ++i) | |
570 | { | |
c801dbf7 | 571 | state_number default_state = default_goto (i, state_count); |
c6f1a33c AD |
572 | save_column (i, default_state); |
573 | yydefgoto[i - ntokens] = default_state; | |
574 | } | |
575 | free (state_count); | |
576 | } | |
577 | ||
578 | ||
579 | /*------------------------------------------------------------------. | |
580 | | Compute ORDER, a reordering of vectors, in order to decide how to | | |
581 | | pack the actions and gotos information into yytable. | | |
582 | `------------------------------------------------------------------*/ | |
583 | ||
584 | static void | |
585 | sort_actions (void) | |
586 | { | |
587 | int i; | |
588 | ||
589 | nentries = 0; | |
590 | ||
591 | for (i = 0; i < nvectors; i++) | |
db598939 | 592 | if (0 < tally[i]) |
c6f1a33c | 593 | { |
e9690142 | 594 | int k; |
e697f05b | 595 | size_t t = tally[i]; |
e9690142 JD |
596 | int w = width[i]; |
597 | int j = nentries - 1; | |
c6f1a33c | 598 | |
db598939 | 599 | while (0 <= j && width[order[j]] < w) |
e9690142 | 600 | j--; |
c6f1a33c | 601 | |
db598939 | 602 | while (0 <= j && width[order[j]] == w && tally[order[j]] < t) |
e9690142 | 603 | j--; |
c6f1a33c | 604 | |
e9690142 JD |
605 | for (k = nentries - 1; k > j; k--) |
606 | order[k + 1] = order[k]; | |
c6f1a33c | 607 | |
e9690142 JD |
608 | order[j + 1] = i; |
609 | nentries++; | |
c6f1a33c AD |
610 | } |
611 | } | |
612 | ||
613 | ||
db598939 | 614 | /* If VECTOR is a state whose actions (reflected by FROMS, TOS, TALLY |
c6f1a33c AD |
615 | and WIDTH of VECTOR) are common to a previous state, return this |
616 | state number. | |
617 | ||
618 | In any other case, return -1. */ | |
619 | ||
c801dbf7 PE |
620 | static state_number |
621 | matching_state (vector_number vector) | |
c6f1a33c | 622 | { |
c801dbf7 | 623 | vector_number i = order[vector]; |
c6f1a33c | 624 | /* If VECTOR is a nterm, return -1. */ |
465df9c6 | 625 | if (i < nstates) |
51b4a04c | 626 | { |
465df9c6 AD |
627 | size_t t = tally[i]; |
628 | int w = width[i]; | |
629 | int prev; | |
51b4a04c | 630 | |
465df9c6 AD |
631 | /* If VECTOR has GLR conflicts, return -1 */ |
632 | if (conflict_tos[i] != NULL) | |
633 | { | |
634 | int j; | |
635 | for (j = 0; j < t; j += 1) | |
636 | if (conflict_tos[i][j] != 0) | |
637 | return -1; | |
638 | } | |
c6f1a33c | 639 | |
465df9c6 AD |
640 | for (prev = vector - 1; 0 <= prev; prev--) |
641 | { | |
642 | vector_number j = order[prev]; | |
643 | /* Given how ORDER was computed, if the WIDTH or TALLY is | |
644 | different, there cannot be a matching state. */ | |
645 | if (width[j] != w || tally[j] != t) | |
646 | return -1; | |
647 | else | |
648 | { | |
649 | bool match = true; | |
650 | int k; | |
651 | for (k = 0; match && k < t; k++) | |
652 | if (tos[j][k] != tos[i][k] | |
653 | || froms[j][k] != froms[i][k] | |
654 | || (conflict_tos[j] != NULL && conflict_tos[j][k] != 0)) | |
655 | match = false; | |
656 | if (match) | |
657 | return j; | |
658 | } | |
659 | } | |
660 | } | |
c6f1a33c AD |
661 | return -1; |
662 | } | |
663 | ||
664 | ||
c801dbf7 PE |
665 | static base_number |
666 | pack_vector (vector_number vector) | |
c6f1a33c | 667 | { |
28be3b8e | 668 | base_number res; |
c801dbf7 | 669 | vector_number i = order[vector]; |
e697f05b | 670 | size_t t = tally[i]; |
c801dbf7 PE |
671 | base_number *from = froms[i]; |
672 | base_number *to = tos[i]; | |
c6f1a33c AD |
673 | unsigned int *conflict_to = conflict_tos[i]; |
674 | ||
4f82b42a | 675 | aver (t != 0); |
c6f1a33c | 676 | |
28be3b8e | 677 | for (res = lowzero - from[0]; ; res++) |
c6f1a33c | 678 | { |
d0829076 | 679 | bool ok = true; |
28be3b8e AD |
680 | aver (res < table_size); |
681 | { | |
682 | int k; | |
683 | for (k = 0; ok && k < t; k++) | |
684 | { | |
685 | int loc = res + state_number_as_int (from[k]); | |
686 | if (table_size <= loc) | |
687 | table_grow (loc); | |
688 | ||
689 | if (table[loc] != 0) | |
690 | ok = false; | |
691 | } | |
692 | ||
693 | if (ok) | |
694 | for (k = 0; k < vector; k++) | |
695 | if (pos[k] == res) | |
696 | ok = false; | |
697 | } | |
c6f1a33c AD |
698 | |
699 | if (ok) | |
e9690142 | 700 | { |
a2c525b3 | 701 | int loc PACIFY_CC (= -1); |
28be3b8e | 702 | int k; |
e9690142 JD |
703 | for (k = 0; k < t; k++) |
704 | { | |
28be3b8e | 705 | loc = res + state_number_as_int (from[k]); |
e9690142 JD |
706 | table[loc] = to[k]; |
707 | if (nondeterministic_parser && conflict_to != NULL) | |
708 | conflict_table[loc] = conflict_to[k]; | |
709 | check[loc] = from[k]; | |
710 | } | |
711 | ||
712 | while (table[lowzero] != 0) | |
713 | lowzero++; | |
714 | ||
db598939 | 715 | if (high < loc) |
e9690142 JD |
716 | high = loc; |
717 | ||
28be3b8e AD |
718 | aver (BASE_MINIMUM <= res && res <= BASE_MAXIMUM); |
719 | return res; | |
e9690142 | 720 | } |
c6f1a33c | 721 | } |
c6f1a33c AD |
722 | } |
723 | ||
724 | ||
725 | /*-------------------------------------------------------------. | |
726 | | Remap the negative infinite in TAB from NINF to the greatest | | |
727 | | possible smallest value. Return it. | | |
728 | | | | |
729 | | In most case this allows us to use shorts instead of ints in | | |
730 | | parsers. | | |
731 | `-------------------------------------------------------------*/ | |
732 | ||
c801dbf7 | 733 | static base_number |
ff5c8b85 | 734 | table_ninf_remap (base_number tab[], int size, base_number ninf) |
c6f1a33c | 735 | { |
c801dbf7 | 736 | base_number res = 0; |
ff5c8b85 | 737 | int i; |
c6f1a33c AD |
738 | |
739 | for (i = 0; i < size; i++) | |
740 | if (tab[i] < res && tab[i] != ninf) | |
05846dae | 741 | res = tab[i]; |
c6f1a33c AD |
742 | |
743 | --res; | |
744 | ||
745 | for (i = 0; i < size; i++) | |
746 | if (tab[i] == ninf) | |
747 | tab[i] = res; | |
748 | ||
749 | return res; | |
750 | } | |
751 | ||
752 | static void | |
753 | pack_table (void) | |
754 | { | |
755 | int i; | |
756 | ||
da2a7671 PE |
757 | base = xnmalloc (nvectors, sizeof *base); |
758 | pos = xnmalloc (nentries, sizeof *pos); | |
759 | table = xcalloc (table_size, sizeof *table); | |
760 | conflict_table = xcalloc (table_size, sizeof *conflict_table); | |
761 | check = xnmalloc (table_size, sizeof *check); | |
c6f1a33c AD |
762 | |
763 | lowzero = 0; | |
764 | high = 0; | |
765 | ||
766 | for (i = 0; i < nvectors; i++) | |
c801dbf7 | 767 | base[i] = BASE_MINIMUM; |
c6f1a33c | 768 | |
ff5c8b85 | 769 | for (i = 0; i < table_size; i++) |
c6f1a33c AD |
770 | check[i] = -1; |
771 | ||
772 | for (i = 0; i < nentries; i++) | |
773 | { | |
c801dbf7 PE |
774 | state_number s = matching_state (i); |
775 | base_number place; | |
c6f1a33c | 776 | |
c801dbf7 | 777 | if (s < 0) |
e9690142 JD |
778 | /* A new set of state actions, or a nonterminal. */ |
779 | place = pack_vector (i); | |
c6f1a33c | 780 | else |
e9690142 JD |
781 | /* Action of I were already coded for S. */ |
782 | place = base[s]; | |
c6f1a33c AD |
783 | |
784 | pos[i] = place; | |
785 | base[order[i]] = place; | |
786 | } | |
787 | ||
788 | /* Use the greatest possible negative infinites. */ | |
c801dbf7 PE |
789 | base_ninf = table_ninf_remap (base, nvectors, BASE_MINIMUM); |
790 | table_ninf = table_ninf_remap (table, high + 1, ACTION_NUMBER_MINIMUM); | |
c6f1a33c | 791 | |
c6f1a33c AD |
792 | free (pos); |
793 | } | |
794 | ||
795 | \f | |
796 | ||
797 | /*-----------------------------------------------------------------. | |
798 | | Compute and output yydefact, yydefgoto, yypact, yypgoto, yytable | | |
799 | | and yycheck. | | |
800 | `-----------------------------------------------------------------*/ | |
801 | ||
802 | void | |
803 | tables_generate (void) | |
804 | { | |
3325ddc4 AD |
805 | int i; |
806 | ||
443594d0 PE |
807 | /* This is a poor way to make sure the sizes are properly |
808 | correlated. In particular the signedness is not taken into | |
809 | account. But it's not useless. */ | |
8a6f72f3 | 810 | verify (sizeof nstates <= sizeof nvectors |
e9690142 | 811 | && sizeof nvars <= sizeof nvectors); |
c6f1a33c AD |
812 | |
813 | nvectors = state_number_as_int (nstates) + nvars; | |
814 | ||
da2a7671 PE |
815 | froms = xcalloc (nvectors, sizeof *froms); |
816 | tos = xcalloc (nvectors, sizeof *tos); | |
817 | conflict_tos = xcalloc (nvectors, sizeof *conflict_tos); | |
818 | tally = xcalloc (nvectors, sizeof *tally); | |
819 | width = xnmalloc (nvectors, sizeof *width); | |
c6f1a33c AD |
820 | |
821 | token_actions (); | |
c6f1a33c AD |
822 | |
823 | goto_actions (); | |
ff5c8b85 | 824 | free (goto_map); |
b1ae9233 AD |
825 | free (from_state); |
826 | free (to_state); | |
c6f1a33c | 827 | |
da2a7671 | 828 | order = xcalloc (nvectors, sizeof *order); |
c6f1a33c AD |
829 | sort_actions (); |
830 | pack_table (); | |
831 | free (order); | |
832 | ||
833 | free (tally); | |
834 | free (width); | |
3325ddc4 AD |
835 | |
836 | for (i = 0; i < nvectors; i++) | |
837 | { | |
b1ae9233 AD |
838 | free (froms[i]); |
839 | free (tos[i]); | |
afbb696d | 840 | free (conflict_tos[i]); |
3325ddc4 AD |
841 | } |
842 | ||
843 | free (froms); | |
844 | free (tos); | |
845 | free (conflict_tos); | |
c6f1a33c AD |
846 | } |
847 | ||
848 | ||
849 | /*-------------------------. | |
850 | | Free the parser tables. | | |
851 | `-------------------------*/ | |
852 | ||
853 | void | |
854 | tables_free (void) | |
855 | { | |
856 | free (base); | |
857 | free (conflict_table); | |
858 | free (conflict_list); | |
859 | free (table); | |
860 | free (check); | |
861 | free (yydefgoto); | |
862 | free (yydefact); | |
863 | } |