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