1 /* Definitions for symtab.c and callers, part of Bison.
3 Copyright (C) 1984, 1989, 1992, 2000-2002, 2004-2007, 2009-2012 Free
4 Software Foundation, Inc.
6 This file is part of Bison, the GNU Compiler Compiler.
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.
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.
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/>. */
23 * \brief Manipulating ::symbol.
30 # include "location.h"
31 # include "scan-code.h"
38 /** Symbol classes. */
41 unknown_sym
, /**< Undefined. */
42 token_sym
, /**< Terminal. */
43 nterm_sym
/**< Non-terminal. */
47 /** Internal token numbers. */
48 typedef int symbol_number
;
49 #define SYMBOL_NUMBER_MAXIMUM INT_MAX
52 typedef struct symbol symbol
;
54 /* When extending this structure, be sure to complete
55 symbol_check_alias_consistency. */
58 /** The key, name of the symbol. */
60 /** 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
72 functions. For example, if <tt>symbol::destructor = NULL</tt>, a
73 default \c \%destructor or a per-type \c \%destructor might be
74 appropriate, and \c symbol_destructor_get will compute the
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 symbol-string pair for an alias.
91 Special value USER_NUMBER_HAS_STRING_ALIAS in the symbol half of the
92 symbol-string pair for an alias. */
98 /** Undefined user number. */
99 #define USER_NUMBER_UNDEFINED -1
101 /* `symbol->user_token_number == USER_NUMBER_HAS_STRING_ALIAS' means
102 this symbol has a literal string alias. For instance, `%token foo
103 "foo"' has `"foo"' numbered regularly, and `foo' numbered as
104 USER_NUMBER_HAS_STRING_ALIAS. */
105 #define USER_NUMBER_HAS_STRING_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
);
128 * Make \c str the literal string alias of \c sym. Copy token number,
129 * symbol number, and type from \c sym to \c str.
131 void symbol_make_alias (symbol
*sym
, symbol
*str
, location loc
);
133 /** Set the \c type_name associated with \c sym.
135 Do nothing if passed 0 as \c type_name. */
136 void symbol_type_set (symbol
*sym
, uniqstr type_name
, location loc
);
138 /** Set the \c destructor associated with \c sym. */
139 void symbol_destructor_set (symbol
*sym
, code_props
const *destructor
);
141 /** Get the computed \c \%destructor for \c sym, which was initialized with
142 \c code_props_none_init if there's no \c \%destructor. */
143 code_props
const *symbol_destructor_get (symbol
const *sym
);
145 /** Set the \c printer associated with \c sym. */
146 void symbol_printer_set (symbol
*sym
, code_props
const *printer
);
148 /** Get the computed \c \%printer for \c sym, which was initialized with
149 \c code_props_none_init if there's no \c \%printer. */
150 code_props
const *symbol_printer_get (symbol
const *sym
);
152 /* Set the \c precedence associated with \c sym.
154 Ensure that \a symbol is a terminal.
155 Do nothing if invoked with \c undef_assoc as \c assoc. */
156 void symbol_precedence_set (symbol
*sym
, int prec
, assoc a
, location loc
);
158 /** Set the \c class associated with \c sym. */
159 void symbol_class_set (symbol
*sym
, symbol_class
class, location loc
,
162 /** Set the \c user_token_number associated with \c sym. */
163 void symbol_user_token_number_set (symbol
*sym
, int user_number
, location loc
);
166 /** The error token. */
167 extern symbol
*errtoken
;
168 /** The token for unknown tokens. */
169 extern symbol
*undeftoken
;
170 /** The end of input token. */
171 extern symbol
*endtoken
;
172 /** The genuine start symbol.
174 $accept: start-symbol $end */
175 extern symbol
*accept
;
177 /** The user start symbol. */
178 extern symbol
*startsymbol
;
179 /** The location of the \c \%start declaration. */
180 extern location startsymbol_location
;
187 /** A semantic type and its associated \c \%destructor and \c \%printer.
189 Access the fields of this struct only through the interface functions in
190 this file. \sa symbol::destructor */
192 /** The key, name of the semantic type. */
195 /** Any \c %destructor declared for this semantic type. */
196 code_props destructor
;
197 /** Any \c %printer declared for this semantic type. */
201 /** Fetch (or create) the semantic type associated to KEY. */
202 semantic_type
*semantic_type_from_uniqstr (const uniqstr key
);
204 /** Fetch (or create) the semantic type associated to KEY. */
205 semantic_type
*semantic_type_get (const char *key
);
207 /** Set the \c destructor associated with \c type. */
208 void semantic_type_destructor_set (semantic_type
*type
,
209 code_props
const *destructor
);
211 /** Set the \c printer associated with \c type. */
212 void semantic_type_printer_set (semantic_type
*type
,
213 code_props
const *printer
);
215 /*----------------------------------.
216 | Symbol and semantic type tables. |
217 `----------------------------------*/
219 /** Create the symbol and semantic type tables. */
220 void symbols_new (void);
222 /** Free all the memory allocated for symbols and semantic types. */
223 void symbols_free (void);
225 /** Check that all the symbols are defined.
227 Report any undefined symbols and consider them nonterminals. */
228 void symbols_check_defined (void);
230 /** Sanity checks and #token_translations construction.
232 Perform various sanity checks, assign symbol numbers, and set up
233 #token_translations. */
234 void symbols_pack (void);
237 /*---------------------------------------.
238 | Default %destructor's and %printer's. |
239 `---------------------------------------*/
241 /** Set the default \c \%destructor for tagged values. */
242 void default_tagged_destructor_set (code_props
const *destructor
);
243 /** Set the default \c \%destructor for tagless values. */
244 void default_tagless_destructor_set (code_props
const *destructor
);
246 /** Set the default \c \%printer for tagged values. */
247 void default_tagged_printer_set (code_props
const *printer
);
248 /** Set the default \c \%printer for tagless values. */
249 void default_tagless_printer_set (code_props
const *printer
);
251 #endif /* !SYMTAB_H_ */