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