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