1 /* Definitions for symtab.c and callers, part of Bison.
3 Copyright (C) 1984, 1989, 1992, 2000, 2001, 2002, 2004, 2005, 2006
4 Free Software Foundation, Inc.
6 This file is part of Bison, the GNU Compiler Compiler.
8 Bison 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 2, or (at your option)
13 Bison 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.
18 You should have received a copy of the GNU General Public License
19 along with Bison; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
25 * \brief Manipulating ::symbol.
32 # include "location.h"
39 /** Symbol classes. */
42 unknown_sym
, /**< Undefined. */
43 token_sym
, /**< Terminal. */
44 nterm_sym
/**< Non-terminal. */
48 /** Internal token numbers. */
49 typedef int symbol_number
;
50 #define SYMBOL_NUMBER_MAXIMUM INT_MAX
53 typedef struct symbol symbol
;
55 /* When extending this structure, be sure to complete
56 symbol_check_alias_consistency. */
59 /** The key, name of the symbol. */
61 /** The location of its first occurrence. */
66 /** Its \c \%type's location. */
67 location type_location
;
69 /** Any \c \%destructor declared specifically for this symbol.
71 Access this field only through <tt>symbol</tt>'s interface functions. For
72 example, if <tt>symbol::destructor = NULL</tt>, a default \c \%destructor
73 or a per-type \c \%destructor might be appropriate, and
74 \c symbol_destructor_get will compute the correct one. */
75 const char *destructor
;
77 /** The location of \c symbol::destructor.
79 Access this field only through <tt>symbol</tt>'s interface functions.
80 \sa symbol::destructor */
81 location destructor_location
;
83 /** Any \c \%printer declared specifically for this symbol.
85 Access this field only through <tt>symbol</tt>'s interface functions.
86 \sa symbol::destructor */
89 /** The location of \c symbol::printer.
91 Access this field only through <tt>symbol</tt>'s interface functions.
92 \sa symbol::destructor */
93 location printer_location
;
96 location prec_location
;
99 int user_token_number
;
101 /* Points to the other in the identifier-symbol pair for an alias.
102 Special value USER_NUMBER_ALIAS in the identifier half of the
103 identifier-symbol pair for an alias. */
109 /** Undefined user number. */
110 #define USER_NUMBER_UNDEFINED -1
112 /* `symbol->user_token_number == USER_NUMBER_ALIAS' means this symbol
113 *has* (not is) a string literal alias. For instance, `%token foo
114 "foo"' has `"foo"' numbered regularly, and `foo' numbered as
115 USER_NUMBER_ALIAS. */
116 #define USER_NUMBER_ALIAS -9991
118 /* Undefined internal token number. */
119 #define NUMBER_UNDEFINED (-1)
121 /** Print a symbol (for debugging). */
122 void symbol_print (symbol
*s
, FILE *f
);
124 /** Fetch (or create) the symbol associated to KEY. */
125 symbol
*symbol_from_uniqstr (const uniqstr key
, location loc
);
127 /** Fetch (or create) the symbol associated to KEY. */
128 symbol
*symbol_get (const char *key
, location loc
);
130 /** Generate a dummy nonterminal.
132 Its name cannot conflict with the user's names. */
133 symbol
*dummy_symbol_get (location loc
);
135 /** Is this a dummy nonterminal? */
136 bool symbol_is_dummy (const symbol
*sym
);
138 /** Declare the new symbol \c sym. Make it an alias of \c symval. */
139 void symbol_make_alias (symbol
*sym
, symbol
*symval
, location loc
);
141 /** Set the \c type_name associated with \c sym.
143 Do nothing if passed 0 as \c type_name. */
144 void symbol_type_set (symbol
*sym
, uniqstr type_name
, location loc
);
146 /** Set the \c destructor associated with \c sym. */
147 void symbol_destructor_set (symbol
*sym
, const char *destructor
, location loc
);
149 /** Get the computed \c \%destructor for \c sym, or \c NULL if none. */
150 const char *symbol_destructor_get (symbol
*sym
);
152 /** Get the grammar location of the computed \c \%destructor for \c sym.
154 \pre <tt>symbol_destructor_get (sym) != NULL</tt> */
155 location
symbol_destructor_location_get (symbol
*sym
);
157 /** Set the \c printer associated with \c sym. */
158 void symbol_printer_set (symbol
*sym
, const char *printer
, location loc
);
160 /** Get the computed \c \%printer for \c sym, or \c NULL if none. */
161 const char *symbol_printer_get (symbol
*sym
);
163 /** Get the grammar location of the computed \c \%printer for \c sym.
165 \pre <tt>symbol_printer_get (sym) != NULL</tt> */
166 location
symbol_printer_location_get (symbol
*sym
);
168 /* Set the \c precedence associated with \c sym.
170 Ensure that \a symbol is a terminal.
171 Do nothing if invoked with \c undef_assoc as \c assoc. */
172 void symbol_precedence_set (symbol
*sym
, int prec
, assoc a
, location loc
);
174 /** Set the \c class associated with \c sym. */
175 void symbol_class_set (symbol
*sym
, symbol_class
class, location loc
,
178 /** Set the \c user_token_number associated with \c sym. */
179 void symbol_user_token_number_set (symbol
*sym
, int user_number
, location loc
);
182 /** The error token. */
183 extern symbol
*errtoken
;
184 /** The token for unknown tokens. */
185 extern symbol
*undeftoken
;
186 /** The end of input token. */
187 extern symbol
*endtoken
;
188 /** The genuine start symbol.
190 $accept: start-symbol $end */
191 extern symbol
*accept
;
193 /** The user start symbol. */
194 extern symbol
*startsymbol
;
195 /** The location of the \c \%start declaration. */
196 extern location startsymbol_location
;
203 /** A semantic type and its associated \c \%destructor and \c \%printer.
205 Access the fields of this struct only through the interface functions in
206 this file. \sa symbol::destructor */
207 typedef struct semantic_type
{
208 /** The key, name of the semantic type. */
211 /** Any \c %destructor declared for this semantic type. */
212 const char *destructor
;
213 /** The location of \c semantic_type::destructor. */
214 location destructor_location
;
216 /** Any \c %printer declared for this semantic type. */
218 /** The location of \c semantic_type::printer. */
219 location printer_location
;
222 /** Fetch (or create) the semantic type associated to KEY. */
223 semantic_type
*semantic_type_from_uniqstr (const uniqstr key
);
225 /** Fetch (or create) the semantic type associated to KEY. */
226 semantic_type
*semantic_type_get (const char *key
);
228 /** Set the \c destructor associated with \c type. */
229 void semantic_type_destructor_set (semantic_type
*type
, const char *destructor
,
232 /** Set the \c printer associated with \c type. */
233 void semantic_type_printer_set (semantic_type
*type
, const char *printer
,
236 /*----------------------------------.
237 | Symbol and semantic type tables. |
238 `----------------------------------*/
240 /** Create the symbol and semantic type tables. */
241 void symbols_new (void);
243 /** Free all the memory allocated for symbols and semantic types. */
244 void symbols_free (void);
246 /** Check that all the symbols are defined.
248 Report any undefined symbols and consider them nonterminals. */
249 void symbols_check_defined (void);
251 /** Sanity checks and #token_translations construction.
253 Perform various sanity checks, assign symbol numbers, and set up
254 #token_translations. */
255 void symbols_pack (void);
258 /*---------------------------------------.
259 | Default %destructor's and %printer's. |
260 `---------------------------------------*/
262 /** Set the default \c \%destructor for tagged values. */
263 void default_tagged_destructor_set (const char *destructor
, location loc
);
264 /** Set the default \c \%destructor for tagless values. */
265 void default_tagless_destructor_set (const char *destructor
, location loc
);
267 /** Set the default \c \%printer for tagged values. */
268 void default_tagged_printer_set (const char *printer
, location loc
);
269 /** Set the default \c \%printer for tagless values. */
270 void default_tagless_printer_set (const char *printer
, location loc
);
272 #endif /* !SYMTAB_H_ */