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