1 /* Symbol table manager for Bison,
2 Copyright (C) 1984, 1989, 2000, 2001, 2002 Free Software Foundation, Inc.
4 This file is part of Bison, the GNU Compiler Compiler.
6 Bison is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 Bison is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Bison; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
28 /*------------------------.
29 | Distinguished symbols. |
30 `------------------------*/
32 symbol_t
*errtoken
= NULL
;
33 symbol_t
*undeftoken
= NULL
;
34 symbol_t
*eoftoken
= NULL
;
35 symbol_t
*axiom
= NULL
;
36 symbol_t
*startsymbol
= NULL
;
38 /*---------------------------------.
39 | Create a new symbol, named TAG. |
40 `---------------------------------*/
43 symbol_new (const char *tag
)
45 symbol_t
*res
= XMALLOC (symbol_t
, 1);
47 res
->tag
= xstrdup (tag
);
48 res
->type_name
= NULL
;
49 res
->number
= NUMBER_UNDEFINED
;
51 res
->assoc
= right_assoc
;
52 res
->user_token_number
= USER_NUMBER_UNDEFINED
;
54 res
->class = unknown_sym
;
67 symbol_free (symbol_t
*this)
70 /* This causes crashes because one string can appear more
72 XFREE (this->type_name
);
79 /*-----------------------------------------------------------.
80 | If THIS is not defined, report an error, and consider it a |
82 `-----------------------------------------------------------*/
85 symbol_check_defined (symbol_t
*this)
87 if (this->class == unknown_sym
)
90 (_("symbol %s is used, but is not defined as a token and has no rules"),
92 this->class = nterm_sym
;
93 this->number
= nvars
++;
100 /*-------------------------------------------------------------------.
101 | Declare the new SYMBOL. Make it an alias of SYMVAL, and type them |
103 `-------------------------------------------------------------------*/
106 symbol_make_alias (symbol_t
*symbol
, symbol_t
*symval
, char *typename
)
109 warn (_("symbol `%s' used more than once as a literal string"),
111 else if (symbol
->alias
)
112 warn (_("symbol `%s' given more than one literal string"),
116 symval
->class = token_sym
;
117 symval
->type_name
= typename
;
118 symval
->user_token_number
= symbol
->user_token_number
;
119 symbol
->user_token_number
= USER_NUMBER_ALIAS
;
120 symval
->alias
= symbol
;
121 symbol
->alias
= symval
;
122 /* symbol and symval combined are only one symbol */
125 assert (ntokens
== symbol
->number
|| ntokens
== symval
->number
);
126 symbol
->number
= symval
->number
=
127 (symval
->number
< symbol
->number
) ? symval
->number
: symbol
->number
;
132 /*---------------------------------------------------------.
133 | Check that THIS, and its alias, have same precedence and |
135 `---------------------------------------------------------*/
138 symbol_check_alias_consistence (symbol_t
*this)
140 /* Check only those who _are_ the aliases. */
141 if (this->alias
&& this->user_token_number
== USER_NUMBER_ALIAS
)
143 if (this->prec
!= this->alias
->prec
)
145 if (this->prec
!= 0 && this->alias
->prec
!= 0)
146 complain (_("conflicting precedences for %s and %s"),
147 this->tag
, this->alias
->tag
);
149 this->alias
->prec
= this->prec
;
151 this->prec
= this->alias
->prec
;
154 if (this->assoc
!= this->alias
->assoc
)
156 if (this->assoc
!= 0 && this->alias
->assoc
!= 0)
157 complain (_("conflicting assoc values for %s and %s"),
158 this->tag
, this->alias
->tag
);
159 if (this->assoc
!= 0)
160 this->alias
->assoc
= this->assoc
;
162 this->assoc
= this->alias
->assoc
;
169 /*-------------------------------------------------------------------.
170 | Assign a symbol number, and write the definition of the token name |
171 | into FDEFINES. Put in SYMBOLS. |
172 `-------------------------------------------------------------------*/
175 symbol_pack (symbol_t
*this)
177 if (this->class == nterm_sym
)
179 this->number
+= ntokens
;
181 else if (this->alias
)
183 /* This symbol and its alias are a single token defn.
184 Allocate a tokno, and assign to both check agreement of
185 prec and assoc fields and make both the same */
186 if (this->number
== NUMBER_UNDEFINED
)
188 if (this == eoftoken
|| this->alias
== eoftoken
)
189 this->number
= this->alias
->number
= 0;
192 assert (this->alias
->number
!= NUMBER_UNDEFINED
);
193 this->number
= this->alias
->number
;
196 /* Do not do processing below for USER_NUMBER_ALIASs. */
197 if (this->user_token_number
== USER_NUMBER_ALIAS
)
200 else /* this->class == token_sym */
202 assert (this->number
!= NUMBER_UNDEFINED
);
205 symbols
[this->number
] = this;
212 /*--------------------------------------------------.
213 | Put THIS in TOKEN_TRANSLATIONS if it is a token. |
214 `--------------------------------------------------*/
217 symbol_translation (symbol_t
*this)
220 if (this->class == token_sym
221 && this->user_token_number
!= USER_NUMBER_ALIAS
)
223 /* A token which translation has already been set? */
224 if (token_translations
[this->user_token_number
] != undeftoken
->number
)
225 complain (_("tokens %s and %s both assigned number %d"),
226 symbols
[token_translations
[this->user_token_number
]]->tag
,
227 this->tag
, this->user_token_number
);
229 token_translations
[this->user_token_number
] = this->number
;
236 /*----------------------.
237 | A symbol hash table. |
238 `----------------------*/
240 /* Initial capacity of symbols hash table. */
241 #define HT_INITIAL_CAPACITY 257
243 static struct hash_table
*symbol_table
= NULL
;
246 hash_compare_symbol_t (const symbol_t
*m1
, const symbol_t
*m2
)
248 return strcmp (m1
->tag
, m2
->tag
) ? FALSE
: TRUE
;
252 hash_symbol_t (const symbol_t
*m
, unsigned int tablesize
)
254 return hash_string (m
->tag
, tablesize
);
258 /*-------------------------------.
259 | Create the symbol hash table. |
260 `-------------------------------*/
265 symbol_table
= hash_initialize (HT_INITIAL_CAPACITY
,
267 (Hash_hasher
) hash_symbol_t
,
268 (Hash_comparator
) hash_compare_symbol_t
,
269 (Hash_data_freer
) symbol_free
);
273 /*----------------------------------------------------------------.
274 | Find the symbol named KEY, and return it. If it does not exist |
276 `----------------------------------------------------------------*/
279 getsym (const char *key
)
284 (const char *) probe
.tag
= key
;
285 entry
= hash_lookup (symbol_table
, &probe
);
289 /* First insertion in the hash. */
290 entry
= symbol_new (key
);
291 hash_insert (symbol_table
, entry
);
297 /*-------------------.
298 | Free the symbols. |
299 `-------------------*/
304 hash_free (symbol_table
);
308 /*---------------------------------------------------------------.
309 | Look for undefined symbols, report an error, and consider them |
311 `---------------------------------------------------------------*/
314 symbols_do (symbol_processor processor
, void *processor_data
)
316 hash_do_for_each (symbol_table
,
317 (Hash_processor
) processor
,
322 /*--------------------------------------------------------------.
323 | Check that all the symbols are defined. Report any undefined |
324 | symbols and consider them nonterminals. |
325 `--------------------------------------------------------------*/
328 symbols_check_defined (void)
330 symbols_do (symbol_check_defined
, NULL
);
333 /*------------------------------------------------------------------.
334 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
336 `------------------------------------------------------------------*/
339 symbols_token_translations_init (void)
341 int num_256_available_p
= TRUE
;
344 /* Find the highest user token number, and whether 256, the POSIX
345 preferred user token number for the error token, is used. */
346 max_user_token_number
= 0;
347 for (i
= 0; i
< ntokens
; ++i
)
349 symbol_t
*this = symbols
[i
];
350 if (this->user_token_number
!= USER_NUMBER_UNDEFINED
)
352 if (this->user_token_number
> max_user_token_number
)
353 max_user_token_number
= this->user_token_number
;
354 if (this->user_token_number
== 256)
355 num_256_available_p
= FALSE
;
359 /* If 256 is not used, assign it to error, to follow POSIX. */
360 if (num_256_available_p
361 && errtoken
->user_token_number
== USER_NUMBER_UNDEFINED
)
362 errtoken
->user_token_number
= 256;
364 /* Set the missing user numbers. */
365 if (max_user_token_number
< 256)
366 max_user_token_number
= 256;
368 for (i
= 0; i
< ntokens
; ++i
)
370 symbol_t
*this = symbols
[i
];
371 if (this->user_token_number
== USER_NUMBER_UNDEFINED
)
372 this->user_token_number
= ++max_user_token_number
;
373 if (this->user_token_number
> max_user_token_number
)
374 max_user_token_number
= this->user_token_number
;
377 token_translations
= XCALLOC (symbol_number_t
, max_user_token_number
+ 1);
379 /* Initialize all entries for literal tokens to 2, the internal
380 token number for $undefined., which represents all invalid
382 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
383 token_translations
[i
] = undeftoken
->number
;
384 symbols_do (symbol_translation
, NULL
);
388 /*----------------------------------------------------------------.
389 | Assign symbol numbers, and write definition of token names into |
390 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
391 `----------------------------------------------------------------*/
396 symbols
= XCALLOC (symbol_t
*, nsyms
);
398 symbols_do (symbol_check_alias_consistence
, NULL
);
399 symbols_do (symbol_pack
, NULL
);
401 symbols_token_translations_init ();
403 if (startsymbol
->class == unknown_sym
)
404 fatal (_("the start symbol %s is undefined"), startsymbol
->tag
);
405 else if (startsymbol
->class == token_sym
)
406 fatal (_("the start symbol %s is a token"), startsymbol
->tag
);