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