maint: mention help2man, texinfo, apt-get
[bison.git] / src / symtab.h
blobbcc749511c2d20465bcc3e092e6af6f0131fca94
1 /* Definitions for symtab.c and callers, part of Bison.
3 Copyright (C) 1984, 1989, 1992, 2000-2002, 2004-2013 Free Software
4 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/>. */
21 /**
22 * \file symtab.h
23 * \brief Manipulating ::symbol.
26 #ifndef SYMTAB_H_
27 # define SYMTAB_H_
29 # include "assoc.h"
30 # include "location.h"
31 # include "scan-code.h"
32 # include "uniqstr.h"
34 /*----------.
35 | Symbols. |
36 `----------*/
38 /** Symbol classes. */
39 typedef enum
41 unknown_sym, /**< Undefined. */
42 token_sym, /**< Terminal. */
43 nterm_sym /**< Non-terminal. */
44 } symbol_class;
47 /** Internal token numbers. */
48 typedef int symbol_number;
49 # define SYMBOL_NUMBER_MAXIMUM INT_MAX
52 typedef struct symbol symbol;
54 /* Declaration status of a symbol.
56 First, it is "undeclared". Then, if "undeclared" and used in a
57 %printer/%destructor, it is "used". If not "declared" but used in
58 a rule, it is "needed". Finally, if declared (via a rule for
59 nonterminals, or %token), it is "declared".
61 When status are checked at the end, "declared" symbols are fine,
62 "used" symbols trigger warnings, otherwise it's an error. */
64 typedef enum
66 /** Used in the input file for an unknown reason (error). */
67 undeclared,
68 /** Used by %destructor/%printer but not defined (warning). */
69 used,
70 /** Used in the gramar (rules) but not defined (error). */
71 needed,
72 /** Defined with %type or %token (good). */
73 declared,
74 } status;
76 typedef enum code_props_type code_props_type;
77 enum code_props_type
79 destructor = 0,
80 printer = 1,
83 enum { CODE_PROPS_SIZE = 2 };
85 /* When extending this structure, be sure to complete
86 symbol_check_alias_consistency. */
87 struct symbol
89 /** The key, name of the symbol. */
90 uniqstr tag;
91 /** The location of its first occurrence. */
92 location location;
94 /** Its \c \%type.
96 Beware that this is the type_name as was entered by the user,
97 including silly things such as "]" if she entered "%token <]> t".
98 Therefore, when outputting type_name to M4, be sure to escape it
99 into "@}". See quoted_output for instance. */
100 uniqstr type_name;
102 /** Its \c \%type's location. */
103 location type_location;
105 /** Any \c \%destructor (resp. \%printer) declared specificially for this
106 symbol.
108 Access this field only through <tt>symbol</tt>'s interface functions. For
109 example, if <tt>symbol::destructor = NULL</tt> (resp. <tt>symbol::printer
110 = NULL</tt>), a default \c \%destructor (resp. \%printer) or a per-type
111 \c symbol_destructor_printer_get will compute the correct one. */
112 code_props props[CODE_PROPS_SIZE];
114 symbol_number number;
115 location prec_location;
116 int prec;
117 assoc assoc;
118 int user_token_number;
120 /* Points to the other in the symbol-string pair for an alias.
121 Special value USER_NUMBER_HAS_STRING_ALIAS in the symbol half of the
122 symbol-string pair for an alias. */
123 symbol *alias;
124 symbol_class class;
125 status status;
128 /** Undefined user number. */
129 # define USER_NUMBER_UNDEFINED -1
131 /* 'symbol->user_token_number == USER_NUMBER_HAS_STRING_ALIAS' means
132 this symbol has a literal string alias. For instance, '%token foo
133 "foo"' has '"foo"' numbered regularly, and 'foo' numbered as
134 USER_NUMBER_HAS_STRING_ALIAS. */
135 # define USER_NUMBER_HAS_STRING_ALIAS -9991
137 /* Undefined internal token number. */
138 # define NUMBER_UNDEFINED (-1)
140 /** Fetch (or create) the symbol associated to KEY. */
141 symbol *symbol_from_uniqstr (const uniqstr key, location loc);
143 /** Fetch (or create) the symbol associated to KEY. */
144 symbol *symbol_get (const char *key, location loc);
146 /** Generate a dummy nonterminal.
148 Its name cannot conflict with the user's names. */
149 symbol *dummy_symbol_get (location loc);
152 /*--------------------.
153 | Methods on symbol. |
154 `--------------------*/
156 /** Print a symbol (for debugging). */
157 void symbol_print (symbol const *s, FILE *f);
159 /** Is this a dummy nonterminal? */
160 bool symbol_is_dummy (const symbol *sym);
162 /** The name of the code_props type: "\%destructor" or "\%printer". */
163 char const *code_props_type_string (code_props_type kind);
165 /** The name of the symbol that can be used as an identifier.
166 ** Consider the alias if needed.
167 ** Return 0 if there is none (e.g., the symbol is only defined as
168 ** a string). */
169 uniqstr symbol_id_get (symbol const *sym);
172 * Make \c str the literal string alias of \c sym. Copy token number,
173 * symbol number, and type from \c sym to \c str.
175 void symbol_make_alias (symbol *sym, symbol *str, location loc);
177 /** Set the \c type_name associated with \c sym.
179 Do nothing if passed 0 as \c type_name. */
180 void symbol_type_set (symbol *sym, uniqstr type_name, location loc);
182 /** Set the \c \%destructor or \c \%printer associated with \c sym. */
183 void symbol_code_props_set (symbol *sym, code_props_type kind,
184 code_props const *destructor);
186 /** Get the computed \c \%destructor or \c %printer for \c sym, which was
187 initialized with \c code_props_none_init if there's no \c \%destructor or
188 \c %printer. */
189 code_props *symbol_code_props_get (symbol *sym, code_props_type kind);
191 /** Set the \c precedence associated with \c sym.
193 Ensure that \a symbol is a terminal.
194 Do nothing if invoked with \c undef_assoc as \c assoc. */
195 void symbol_precedence_set (symbol *sym, int prec, assoc a, location loc);
197 /** Set the \c class associated with \c sym. */
198 void symbol_class_set (symbol *sym, symbol_class class, location loc,
199 bool declaring);
201 /** Set the \c user_token_number associated with \c sym. */
202 void symbol_user_token_number_set (symbol *sym, int user_number, location loc);
206 /*------------------.
207 | Special symbols. |
208 `------------------*/
210 /** The error token. */
211 extern symbol *errtoken;
212 /** The token for unknown tokens. */
213 extern symbol *undeftoken;
214 /** The end of input token. */
215 extern symbol *endtoken;
216 /** The genuine start symbol.
218 $accept: start-symbol $end */
219 extern symbol *accept;
221 /** The user start symbol. */
222 extern symbol *startsymbol;
223 /** The location of the \c \%start declaration. */
224 extern location startsymbol_location;
228 /*-------------------.
229 | Symbol Relations. |
230 `-------------------*/
232 /* The symbol relations are represented by a directed graph. */
234 /* The id of a node */
235 typedef int graphid;
237 typedef struct symgraphlink symgraphlink;
239 struct symgraphlink
241 /** The second \c symbol or group of a precedence relation.
242 * See \c symgraph. */
243 graphid id;
245 symgraphlink *next;
248 /* Symbol precedence graph, to store the used precedence relations between
249 * symbols. */
251 typedef struct symgraph symgraph;
253 struct symgraph
255 /** Identifier for the node: equal to the number of the symbol. */
256 graphid id;
258 /** The list of related symbols that have a smaller precedence. */
259 symgraphlink *succ;
261 /** The list of related symbols that have a greater precedence. */
262 symgraphlink *pred;
265 /** Register a new precedence relation as used. */
267 void register_precedence (graphid first, graphid snd);
269 /** Print a warning for each symbol whose precedence and/or associativity
270 * is useless. */
272 void print_precedence_warnings (void);
274 /*----------------------.
275 | Symbol associativity |
276 `----------------------*/
278 void register_assoc (graphid i, graphid j);
280 /*-----------------.
281 | Semantic types. |
282 `-----------------*/
284 /** A semantic type and its associated \c \%destructor and \c \%printer.
286 Access the fields of this struct only through the interface functions in
287 this file. \sa symbol::destructor */
288 typedef struct {
289 /** The key, name of the semantic type. */
290 uniqstr tag;
292 /** The location of its first occurence. */
293 location location;
295 /** Its status : "undeclared", "used" or "declared".
296 It cannot be "needed". */
297 status status;
299 /** Any \c %destructor and %printer declared for this
300 semantic type. */
301 code_props props[CODE_PROPS_SIZE];
303 } semantic_type;
305 /** Fetch (or create) the semantic type associated to KEY. */
306 semantic_type *semantic_type_from_uniqstr (const uniqstr key,
307 const location *loc);
309 /** Fetch (or create) the semantic type associated to KEY. */
310 semantic_type *semantic_type_get (const char *key, const location *loc);
312 /** Set the \c destructor or \c printer associated with \c type. */
313 void semantic_type_code_props_set (semantic_type *type,
314 code_props_type kind,
315 code_props const *code);
317 /*----------------------------------.
318 | Symbol and semantic type tables. |
319 `----------------------------------*/
321 /** Create the symbol and semantic type tables. */
322 void symbols_new (void);
324 /** Free all the memory allocated for symbols and semantic types. */
325 void symbols_free (void);
327 /** Check that all the symbols are defined.
329 Report any undefined symbols and consider them nonterminals. */
330 void symbols_check_defined (void);
332 /** Sanity checks and #token_translations construction.
334 Perform various sanity checks, assign symbol numbers, and set up
335 #token_translations. */
336 void symbols_pack (void);
338 #endif /* !SYMTAB_H_ */