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