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