1 /* Definitions for symtab.c and callers, part of Bison.
3 Copyright (C) 1984, 1989, 1992, 2000, 2001, 2002, 2004, 2005, 2006, 2007
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"
33 # include "scan-code.h"
40 /** Symbol classes. */
43 unknown_sym
, /**< Undefined. */
44 token_sym
, /**< Terminal. */
45 nterm_sym
/**< Non-terminal. */
49 /** Internal token numbers. */
50 typedef int symbol_number
;
51 #define SYMBOL_NUMBER_MAXIMUM INT_MAX
54 typedef struct symbol symbol
;
56 /* When extending this structure, be sure to complete
57 symbol_check_alias_consistency. */
60 /** The key, name of the symbol. */
62 /** The location of its first occurrence. */
67 /** Its \c \%type's location. */
68 location type_location
;
70 /** Any \c \%destructor declared specifically for this symbol.
72 Access this field only through <tt>symbol</tt>'s interface functions. For
73 example, if <tt>symbol::destructor = NULL</tt>, a default \c \%destructor
74 or a per-type \c \%destructor might be appropriate, and
75 \c symbol_destructor_get will compute the correct one. */
76 code_props destructor
;
78 /** Any \c \%printer declared specifically for this symbol.
80 Access this field only through <tt>symbol</tt>'s interface functions.
81 \sa symbol::destructor */
85 location prec_location
;
88 int user_token_number
;
90 /* Points to the other in the identifier-symbol pair for an alias.
91 Special value USER_NUMBER_ALIAS in the identifier half of the
92 identifier-symbol pair for an alias. */
98 /** Undefined user number. */
99 #define USER_NUMBER_UNDEFINED -1
101 /* `symbol->user_token_number == USER_NUMBER_ALIAS' means this symbol
102 *has* (not is) a string literal alias. For instance, `%token foo
103 "foo"' has `"foo"' numbered regularly, and `foo' numbered as
104 USER_NUMBER_ALIAS. */
105 #define USER_NUMBER_ALIAS -9991
107 /* Undefined internal token number. */
108 #define NUMBER_UNDEFINED (-1)
110 /** Print a symbol (for debugging). */
111 void symbol_print (symbol
*s
, FILE *f
);
113 /** Fetch (or create) the symbol associated to KEY. */
114 symbol
*symbol_from_uniqstr (const uniqstr key
, location loc
);
116 /** Fetch (or create) the symbol associated to KEY. */
117 symbol
*symbol_get (const char *key
, location loc
);
119 /** Generate a dummy nonterminal.
121 Its name cannot conflict with the user's names. */
122 symbol
*dummy_symbol_get (location loc
);
124 /** Is this a dummy nonterminal? */
125 bool symbol_is_dummy (const symbol
*sym
);
127 /** Declare the new symbol \c sym. Make it an alias of \c symval. */
128 void symbol_make_alias (symbol
*sym
, symbol
*symval
, location loc
);
130 /** Set the \c type_name associated with \c sym.
132 Do nothing if passed 0 as \c type_name. */
133 void symbol_type_set (symbol
*sym
, uniqstr type_name
, location loc
);
135 /** Set the \c destructor associated with \c sym. */
136 void symbol_destructor_set (symbol
*sym
, code_props
const *destructor
);
138 /** Get the computed \c \%destructor for \c sym, which was initialized with
139 \c code_props_none_init if there's no \c \%destructor. */
140 code_props
const *symbol_destructor_get (symbol
const *sym
);
142 /** Set the \c printer associated with \c sym. */
143 void symbol_printer_set (symbol
*sym
, code_props
const *printer
);
145 /** Get the computed \c \%printer for \c sym, which was initialized with
146 \c code_props_none_init if there's no \c \%printer. */
147 code_props
const *symbol_printer_get (symbol
const *sym
);
149 /* Set the \c precedence associated with \c sym.
151 Ensure that \a symbol is a terminal.
152 Do nothing if invoked with \c undef_assoc as \c assoc. */
153 void symbol_precedence_set (symbol
*sym
, int prec
, assoc a
, location loc
);
155 /** Set the \c class associated with \c sym. */
156 void symbol_class_set (symbol
*sym
, symbol_class
class, location loc
,
159 /** Set the \c user_token_number associated with \c sym. */
160 void symbol_user_token_number_set (symbol
*sym
, int user_number
, location loc
);
163 /** The error token. */
164 extern symbol
*errtoken
;
165 /** The token for unknown tokens. */
166 extern symbol
*undeftoken
;
167 /** The end of input token. */
168 extern symbol
*endtoken
;
169 /** The genuine start symbol.
171 $accept: start-symbol $end */
172 extern symbol
*accept
;
174 /** The user start symbol. */
175 extern symbol
*startsymbol
;
176 /** The location of the \c \%start declaration. */
177 extern location startsymbol_location
;
184 /** A semantic type and its associated \c \%destructor and \c \%printer.
186 Access the fields of this struct only through the interface functions in
187 this file. \sa symbol::destructor */
189 /** The key, name of the semantic type. */
192 /** Any \c %destructor declared for this semantic type. */
193 code_props destructor
;
194 /** Any \c %printer declared for this semantic type. */
198 /** Fetch (or create) the semantic type associated to KEY. */
199 semantic_type
*semantic_type_from_uniqstr (const uniqstr key
);
201 /** Fetch (or create) the semantic type associated to KEY. */
202 semantic_type
*semantic_type_get (const char *key
);
204 /** Set the \c destructor associated with \c type. */
205 void semantic_type_destructor_set (semantic_type
*type
,
206 code_props
const *destructor
);
208 /** Set the \c printer associated with \c type. */
209 void semantic_type_printer_set (semantic_type
*type
,
210 code_props
const *printer
);
212 /*----------------------------------.
213 | Symbol and semantic type tables. |
214 `----------------------------------*/
216 /** Create the symbol and semantic type tables. */
217 void symbols_new (void);
219 /** Free all the memory allocated for symbols and semantic types. */
220 void symbols_free (void);
222 /** Check that all the symbols are defined.
224 Report any undefined symbols and consider them nonterminals. */
225 void symbols_check_defined (void);
227 /** Sanity checks and #token_translations construction.
229 Perform various sanity checks, assign symbol numbers, and set up
230 #token_translations. */
231 void symbols_pack (void);
234 /*---------------------------------------.
235 | Default %destructor's and %printer's. |
236 `---------------------------------------*/
238 /** Set the default \c \%destructor for tagged values. */
239 void default_tagged_destructor_set (code_props
const *destructor
);
240 /** Set the default \c \%destructor for tagless values. */
241 void default_tagless_destructor_set (code_props
const *destructor
);
243 /** Set the default \c \%printer for tagged values. */
244 void default_tagged_printer_set (code_props
const *printer
);
245 /** Set the default \c \%printer for tagless values. */
246 void default_tagless_printer_set (code_props
const *printer
);
248 #endif /* !SYMTAB_H_ */