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