OLD | NEW |
(Empty) | |
| 1 /* Symbol table manager for Bison. |
| 2 |
| 3 Copyright (C) 1984, 1989, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2008 |
| 4 Free Software Foundation, Inc. |
| 5 |
| 6 This file is part of Bison, the GNU Compiler Compiler. |
| 7 |
| 8 This program is free software: you can redistribute it and/or modify |
| 9 it under the terms of the GNU General Public License as published by |
| 10 the Free Software Foundation, either version 3 of the License, or |
| 11 (at your option) any later version. |
| 12 |
| 13 This program is distributed in the hope that it will be useful, |
| 14 but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 GNU General Public License for more details. |
| 17 |
| 18 You should have received a copy of the GNU General Public License |
| 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 20 |
| 21 #include <config.h> |
| 22 #include "system.h" |
| 23 |
| 24 #include <hash.h> |
| 25 #include <quotearg.h> |
| 26 |
| 27 #include "complain.h" |
| 28 #include "gram.h" |
| 29 #include "symtab.h" |
| 30 |
| 31 /*------------------------. |
| 32 | Distinguished symbols. | |
| 33 `------------------------*/ |
| 34 |
| 35 symbol *errtoken = NULL; |
| 36 symbol *undeftoken = NULL; |
| 37 symbol *endtoken = NULL; |
| 38 symbol *accept = NULL; |
| 39 symbol *startsymbol = NULL; |
| 40 location startsymbol_location; |
| 41 |
| 42 /*---------------------------------------. |
| 43 | Default %destructor's and %printer's. | |
| 44 `---------------------------------------*/ |
| 45 |
| 46 static code_props default_tagged_destructor = CODE_PROPS_NONE_INIT; |
| 47 static code_props default_tagless_destructor = CODE_PROPS_NONE_INIT; |
| 48 static code_props default_tagged_printer = CODE_PROPS_NONE_INIT; |
| 49 static code_props default_tagless_printer = CODE_PROPS_NONE_INIT; |
| 50 |
| 51 /*---------------------------------. |
| 52 | Create a new symbol, named TAG. | |
| 53 `---------------------------------*/ |
| 54 |
| 55 static symbol * |
| 56 symbol_new (uniqstr tag, location loc) |
| 57 { |
| 58 symbol *res = xmalloc (sizeof *res); |
| 59 |
| 60 uniqstr_assert (tag); |
| 61 res->tag = tag; |
| 62 res->location = loc; |
| 63 |
| 64 res->type_name = NULL; |
| 65 code_props_none_init (&res->destructor); |
| 66 code_props_none_init (&res->printer); |
| 67 |
| 68 res->number = NUMBER_UNDEFINED; |
| 69 res->prec = 0; |
| 70 res->assoc = undef_assoc; |
| 71 res->user_token_number = USER_NUMBER_UNDEFINED; |
| 72 |
| 73 res->alias = NULL; |
| 74 res->class = unknown_sym; |
| 75 res->declared = false; |
| 76 |
| 77 if (nsyms == SYMBOL_NUMBER_MAXIMUM) |
| 78 fatal (_("too many symbols in input grammar (limit is %d)"), |
| 79 SYMBOL_NUMBER_MAXIMUM); |
| 80 nsyms++; |
| 81 return res; |
| 82 } |
| 83 |
| 84 /*----------------------------------------. |
| 85 | Create a new semantic type, named TAG. | |
| 86 `----------------------------------------*/ |
| 87 |
| 88 static semantic_type * |
| 89 semantic_type_new (uniqstr tag) |
| 90 { |
| 91 semantic_type *res = xmalloc (sizeof *res); |
| 92 |
| 93 uniqstr_assert (tag); |
| 94 res->tag = tag; |
| 95 code_props_none_init (&res->destructor); |
| 96 code_props_none_init (&res->printer); |
| 97 |
| 98 return res; |
| 99 } |
| 100 |
| 101 |
| 102 /*-----------------. |
| 103 | Print a symbol. | |
| 104 `-----------------*/ |
| 105 |
| 106 #define SYMBOL_ATTR_PRINT(Attr) \ |
| 107 if (s->Attr) \ |
| 108 fprintf (f, " %s { %s }", #Attr, s->Attr) |
| 109 |
| 110 #define SYMBOL_CODE_PRINT(Attr) \ |
| 111 if (s->Attr.code) \ |
| 112 fprintf (f, " %s { %s }", #Attr, s->Attr.code) |
| 113 |
| 114 void |
| 115 symbol_print (symbol *s, FILE *f) |
| 116 { |
| 117 if (s) |
| 118 { |
| 119 fprintf (f, "\"%s\"", s->tag); |
| 120 SYMBOL_ATTR_PRINT (type_name); |
| 121 SYMBOL_CODE_PRINT (destructor); |
| 122 SYMBOL_CODE_PRINT (printer); |
| 123 } |
| 124 else |
| 125 fprintf (f, "<NULL>"); |
| 126 } |
| 127 |
| 128 #undef SYMBOL_ATTR_PRINT |
| 129 #undef SYMBOL_CODE_PRINT |
| 130 |
| 131 /*------------------------------------------------------------------. |
| 132 | Complain that S's WHAT is redeclared at SECOND, and was first set | |
| 133 | at FIRST. | |
| 134 `------------------------------------------------------------------*/ |
| 135 |
| 136 static void |
| 137 symbol_redeclaration (symbol *s, const char *what, location first, |
| 138 location second) |
| 139 { |
| 140 complain_at (second, _("%s redeclaration for %s"), what, s->tag); |
| 141 complain_at (first, _("previous declaration")); |
| 142 } |
| 143 |
| 144 static void |
| 145 semantic_type_redeclaration (semantic_type *s, const char *what, location first, |
| 146 location second) |
| 147 { |
| 148 complain_at (second, _("%s redeclaration for <%s>"), what, s->tag); |
| 149 complain_at (first, _("previous declaration")); |
| 150 } |
| 151 |
| 152 |
| 153 /*-----------------------------------------------------------------. |
| 154 | Set the TYPE_NAME associated with SYM. Does nothing if passed 0 | |
| 155 | as TYPE_NAME. | |
| 156 `-----------------------------------------------------------------*/ |
| 157 |
| 158 void |
| 159 symbol_type_set (symbol *sym, uniqstr type_name, location loc) |
| 160 { |
| 161 if (type_name) |
| 162 { |
| 163 if (sym->type_name) |
| 164 symbol_redeclaration (sym, "%type", sym->type_location, loc); |
| 165 uniqstr_assert (type_name); |
| 166 sym->type_name = type_name; |
| 167 sym->type_location = loc; |
| 168 } |
| 169 } |
| 170 |
| 171 /*-----------------------------------. |
| 172 | Get the CLASS associated with SYM. | |
| 173 `-----------------------------------*/ |
| 174 |
| 175 const char * |
| 176 symbol_class_get_string (symbol *sym) |
| 177 { |
| 178 if (sym->class) |
| 179 { |
| 180 if (sym->class == token_sym) |
| 181 return "terminal"; |
| 182 else if (sym->class == nterm_sym) |
| 183 return "nonterminal"; |
| 184 } |
| 185 return "unknown"; |
| 186 } |
| 187 |
| 188 |
| 189 /*-----------------------------------------. |
| 190 | Set the DESTRUCTOR associated with SYM. | |
| 191 `-----------------------------------------*/ |
| 192 |
| 193 void |
| 194 symbol_destructor_set (symbol *sym, code_props const *destructor) |
| 195 { |
| 196 if (sym->destructor.code) |
| 197 symbol_redeclaration (sym, "%destructor", sym->destructor.location, |
| 198 destructor->location); |
| 199 sym->destructor = *destructor; |
| 200 } |
| 201 |
| 202 /*------------------------------------------. |
| 203 | Set the DESTRUCTOR associated with TYPE. | |
| 204 `------------------------------------------*/ |
| 205 |
| 206 void |
| 207 semantic_type_destructor_set (semantic_type *type, |
| 208 code_props const *destructor) |
| 209 { |
| 210 if (type->destructor.code) |
| 211 semantic_type_redeclaration (type, "%destructor", |
| 212 type->destructor.location, |
| 213 destructor->location); |
| 214 type->destructor = *destructor; |
| 215 } |
| 216 |
| 217 /*---------------------------------------. |
| 218 | Get the computed %destructor for SYM. | |
| 219 `---------------------------------------*/ |
| 220 |
| 221 code_props const * |
| 222 symbol_destructor_get (symbol const *sym) |
| 223 { |
| 224 /* Per-symbol %destructor. */ |
| 225 if (sym->destructor.code) |
| 226 return &sym->destructor; |
| 227 |
| 228 /* Per-type %destructor. */ |
| 229 if (sym->type_name) |
| 230 { |
| 231 code_props const *destructor = |
| 232 &semantic_type_get (sym->type_name)->destructor; |
| 233 if (destructor->code) |
| 234 return destructor; |
| 235 } |
| 236 |
| 237 /* Apply default %destructor's only to user-defined symbols. */ |
| 238 if (sym->tag[0] == '$' || sym == errtoken) |
| 239 return &code_props_none; |
| 240 |
| 241 if (sym->type_name) |
| 242 return &default_tagged_destructor; |
| 243 return &default_tagless_destructor; |
| 244 } |
| 245 |
| 246 /*--------------------------------------. |
| 247 | Set the PRINTER associated with SYM. | |
| 248 `--------------------------------------*/ |
| 249 |
| 250 void |
| 251 symbol_printer_set (symbol *sym, code_props const *printer) |
| 252 { |
| 253 if (sym->printer.code) |
| 254 symbol_redeclaration (sym, "%printer", |
| 255 sym->printer.location, printer->location); |
| 256 sym->printer = *printer; |
| 257 } |
| 258 |
| 259 /*---------------------------------------. |
| 260 | Set the PRINTER associated with TYPE. | |
| 261 `---------------------------------------*/ |
| 262 |
| 263 void |
| 264 semantic_type_printer_set (semantic_type *type, code_props const *printer) |
| 265 { |
| 266 if (type->printer.code) |
| 267 semantic_type_redeclaration (type, "%printer", |
| 268 type->printer.location, printer->location); |
| 269 type->printer = *printer; |
| 270 } |
| 271 |
| 272 /*------------------------------------. |
| 273 | Get the computed %printer for SYM. | |
| 274 `------------------------------------*/ |
| 275 |
| 276 code_props const * |
| 277 symbol_printer_get (symbol const *sym) |
| 278 { |
| 279 /* Per-symbol %printer. */ |
| 280 if (sym->printer.code) |
| 281 return &sym->printer; |
| 282 |
| 283 /* Per-type %printer. */ |
| 284 if (sym->type_name) |
| 285 { |
| 286 code_props const *printer = &semantic_type_get (sym->type_name)->printer; |
| 287 if (printer->code) |
| 288 return printer; |
| 289 } |
| 290 |
| 291 /* Apply the default %printer only to user-defined symbols. */ |
| 292 if (sym->tag[0] == '$' || sym == errtoken) |
| 293 return &code_props_none; |
| 294 |
| 295 if (sym->type_name) |
| 296 return &default_tagged_printer; |
| 297 return &default_tagless_printer; |
| 298 } |
| 299 |
| 300 /*-----------------------------------------------------------------. |
| 301 | Set the PRECEDENCE associated with SYM. Does nothing if invoked | |
| 302 | with UNDEF_ASSOC as ASSOC. | |
| 303 `-----------------------------------------------------------------*/ |
| 304 |
| 305 void |
| 306 symbol_precedence_set (symbol *sym, int prec, assoc a, location loc) |
| 307 { |
| 308 if (a != undef_assoc) |
| 309 { |
| 310 if (sym->prec != 0) |
| 311 symbol_redeclaration (sym, assoc_to_string (a), sym->prec_location, |
| 312 loc); |
| 313 sym->prec = prec; |
| 314 sym->assoc = a; |
| 315 sym->prec_location = loc; |
| 316 } |
| 317 |
| 318 /* Only terminals have a precedence. */ |
| 319 symbol_class_set (sym, token_sym, loc, false); |
| 320 } |
| 321 |
| 322 |
| 323 /*------------------------------------. |
| 324 | Set the CLASS associated with SYM. | |
| 325 `------------------------------------*/ |
| 326 |
| 327 void |
| 328 symbol_class_set (symbol *sym, symbol_class class, location loc, bool declaring) |
| 329 { |
| 330 if (sym->class != unknown_sym && sym->class != class) |
| 331 { |
| 332 complain_at (loc, _("symbol %s redefined"), sym->tag); |
| 333 sym->declared = false; |
| 334 } |
| 335 |
| 336 if (class == nterm_sym && sym->class != nterm_sym) |
| 337 sym->number = nvars++; |
| 338 else if (class == token_sym && sym->number == NUMBER_UNDEFINED) |
| 339 sym->number = ntokens++; |
| 340 |
| 341 sym->class = class; |
| 342 |
| 343 if (declaring) |
| 344 { |
| 345 if (sym->declared) |
| 346 warn_at (loc, _("symbol %s redeclared"), sym->tag); |
| 347 sym->declared = true; |
| 348 } |
| 349 } |
| 350 |
| 351 |
| 352 /*------------------------------------------------. |
| 353 | Set the USER_TOKEN_NUMBER associated with SYM. | |
| 354 `------------------------------------------------*/ |
| 355 |
| 356 void |
| 357 symbol_user_token_number_set (symbol *sym, int user_token_number, location loc) |
| 358 { |
| 359 int *user_token_numberp; |
| 360 |
| 361 if (sym->user_token_number != USER_NUMBER_ALIAS) |
| 362 user_token_numberp = &sym->user_token_number; |
| 363 else |
| 364 user_token_numberp = &sym->alias->user_token_number; |
| 365 if (*user_token_numberp != USER_NUMBER_UNDEFINED |
| 366 && *user_token_numberp != user_token_number) |
| 367 complain_at (loc, _("redefining user token number of %s"), sym->tag); |
| 368 |
| 369 *user_token_numberp = user_token_number; |
| 370 /* User defined $end token? */ |
| 371 if (user_token_number == 0) |
| 372 { |
| 373 endtoken = sym; |
| 374 endtoken->number = 0; |
| 375 /* It is always mapped to 0, so it was already counted in |
| 376 NTOKENS. */ |
| 377 --ntokens; |
| 378 } |
| 379 } |
| 380 |
| 381 |
| 382 /*----------------------------------------------------------. |
| 383 | If SYM is not defined, report an error, and consider it a | |
| 384 | nonterminal. | |
| 385 `----------------------------------------------------------*/ |
| 386 |
| 387 static inline bool |
| 388 symbol_check_defined (symbol *sym) |
| 389 { |
| 390 if (sym->class == unknown_sym) |
| 391 { |
| 392 complain_at |
| 393 (sym->location, |
| 394 _("symbol %s is used, but is not defined as a token and has no rules"), |
| 395 sym->tag); |
| 396 sym->class = nterm_sym; |
| 397 sym->number = nvars++; |
| 398 } |
| 399 |
| 400 return true; |
| 401 } |
| 402 |
| 403 static bool |
| 404 symbol_check_defined_processor (void *sym, void *null ATTRIBUTE_UNUSED) |
| 405 { |
| 406 return symbol_check_defined (sym); |
| 407 } |
| 408 |
| 409 |
| 410 /*------------------------------------------------------------------. |
| 411 | Declare the new symbol SYM. Make it an alias of SYMVAL, and type | |
| 412 | SYMVAL with SYM's type. | |
| 413 `------------------------------------------------------------------*/ |
| 414 |
| 415 void |
| 416 symbol_make_alias (symbol *sym, symbol *symval, location loc) |
| 417 { |
| 418 if (symval->alias) |
| 419 warn_at (loc, _("symbol `%s' used more than once as a literal string"), |
| 420 symval->tag); |
| 421 else if (sym->alias) |
| 422 warn_at (loc, _("symbol `%s' given more than one literal string"), |
| 423 sym->tag); |
| 424 else |
| 425 { |
| 426 symval->class = token_sym; |
| 427 symval->user_token_number = sym->user_token_number; |
| 428 sym->user_token_number = USER_NUMBER_ALIAS; |
| 429 symval->alias = sym; |
| 430 sym->alias = symval; |
| 431 symval->number = sym->number; |
| 432 symbol_type_set (symval, sym->type_name, loc); |
| 433 } |
| 434 } |
| 435 |
| 436 |
| 437 /*---------------------------------------------------------. |
| 438 | Check that THIS, and its alias, have same precedence and | |
| 439 | associativity. | |
| 440 `---------------------------------------------------------*/ |
| 441 |
| 442 static inline void |
| 443 symbol_check_alias_consistency (symbol *this) |
| 444 { |
| 445 symbol *alias = this; |
| 446 symbol *orig = this->alias; |
| 447 |
| 448 /* Check only those that _are_ the aliases. */ |
| 449 if (!(this->alias && this->user_token_number == USER_NUMBER_ALIAS)) |
| 450 return; |
| 451 |
| 452 if (orig->type_name != alias->type_name) |
| 453 { |
| 454 if (orig->type_name) |
| 455 symbol_type_set (alias, orig->type_name, orig->type_location); |
| 456 else |
| 457 symbol_type_set (orig, alias->type_name, alias->type_location); |
| 458 } |
| 459 |
| 460 |
| 461 if (orig->destructor.code || alias->destructor.code) |
| 462 { |
| 463 if (orig->destructor.code) |
| 464 symbol_destructor_set (alias, &orig->destructor); |
| 465 else |
| 466 symbol_destructor_set (orig, &alias->destructor); |
| 467 } |
| 468 |
| 469 if (orig->printer.code || alias->printer.code) |
| 470 { |
| 471 if (orig->printer.code) |
| 472 symbol_printer_set (alias, &orig->printer); |
| 473 else |
| 474 symbol_printer_set (orig, &alias->printer); |
| 475 } |
| 476 |
| 477 if (alias->prec || orig->prec) |
| 478 { |
| 479 if (orig->prec) |
| 480 symbol_precedence_set (alias, orig->prec, orig->assoc, |
| 481 orig->prec_location); |
| 482 else |
| 483 symbol_precedence_set (orig, alias->prec, alias->assoc, |
| 484 alias->prec_location); |
| 485 } |
| 486 } |
| 487 |
| 488 static bool |
| 489 symbol_check_alias_consistency_processor (void *this, |
| 490 void *null ATTRIBUTE_UNUSED) |
| 491 { |
| 492 symbol_check_alias_consistency (this); |
| 493 return true; |
| 494 } |
| 495 |
| 496 |
| 497 /*-------------------------------------------------------------------. |
| 498 | Assign a symbol number, and write the definition of the token name | |
| 499 | into FDEFINES. Put in SYMBOLS. | |
| 500 `-------------------------------------------------------------------*/ |
| 501 |
| 502 static inline bool |
| 503 symbol_pack (symbol *this) |
| 504 { |
| 505 if (this->class == nterm_sym) |
| 506 { |
| 507 this->number += ntokens; |
| 508 } |
| 509 else if (this->alias) |
| 510 { |
| 511 /* This symbol and its alias are a single token defn. |
| 512 Allocate a tokno, and assign to both check agreement of |
| 513 prec and assoc fields and make both the same */ |
| 514 if (this->number == NUMBER_UNDEFINED) |
| 515 { |
| 516 if (this == endtoken || this->alias == endtoken) |
| 517 this->number = this->alias->number = 0; |
| 518 else |
| 519 { |
| 520 aver (this->alias->number != NUMBER_UNDEFINED); |
| 521 this->number = this->alias->number; |
| 522 } |
| 523 } |
| 524 /* Do not do processing below for USER_NUMBER_ALIASes. */ |
| 525 if (this->user_token_number == USER_NUMBER_ALIAS) |
| 526 return true; |
| 527 } |
| 528 else /* this->class == token_sym */ |
| 529 aver (this->number != NUMBER_UNDEFINED); |
| 530 |
| 531 symbols[this->number] = this; |
| 532 return true; |
| 533 } |
| 534 |
| 535 static bool |
| 536 symbol_pack_processor (void *this, void *null ATTRIBUTE_UNUSED) |
| 537 { |
| 538 return symbol_pack (this); |
| 539 } |
| 540 |
| 541 |
| 542 |
| 543 |
| 544 /*--------------------------------------------------. |
| 545 | Put THIS in TOKEN_TRANSLATIONS if it is a token. | |
| 546 `--------------------------------------------------*/ |
| 547 |
| 548 static inline bool |
| 549 symbol_translation (symbol *this) |
| 550 { |
| 551 /* Non-terminal? */ |
| 552 if (this->class == token_sym |
| 553 && this->user_token_number != USER_NUMBER_ALIAS) |
| 554 { |
| 555 /* A token which translation has already been set? */ |
| 556 if (token_translations[this->user_token_number] != undeftoken->number) |
| 557 complain_at (this->location, |
| 558 _("tokens %s and %s both assigned number %d"), |
| 559 symbols[token_translations[this->user_token_number]]->tag, |
| 560 this->tag, this->user_token_number); |
| 561 |
| 562 token_translations[this->user_token_number] = this->number; |
| 563 } |
| 564 |
| 565 return true; |
| 566 } |
| 567 |
| 568 static bool |
| 569 symbol_translation_processor (void *this, void *null ATTRIBUTE_UNUSED) |
| 570 { |
| 571 return symbol_translation (this); |
| 572 } |
| 573 |
| 574 |
| 575 /*---------------------------------------. |
| 576 | Symbol and semantic type hash tables. | |
| 577 `---------------------------------------*/ |
| 578 |
| 579 /* Initial capacity of symbol and semantic type hash table. */ |
| 580 #define HT_INITIAL_CAPACITY 257 |
| 581 |
| 582 static struct hash_table *symbol_table = NULL; |
| 583 static struct hash_table *semantic_type_table = NULL; |
| 584 |
| 585 static inline bool |
| 586 hash_compare_symbol (const symbol *m1, const symbol *m2) |
| 587 { |
| 588 /* Since tags are unique, we can compare the pointers themselves. */ |
| 589 return UNIQSTR_EQ (m1->tag, m2->tag); |
| 590 } |
| 591 |
| 592 static inline bool |
| 593 hash_compare_semantic_type (const semantic_type *m1, const semantic_type *m2) |
| 594 { |
| 595 /* Since names are unique, we can compare the pointers themselves. */ |
| 596 return UNIQSTR_EQ (m1->tag, m2->tag); |
| 597 } |
| 598 |
| 599 static bool |
| 600 hash_symbol_comparator (void const *m1, void const *m2) |
| 601 { |
| 602 return hash_compare_symbol (m1, m2); |
| 603 } |
| 604 |
| 605 static bool |
| 606 hash_semantic_type_comparator (void const *m1, void const *m2) |
| 607 { |
| 608 return hash_compare_semantic_type (m1, m2); |
| 609 } |
| 610 |
| 611 static inline size_t |
| 612 hash_symbol (const symbol *m, size_t tablesize) |
| 613 { |
| 614 /* Since tags are unique, we can hash the pointer itself. */ |
| 615 return ((uintptr_t) m->tag) % tablesize; |
| 616 } |
| 617 |
| 618 static inline size_t |
| 619 hash_semantic_type (const semantic_type *m, size_t tablesize) |
| 620 { |
| 621 /* Since names are unique, we can hash the pointer itself. */ |
| 622 return ((uintptr_t) m->tag) % tablesize; |
| 623 } |
| 624 |
| 625 static size_t |
| 626 hash_symbol_hasher (void const *m, size_t tablesize) |
| 627 { |
| 628 return hash_symbol (m, tablesize); |
| 629 } |
| 630 |
| 631 static size_t |
| 632 hash_semantic_type_hasher (void const *m, size_t tablesize) |
| 633 { |
| 634 return hash_semantic_type (m, tablesize); |
| 635 } |
| 636 |
| 637 /*-------------------------------. |
| 638 | Create the symbol hash table. | |
| 639 `-------------------------------*/ |
| 640 |
| 641 void |
| 642 symbols_new (void) |
| 643 { |
| 644 symbol_table = hash_initialize (HT_INITIAL_CAPACITY, |
| 645 NULL, |
| 646 hash_symbol_hasher, |
| 647 hash_symbol_comparator, |
| 648 free); |
| 649 semantic_type_table = hash_initialize (HT_INITIAL_CAPACITY, |
| 650 NULL, |
| 651 hash_semantic_type_hasher, |
| 652 hash_semantic_type_comparator, |
| 653 free); |
| 654 } |
| 655 |
| 656 |
| 657 /*----------------------------------------------------------------. |
| 658 | Find the symbol named KEY, and return it. If it does not exist | |
| 659 | yet, create it. | |
| 660 `----------------------------------------------------------------*/ |
| 661 |
| 662 symbol * |
| 663 symbol_from_uniqstr (const uniqstr key, location loc) |
| 664 { |
| 665 symbol probe; |
| 666 symbol *entry; |
| 667 |
| 668 probe.tag = key; |
| 669 entry = hash_lookup (symbol_table, &probe); |
| 670 |
| 671 if (!entry) |
| 672 { |
| 673 /* First insertion in the hash. */ |
| 674 entry = symbol_new (key, loc); |
| 675 hash_insert (symbol_table, entry); |
| 676 } |
| 677 return entry; |
| 678 } |
| 679 |
| 680 |
| 681 /*-----------------------------------------------------------------------. |
| 682 | Find the semantic type named KEY, and return it. If it does not exist | |
| 683 | yet, create it. | |
| 684 `-----------------------------------------------------------------------*/ |
| 685 |
| 686 semantic_type * |
| 687 semantic_type_from_uniqstr (const uniqstr key) |
| 688 { |
| 689 semantic_type probe; |
| 690 semantic_type *entry; |
| 691 |
| 692 probe.tag = key; |
| 693 entry = hash_lookup (semantic_type_table, &probe); |
| 694 |
| 695 if (!entry) |
| 696 { |
| 697 /* First insertion in the hash. */ |
| 698 entry = semantic_type_new (key); |
| 699 hash_insert (semantic_type_table, entry); |
| 700 } |
| 701 return entry; |
| 702 } |
| 703 |
| 704 |
| 705 /*----------------------------------------------------------------. |
| 706 | Find the symbol named KEY, and return it. If it does not exist | |
| 707 | yet, create it. | |
| 708 `----------------------------------------------------------------*/ |
| 709 |
| 710 symbol * |
| 711 symbol_get (const char *key, location loc) |
| 712 { |
| 713 return symbol_from_uniqstr (uniqstr_new (key), loc); |
| 714 } |
| 715 |
| 716 |
| 717 /*-----------------------------------------------------------------------. |
| 718 | Find the semantic type named KEY, and return it. If it does not exist | |
| 719 | yet, create it. | |
| 720 `-----------------------------------------------------------------------*/ |
| 721 |
| 722 semantic_type * |
| 723 semantic_type_get (const char *key) |
| 724 { |
| 725 return semantic_type_from_uniqstr (uniqstr_new (key)); |
| 726 } |
| 727 |
| 728 |
| 729 /*------------------------------------------------------------------. |
| 730 | Generate a dummy nonterminal, whose name cannot conflict with the | |
| 731 | user's names. | |
| 732 `------------------------------------------------------------------*/ |
| 733 |
| 734 symbol * |
| 735 dummy_symbol_get (location loc) |
| 736 { |
| 737 /* Incremented for each generated symbol. */ |
| 738 static int dummy_count = 0; |
| 739 static char buf[256]; |
| 740 |
| 741 symbol *sym; |
| 742 |
| 743 sprintf (buf, "$@%d", ++dummy_count); |
| 744 sym = symbol_get (buf, loc); |
| 745 sym->class = nterm_sym; |
| 746 sym->number = nvars++; |
| 747 return sym; |
| 748 } |
| 749 |
| 750 bool |
| 751 symbol_is_dummy (const symbol *sym) |
| 752 { |
| 753 return sym->tag[0] == '@' || (sym->tag[0] == '$' && sym->tag[1] == '@'); |
| 754 } |
| 755 |
| 756 /*-------------------. |
| 757 | Free the symbols. | |
| 758 `-------------------*/ |
| 759 |
| 760 void |
| 761 symbols_free (void) |
| 762 { |
| 763 hash_free (symbol_table); |
| 764 hash_free (semantic_type_table); |
| 765 free (symbols); |
| 766 } |
| 767 |
| 768 |
| 769 /*---------------------------------------------------------------. |
| 770 | Look for undefined symbols, report an error, and consider them | |
| 771 | terminals. | |
| 772 `---------------------------------------------------------------*/ |
| 773 |
| 774 static void |
| 775 symbols_do (Hash_processor processor, void *processor_data) |
| 776 { |
| 777 hash_do_for_each (symbol_table, processor, processor_data); |
| 778 } |
| 779 |
| 780 |
| 781 /*--------------------------------------------------------------. |
| 782 | Check that all the symbols are defined. Report any undefined | |
| 783 | symbols and consider them nonterminals. | |
| 784 `--------------------------------------------------------------*/ |
| 785 |
| 786 void |
| 787 symbols_check_defined (void) |
| 788 { |
| 789 symbols_do (symbol_check_defined_processor, NULL); |
| 790 } |
| 791 |
| 792 /*------------------------------------------------------------------. |
| 793 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same | |
| 794 | number. | |
| 795 `------------------------------------------------------------------*/ |
| 796 |
| 797 static void |
| 798 symbols_token_translations_init (void) |
| 799 { |
| 800 bool num_256_available_p = true; |
| 801 int i; |
| 802 |
| 803 /* Find the highest user token number, and whether 256, the POSIX |
| 804 preferred user token number for the error token, is used. */ |
| 805 max_user_token_number = 0; |
| 806 for (i = 0; i < ntokens; ++i) |
| 807 { |
| 808 symbol *this = symbols[i]; |
| 809 if (this->user_token_number != USER_NUMBER_UNDEFINED) |
| 810 { |
| 811 if (this->user_token_number > max_user_token_number) |
| 812 max_user_token_number = this->user_token_number; |
| 813 if (this->user_token_number == 256) |
| 814 num_256_available_p = false; |
| 815 } |
| 816 } |
| 817 |
| 818 /* If 256 is not used, assign it to error, to follow POSIX. */ |
| 819 if (num_256_available_p |
| 820 && errtoken->user_token_number == USER_NUMBER_UNDEFINED) |
| 821 errtoken->user_token_number = 256; |
| 822 |
| 823 /* Set the missing user numbers. */ |
| 824 if (max_user_token_number < 256) |
| 825 max_user_token_number = 256; |
| 826 |
| 827 for (i = 0; i < ntokens; ++i) |
| 828 { |
| 829 symbol *this = symbols[i]; |
| 830 if (this->user_token_number == USER_NUMBER_UNDEFINED) |
| 831 this->user_token_number = ++max_user_token_number; |
| 832 if (this->user_token_number > max_user_token_number) |
| 833 max_user_token_number = this->user_token_number; |
| 834 } |
| 835 |
| 836 token_translations = xnmalloc (max_user_token_number + 1, |
| 837 sizeof *token_translations); |
| 838 |
| 839 /* Initialize all entries for literal tokens to 2, the internal |
| 840 token number for $undefined, which represents all invalid inputs. |
| 841 */ |
| 842 for (i = 0; i < max_user_token_number + 1; i++) |
| 843 token_translations[i] = undeftoken->number; |
| 844 symbols_do (symbol_translation_processor, NULL); |
| 845 } |
| 846 |
| 847 |
| 848 /*----------------------------------------------------------------. |
| 849 | Assign symbol numbers, and write definition of token names into | |
| 850 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. | |
| 851 `----------------------------------------------------------------*/ |
| 852 |
| 853 void |
| 854 symbols_pack (void) |
| 855 { |
| 856 symbols_do (symbol_check_alias_consistency_processor, NULL); |
| 857 |
| 858 symbols = xcalloc (nsyms, sizeof *symbols); |
| 859 symbols_do (symbol_pack_processor, NULL); |
| 860 |
| 861 /* Aliases leave empty slots in symbols, so remove them. */ |
| 862 { |
| 863 int writei; |
| 864 int readi; |
| 865 int nsyms_old = nsyms; |
| 866 for (writei = 0, readi = 0; readi < nsyms_old; readi += 1) |
| 867 { |
| 868 if (symbols[readi] == NULL) |
| 869 { |
| 870 nsyms -= 1; |
| 871 ntokens -= 1; |
| 872 } |
| 873 else |
| 874 { |
| 875 symbols[writei] = symbols[readi]; |
| 876 symbols[writei]->number = writei; |
| 877 if (symbols[writei]->alias) |
| 878 symbols[writei]->alias->number = writei; |
| 879 writei += 1; |
| 880 } |
| 881 } |
| 882 } |
| 883 symbols = xnrealloc (symbols, nsyms, sizeof *symbols); |
| 884 |
| 885 symbols_token_translations_init (); |
| 886 |
| 887 if (startsymbol->class == unknown_sym) |
| 888 fatal_at (startsymbol_location, |
| 889 _("the start symbol %s is undefined"), |
| 890 startsymbol->tag); |
| 891 else if (startsymbol->class == token_sym) |
| 892 fatal_at (startsymbol_location, |
| 893 _("the start symbol %s is a token"), |
| 894 startsymbol->tag); |
| 895 } |
| 896 |
| 897 |
| 898 /*--------------------------------------------------. |
| 899 | Set default tagged/tagless %destructor/%printer. | |
| 900 `--------------------------------------------------*/ |
| 901 |
| 902 void |
| 903 default_tagged_destructor_set (code_props const *destructor) |
| 904 { |
| 905 if (default_tagged_destructor.code) |
| 906 { |
| 907 complain_at (destructor->location, |
| 908 _("redeclaration for default tagged %%destructor")); |
| 909 complain_at (default_tagged_destructor.location, |
| 910 _("previous declaration")); |
| 911 } |
| 912 default_tagged_destructor = *destructor; |
| 913 } |
| 914 |
| 915 void |
| 916 default_tagless_destructor_set (code_props const *destructor) |
| 917 { |
| 918 if (default_tagless_destructor.code) |
| 919 { |
| 920 complain_at (destructor->location, |
| 921 _("redeclaration for default tagless %%destructor")); |
| 922 complain_at (default_tagless_destructor.location, |
| 923 _("previous declaration")); |
| 924 } |
| 925 default_tagless_destructor = *destructor; |
| 926 } |
| 927 |
| 928 void |
| 929 default_tagged_printer_set (code_props const *printer) |
| 930 { |
| 931 if (default_tagged_printer.code) |
| 932 { |
| 933 complain_at (printer->location, |
| 934 _("redeclaration for default tagged %%printer")); |
| 935 complain_at (default_tagged_printer.location, |
| 936 _("previous declaration")); |
| 937 } |
| 938 default_tagged_printer = *printer; |
| 939 } |
| 940 |
| 941 void |
| 942 default_tagless_printer_set (code_props const *printer) |
| 943 { |
| 944 if (default_tagless_printer.code) |
| 945 { |
| 946 complain_at (printer->location, |
| 947 _("redeclaration for default tagless %%printer")); |
| 948 complain_at (default_tagless_printer.location, |
| 949 _("previous declaration")); |
| 950 } |
| 951 default_tagless_printer = *printer; |
| 952 } |
OLD | NEW |