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