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