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