]>
Commit | Line | Data |
---|---|---|
1 | /* Output the generated parsing program for Bison. | |
2 | ||
3 | Copyright (C) 1984, 1986, 1989, 1992, 2000, 2001, 2002 | |
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 s) | |
55 | { | |
56 | return state_number_as_int (nstates) + s - 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 (! glr_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 | int nodefault = 0; | |
247 | int conflicted = 0; | |
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 | conflicted = conflrow[j] = 1; | |
268 | actrow[j] = rule_number_as_item_number (reds->rules[i]->number); | |
269 | } | |
270 | } | |
271 | ||
272 | /* Now see which tokens are allowed for shifts in this state. For | |
273 | them, record the shift as the thing to do. So shift is preferred | |
274 | to reduce. */ | |
275 | FOR_EACH_SHIFT (trans, i) | |
276 | { | |
277 | symbol_number sym = TRANSITION_SYMBOL (trans, i); | |
278 | state *shift_state = trans->states[i]; | |
279 | ||
280 | if (actrow[sym] != 0) | |
281 | conflicted = conflrow[sym] = 1; | |
282 | actrow[sym] = state_number_as_int (shift_state->number); | |
283 | ||
284 | /* Do not use any default reduction if there is a shift for | |
285 | error */ | |
286 | if (sym == errtoken->number) | |
287 | nodefault = 1; | |
288 | } | |
289 | ||
290 | /* See which tokens are an explicit error in this state (due to | |
291 | %nonassoc). For them, record ACTION_NUMBER_MINIMUM as the | |
292 | action. */ | |
293 | for (i = 0; i < errp->num; i++) | |
294 | { | |
295 | symbol *sym = errp->symbols[i]; | |
296 | actrow[sym->number] = ACTION_NUMBER_MINIMUM; | |
297 | } | |
298 | ||
299 | /* Now find the most common reduction and make it the default action | |
300 | for this state. */ | |
301 | ||
302 | if (reds->num >= 1 && !nodefault) | |
303 | { | |
304 | if (s->consistent) | |
305 | default_rule = reds->rules[0]; | |
306 | else | |
307 | { | |
308 | int max = 0; | |
309 | for (i = 0; i < reds->num; i++) | |
310 | { | |
311 | int count = 0; | |
312 | rule *r = reds->rules[i]; | |
313 | symbol_number j; | |
314 | ||
315 | for (j = 0; j < ntokens; j++) | |
316 | if (actrow[j] == rule_number_as_item_number (r->number)) | |
317 | count++; | |
318 | ||
319 | if (count > max) | |
320 | { | |
321 | max = count; | |
322 | default_rule = r; | |
323 | } | |
324 | } | |
325 | ||
326 | /* GLR parsers need space for conflict lists, so we can't | |
327 | default conflicted entries. For non-conflicted entries | |
328 | or as long as we are not building a GLR parser, | |
329 | actions that match the default are replaced with zero, | |
330 | which means "use the default". */ | |
331 | ||
332 | if (max > 0) | |
333 | { | |
334 | int j; | |
335 | for (j = 0; j < ntokens; j++) | |
336 | if (actrow[j] == rule_number_as_item_number (default_rule->number) | |
337 | && ! (glr_parser && conflrow[j])) | |
338 | actrow[j] = 0; | |
339 | } | |
340 | } | |
341 | } | |
342 | ||
343 | /* If have no default rule, the default is an error. | |
344 | So replace any action which says "error" with "use default". */ | |
345 | ||
346 | if (!default_rule) | |
347 | for (i = 0; i < ntokens; i++) | |
348 | if (actrow[i] == ACTION_NUMBER_MINIMUM) | |
349 | actrow[i] = 0; | |
350 | ||
351 | if (conflicted) | |
352 | conflict_row (s); | |
353 | ||
354 | return default_rule; | |
355 | } | |
356 | ||
357 | ||
358 | /*----------------------------------------. | |
359 | | Set FROMS, TOS, TALLY and WIDTH for S. | | |
360 | `----------------------------------------*/ | |
361 | ||
362 | static void | |
363 | save_row (state_number s) | |
364 | { | |
365 | symbol_number i; | |
366 | int count; | |
367 | base_number *sp; | |
368 | base_number *sp1; | |
369 | base_number *sp2; | |
370 | unsigned int *sp3 IF_LINT (= NULL); | |
371 | ||
372 | /* Number of non default actions in S. */ | |
373 | count = 0; | |
374 | for (i = 0; i < ntokens; i++) | |
375 | if (actrow[i] != 0) | |
376 | count++; | |
377 | ||
378 | if (count == 0) | |
379 | return; | |
380 | ||
381 | /* Allocate non defaulted actions. */ | |
382 | froms[s] = sp = CALLOC (sp1, count); | |
383 | tos[s] = CALLOC (sp2, count); | |
384 | conflict_tos[s] = glr_parser ? CALLOC (sp3, count) : NULL; | |
385 | ||
386 | /* Store non defaulted actions. */ | |
387 | for (i = 0; i < ntokens; i++) | |
388 | if (actrow[i] != 0) | |
389 | { | |
390 | *sp1++ = i; | |
391 | *sp2++ = actrow[i]; | |
392 | if (glr_parser) | |
393 | *sp3++ = conflrow[i]; | |
394 | } | |
395 | ||
396 | tally[s] = count; | |
397 | width[s] = sp1[-1] - sp[0] + 1; | |
398 | } | |
399 | ||
400 | ||
401 | /*------------------------------------------------------------------. | |
402 | | Figure out the actions for the specified state, indexed by | | |
403 | | lookahead token type. | | |
404 | | | | |
405 | | The YYDEFACT table is output now. The detailed info is saved for | | |
406 | | putting into YYTABLE later. | | |
407 | `------------------------------------------------------------------*/ | |
408 | ||
409 | static void | |
410 | token_actions (void) | |
411 | { | |
412 | state_number i; | |
413 | symbol_number j; | |
414 | rule_number r; | |
415 | ||
416 | int nconflict = glr_parser ? conflicts_total_count () : 0; | |
417 | ||
418 | CALLOC (yydefact, nstates); | |
419 | ||
420 | CALLOC (actrow, ntokens); | |
421 | CALLOC (conflrow, ntokens); | |
422 | ||
423 | CALLOC (conflict_list, 1 + 2 * nconflict); | |
424 | conflict_list_free = 2 * nconflict; | |
425 | conflict_list_cnt = 1; | |
426 | ||
427 | /* Find the rules which are reduced. */ | |
428 | if (!glr_parser) | |
429 | for (r = 0; r < nrules; ++r) | |
430 | rules[r].useful = false; | |
431 | ||
432 | for (i = 0; i < nstates; ++i) | |
433 | { | |
434 | rule *default_rule = action_row (states[i]); | |
435 | yydefact[i] = default_rule ? default_rule->number + 1 : 0; | |
436 | save_row (i); | |
437 | ||
438 | /* Now that the parser was computed, we can find which rules are | |
439 | really reduced, and which are not because of SR or RR | |
440 | conflicts. */ | |
441 | if (!glr_parser) | |
442 | { | |
443 | for (j = 0; j < ntokens; ++j) | |
444 | if (actrow[j] < 0 && actrow[j] != ACTION_NUMBER_MINIMUM) | |
445 | rules[item_number_as_rule_number (actrow[j])].useful = true; | |
446 | if (yydefact[i]) | |
447 | rules[yydefact[i] - 1].useful = true; | |
448 | } | |
449 | } | |
450 | ||
451 | free (actrow); | |
452 | free (conflrow); | |
453 | } | |
454 | ||
455 | ||
456 | /*------------------------------------------------------------------. | |
457 | | Compute FROMS[VECTOR], TOS[VECTOR], TALLY[VECTOR], WIDTH[VECTOR], | | |
458 | | i.e., the information related to non defaulted GOTO on the nterm | | |
459 | | SYM. | | |
460 | | | | |
461 | | DEFAULT_STATE is the principal destination on SYM, i.e., the | | |
462 | | default GOTO destination on SYM. | | |
463 | `------------------------------------------------------------------*/ | |
464 | ||
465 | static void | |
466 | save_column (symbol_number sym, state_number default_state) | |
467 | { | |
468 | int i; | |
469 | base_number *sp; | |
470 | base_number *sp1; | |
471 | base_number *sp2; | |
472 | int count; | |
473 | vector_number symno = symbol_number_to_vector_number (sym); | |
474 | ||
475 | goto_number begin = goto_map[sym - ntokens]; | |
476 | goto_number end = goto_map[sym - ntokens + 1]; | |
477 | ||
478 | /* Number of non default GOTO. */ | |
479 | count = 0; | |
480 | for (i = begin; i < end; i++) | |
481 | if (to_state[i] != default_state) | |
482 | count++; | |
483 | ||
484 | if (count == 0) | |
485 | return; | |
486 | ||
487 | /* Allocate room for non defaulted gotos. */ | |
488 | froms[symno] = sp = CALLOC (sp1, count); | |
489 | tos[symno] = CALLOC (sp2, count); | |
490 | ||
491 | /* Store the state numbers of the non defaulted gotos. */ | |
492 | for (i = begin; i < end; i++) | |
493 | if (to_state[i] != default_state) | |
494 | { | |
495 | *sp1++ = from_state[i]; | |
496 | *sp2++ = to_state[i]; | |
497 | } | |
498 | ||
499 | tally[symno] = count; | |
500 | width[symno] = sp1[-1] - sp[0] + 1; | |
501 | } | |
502 | ||
503 | ||
504 | /*-------------------------------------------------------------. | |
505 | | Return `the' most common destination GOTO on SYM (a nterm). | | |
506 | `-------------------------------------------------------------*/ | |
507 | ||
508 | static state_number | |
509 | default_goto (symbol_number sym, short state_count[]) | |
510 | { | |
511 | state_number s; | |
512 | int i; | |
513 | goto_number m = goto_map[sym - ntokens]; | |
514 | goto_number n = goto_map[sym - ntokens + 1]; | |
515 | state_number default_state = -1; | |
516 | int max = 0; | |
517 | ||
518 | if (m == n) | |
519 | return -1; | |
520 | ||
521 | for (s = 0; s < nstates; s++) | |
522 | state_count[s] = 0; | |
523 | ||
524 | for (i = m; i < n; i++) | |
525 | state_count[to_state[i]]++; | |
526 | ||
527 | for (s = 0; s < nstates; s++) | |
528 | if (state_count[s] > max) | |
529 | { | |
530 | max = state_count[s]; | |
531 | default_state = s; | |
532 | } | |
533 | ||
534 | return default_state; | |
535 | } | |
536 | ||
537 | ||
538 | /*-------------------------------------------------------------------. | |
539 | | Figure out what to do after reducing with each rule, depending on | | |
540 | | the saved state from before the beginning of parsing the data that | | |
541 | | matched this rule. | | |
542 | | | | |
543 | | The YYDEFGOTO table is output now. The detailed info is saved for | | |
544 | | putting into YYTABLE later. | | |
545 | `-------------------------------------------------------------------*/ | |
546 | ||
547 | static void | |
548 | goto_actions (void) | |
549 | { | |
550 | symbol_number i; | |
551 | short *state_count = CALLOC (state_count, nstates); | |
552 | MALLOC (yydefgoto, nvars); | |
553 | ||
554 | /* For a given nterm I, STATE_COUNT[S] is the number of times there | |
555 | is a GOTO to S on I. */ | |
556 | for (i = ntokens; i < nsyms; ++i) | |
557 | { | |
558 | state_number default_state = default_goto (i, state_count); | |
559 | save_column (i, default_state); | |
560 | yydefgoto[i - ntokens] = default_state; | |
561 | } | |
562 | free (state_count); | |
563 | } | |
564 | ||
565 | ||
566 | /*------------------------------------------------------------------. | |
567 | | Compute ORDER, a reordering of vectors, in order to decide how to | | |
568 | | pack the actions and gotos information into yytable. | | |
569 | `------------------------------------------------------------------*/ | |
570 | ||
571 | static void | |
572 | sort_actions (void) | |
573 | { | |
574 | int i; | |
575 | ||
576 | nentries = 0; | |
577 | ||
578 | for (i = 0; i < nvectors; i++) | |
579 | if (tally[i] > 0) | |
580 | { | |
581 | int k; | |
582 | int t = tally[i]; | |
583 | int w = width[i]; | |
584 | int j = nentries - 1; | |
585 | ||
586 | while (j >= 0 && (width[order[j]] < w)) | |
587 | j--; | |
588 | ||
589 | while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t)) | |
590 | j--; | |
591 | ||
592 | for (k = nentries - 1; k > j; k--) | |
593 | order[k + 1] = order[k]; | |
594 | ||
595 | order[j + 1] = i; | |
596 | nentries++; | |
597 | } | |
598 | } | |
599 | ||
600 | ||
601 | /* If VECTOR is a state which actions (reflected by FROMS, TOS, TALLY | |
602 | and WIDTH of VECTOR) are common to a previous state, return this | |
603 | state number. | |
604 | ||
605 | In any other case, return -1. */ | |
606 | ||
607 | static state_number | |
608 | matching_state (vector_number vector) | |
609 | { | |
610 | vector_number i = order[vector]; | |
611 | int t; | |
612 | int w; | |
613 | int prev; | |
614 | ||
615 | /* If VECTOR is a nterm, return -1. */ | |
616 | if (nstates <= i) | |
617 | return -1; | |
618 | ||
619 | t = tally[i]; | |
620 | w = width[i]; | |
621 | ||
622 | /* If VECTOR has GLR conflicts, return -1 */ | |
623 | if (conflict_tos[i] != NULL) | |
624 | { | |
625 | int j; | |
626 | for (j = 0; j < t; j += 1) | |
627 | if (conflict_tos[i][j] != 0) | |
628 | return -1; | |
629 | } | |
630 | ||
631 | for (prev = vector - 1; prev >= 0; prev--) | |
632 | { | |
633 | vector_number j = order[prev]; | |
634 | int k; | |
635 | int match = 1; | |
636 | ||
637 | /* Given how ORDER was computed, if the WIDTH or TALLY is | |
638 | different, there cannot be a matching state. */ | |
639 | if (width[j] != w || tally[j] != t) | |
640 | return -1; | |
641 | ||
642 | for (k = 0; match && k < t; k++) | |
643 | if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k] | |
644 | || (conflict_tos[j] != NULL && conflict_tos[j][k] != 0)) | |
645 | match = 0; | |
646 | ||
647 | if (match) | |
648 | return j; | |
649 | } | |
650 | ||
651 | return -1; | |
652 | } | |
653 | ||
654 | ||
655 | static base_number | |
656 | pack_vector (vector_number vector) | |
657 | { | |
658 | vector_number i = order[vector]; | |
659 | int j; | |
660 | int t = tally[i]; | |
661 | int loc = 0; | |
662 | base_number *from = froms[i]; | |
663 | base_number *to = tos[i]; | |
664 | unsigned int *conflict_to = conflict_tos[i]; | |
665 | ||
666 | if (! t) | |
667 | abort (); | |
668 | ||
669 | for (j = lowzero - from[0]; ; j++) | |
670 | { | |
671 | int k; | |
672 | int ok = 1; | |
673 | ||
674 | if (table_size <= j) | |
675 | abort (); | |
676 | ||
677 | for (k = 0; ok && k < t; k++) | |
678 | { | |
679 | loc = j + state_number_as_int (from[k]); | |
680 | if (table_size <= loc) | |
681 | table_grow (loc); | |
682 | ||
683 | if (table[loc] != 0) | |
684 | ok = 0; | |
685 | } | |
686 | ||
687 | for (k = 0; ok && k < vector; k++) | |
688 | if (pos[k] == j) | |
689 | ok = 0; | |
690 | ||
691 | if (ok) | |
692 | { | |
693 | for (k = 0; k < t; k++) | |
694 | { | |
695 | loc = j + from[k]; | |
696 | table[loc] = to[k]; | |
697 | if (glr_parser && conflict_to != NULL) | |
698 | conflict_table[loc] = conflict_to[k]; | |
699 | check[loc] = from[k]; | |
700 | } | |
701 | ||
702 | while (table[lowzero] != 0) | |
703 | lowzero++; | |
704 | ||
705 | if (loc > high) | |
706 | high = loc; | |
707 | ||
708 | if (! (BASE_MINIMUM <= j && j <= BASE_MAXIMUM)) | |
709 | abort (); | |
710 | return j; | |
711 | } | |
712 | } | |
713 | } | |
714 | ||
715 | ||
716 | /*-------------------------------------------------------------. | |
717 | | Remap the negative infinite in TAB from NINF to the greatest | | |
718 | | possible smallest value. Return it. | | |
719 | | | | |
720 | | In most case this allows us to use shorts instead of ints in | | |
721 | | parsers. | | |
722 | `-------------------------------------------------------------*/ | |
723 | ||
724 | static base_number | |
725 | table_ninf_remap (base_number tab[], int size, base_number ninf) | |
726 | { | |
727 | base_number res = 0; | |
728 | int i; | |
729 | ||
730 | for (i = 0; i < size; i++) | |
731 | if (tab[i] < res && tab[i] != ninf) | |
732 | res = tab[i]; | |
733 | ||
734 | --res; | |
735 | ||
736 | for (i = 0; i < size; i++) | |
737 | if (tab[i] == ninf) | |
738 | tab[i] = res; | |
739 | ||
740 | return res; | |
741 | } | |
742 | ||
743 | static void | |
744 | pack_table (void) | |
745 | { | |
746 | int i; | |
747 | ||
748 | CALLOC (base, nvectors); | |
749 | CALLOC (pos, nentries); | |
750 | CALLOC (table, table_size); | |
751 | CALLOC (conflict_table, table_size); | |
752 | CALLOC (check, table_size); | |
753 | ||
754 | lowzero = 0; | |
755 | high = 0; | |
756 | ||
757 | for (i = 0; i < nvectors; i++) | |
758 | base[i] = BASE_MINIMUM; | |
759 | ||
760 | for (i = 0; i < table_size; i++) | |
761 | check[i] = -1; | |
762 | ||
763 | for (i = 0; i < nentries; i++) | |
764 | { | |
765 | state_number s = matching_state (i); | |
766 | base_number place; | |
767 | ||
768 | if (s < 0) | |
769 | /* A new set of state actions, or a nonterminal. */ | |
770 | place = pack_vector (i); | |
771 | else | |
772 | /* Action of I were already coded for S. */ | |
773 | place = base[s]; | |
774 | ||
775 | pos[i] = place; | |
776 | base[order[i]] = place; | |
777 | } | |
778 | ||
779 | /* Use the greatest possible negative infinites. */ | |
780 | base_ninf = table_ninf_remap (base, nvectors, BASE_MINIMUM); | |
781 | table_ninf = table_ninf_remap (table, high + 1, ACTION_NUMBER_MINIMUM); | |
782 | ||
783 | free (pos); | |
784 | } | |
785 | ||
786 | \f | |
787 | ||
788 | /*-----------------------------------------------------------------. | |
789 | | Compute and output yydefact, yydefgoto, yypact, yypgoto, yytable | | |
790 | | and yycheck. | | |
791 | `-----------------------------------------------------------------*/ | |
792 | ||
793 | void | |
794 | tables_generate (void) | |
795 | { | |
796 | int i; | |
797 | ||
798 | /* This is a poor way to make sure the sizes are properly | |
799 | correlated. In particular the signedness is not taken into | |
800 | account. But it's not useless. */ | |
801 | verify (sizes_are_properly_correlated, | |
802 | (sizeof nstates <= sizeof nvectors | |
803 | && sizeof nvars <= sizeof nvectors)); | |
804 | ||
805 | nvectors = state_number_as_int (nstates) + nvars; | |
806 | ||
807 | CALLOC (froms, nvectors); | |
808 | CALLOC (tos, nvectors); | |
809 | CALLOC (conflict_tos, nvectors); | |
810 | CALLOC (tally, nvectors); | |
811 | CALLOC (width, nvectors); | |
812 | ||
813 | token_actions (); | |
814 | ||
815 | goto_actions (); | |
816 | free (goto_map); | |
817 | free (from_state); | |
818 | free (to_state); | |
819 | ||
820 | CALLOC (order, nvectors); | |
821 | sort_actions (); | |
822 | pack_table (); | |
823 | free (order); | |
824 | ||
825 | free (tally); | |
826 | free (width); | |
827 | ||
828 | for (i = 0; i < nvectors; i++) | |
829 | { | |
830 | free (froms[i]); | |
831 | free (tos[i]); | |
832 | XFREE (conflict_tos[i]); | |
833 | } | |
834 | ||
835 | free (froms); | |
836 | free (tos); | |
837 | free (conflict_tos); | |
838 | } | |
839 | ||
840 | ||
841 | /*-------------------------. | |
842 | | Free the parser tables. | | |
843 | `-------------------------*/ | |
844 | ||
845 | void | |
846 | tables_free (void) | |
847 | { | |
848 | free (base); | |
849 | free (conflict_table); | |
850 | free (conflict_list); | |
851 | free (table); | |
852 | free (check); | |
853 | free (yydefgoto); | |
854 | free (yydefact); | |
855 | } |