]> git.saurik.com Git - bison.git/blob - src/symtab.h
* m4/.cvsignore: Add inttypes_h.m4,lib-ld.m4, lib-prefix.m4,
[bison.git] / src / symtab.h
1 /* Definitions for symtab.c and callers, part of Bison.
2
3 Copyright (C) 1984, 1989, 1992, 2000, 2001, 2002, 2004, 2005, 2006
4 Free Software Foundation, Inc.
5
6 This file is part of Bison, the GNU Compiler Compiler.
7
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)
11 any later version.
12
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.
17
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. */
22
23 /**
24 * \file symtab.h
25 * \brief Manipulating ::symbol.
26 */
27
28 #ifndef SYMTAB_H_
29 # define SYMTAB_H_
30
31 # include "assoc.h"
32 # include "location.h"
33 # include "uniqstr.h"
34
35 /*----------.
36 | Symbols. |
37 `----------*/
38
39 /** Symbol classes. */
40 typedef enum
41 {
42 unknown_sym, /**< Undefined. */
43 token_sym, /**< Terminal. */
44 nterm_sym /**< Non-terminal. */
45 } symbol_class;
46
47
48 /** Internal token numbers. */
49 typedef int symbol_number;
50 #define SYMBOL_NUMBER_MAXIMUM INT_MAX
51
52
53 typedef struct symbol symbol;
54
55 /* When extending this structure, be sure to complete
56 symbol_check_alias_consistency. */
57 struct symbol
58 {
59 /** The key, name of the symbol. */
60 uniqstr tag;
61 /** The location of its first occurrence. */
62 location location;
63
64 /** Its \c \%type. */
65 uniqstr type_name;
66 /** Its \c \%type's location. */
67 location type_location;
68
69 /** Any \c \%destructor declared specifically for this symbol.
70
71 Access this field only through <tt>symbol</tt>'s interface functions. For
72 example, if <tt>symbol::destructor = NULL</tt>, the default
73 \c \%destructor or a per-type \c \%destructor might be appropriate, and
74 \c symbol_destructor_get will compute the correct one. */
75 const char *destructor;
76
77 /** The location of \c symbol::destructor.
78
79 Access this field only through <tt>symbol</tt>'s interface functions.
80 \sa symbol::destructor */
81 location destructor_location;
82
83 /** Any \c \%printer declared specifically for this symbol.
84
85 Access this field only through <tt>symbol</tt>'s interface functions.
86 \sa symbol::destructor */
87 const char *printer;
88
89 /** The location of \c symbol::printer.
90
91 Access this field only through <tt>symbol</tt>'s interface functions.
92 \sa symbol::destructor */
93 location printer_location;
94
95 symbol_number number;
96 location prec_location;
97 int prec;
98 assoc assoc;
99 int user_token_number;
100
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. */
104 symbol *alias;
105 symbol_class class;
106 bool declared;
107 };
108
109
110 /** Undefined user number. */
111 #define USER_NUMBER_UNDEFINED -1
112
113 /* `symbol->user_token_number == USER_NUMBER_ALIAS' means this symbol
114 *has* (not is) a string literal alias. For instance, `%token foo
115 "foo"' has `"foo"' numbered regularly, and `foo' numbered as
116 USER_NUMBER_ALIAS. */
117 #define USER_NUMBER_ALIAS -9991
118
119 /* Undefined internal token number. */
120 #define NUMBER_UNDEFINED (-1)
121
122 /** Print a symbol (for debugging). */
123 void symbol_print (symbol *s, FILE *f);
124
125 /** Fetch (or create) the symbol associated to KEY. */
126 symbol *symbol_from_uniqstr (const uniqstr key, location loc);
127
128 /** Fetch (or create) the symbol associated to KEY. */
129 symbol *symbol_get (const char *key, location loc);
130
131 /** Generate a dummy nonterminal.
132
133 Its name cannot conflict with the user's names. */
134 symbol *dummy_symbol_get (location loc);
135
136 /** Declare the new symbol \c sym. Make it an alias of \c symval. */
137 void symbol_make_alias (symbol *sym, symbol *symval, location loc);
138
139 /** Set the \c type_name associated with \c sym.
140
141 Do nothing if passed 0 as \c type_name. */
142 void symbol_type_set (symbol *sym, uniqstr type_name, location loc);
143
144 /** Set the \c destructor associated with \c sym. */
145 void symbol_destructor_set (symbol *sym, const char *destructor, location loc);
146
147 /** Get the computed \c \%destructor for \c sym, or \c NULL if none. */
148 const char *symbol_destructor_get (symbol *sym);
149
150 /** Get the grammar location of the computed \c \%destructor for \c sym.
151
152 \pre <tt>symbol_destructor_get (sym) != NULL</tt> */
153 location symbol_destructor_location_get (symbol *sym);
154
155 /** Set the \c printer associated with \c sym. */
156 void symbol_printer_set (symbol *sym, const char *printer, location loc);
157
158 /** Get the computed \c \%printer for \c sym, or \c NULL if none. */
159 const char *symbol_printer_get (symbol *sym);
160
161 /** Get the grammar location of the computed \c \%printer for \c sym.
162
163 \pre <tt>symbol_printer_get (sym) != NULL</tt> */
164 location symbol_printer_location_get (symbol *sym);
165
166 /* Set the \c precedence associated with \c sym.
167
168 Ensure that \a symbol is a terminal.
169 Do nothing if invoked with \c undef_assoc as \c assoc. */
170 void symbol_precedence_set (symbol *sym, int prec, assoc a, location loc);
171
172 /** Set the \c class associated with \c sym. */
173 void symbol_class_set (symbol *sym, symbol_class class, location loc,
174 bool declaring);
175
176 /** Set the \c user_token_number associated with \c sym. */
177 void symbol_user_token_number_set (symbol *sym, int user_number, location loc);
178
179
180 /** The error token. */
181 extern symbol *errtoken;
182 /** The token for unknown tokens. */
183 extern symbol *undeftoken;
184 /** The end of input token. */
185 extern symbol *endtoken;
186 /** The genuine start symbol.
187
188 $accept: start-symbol $end */
189 extern symbol *accept;
190
191 /** The user start symbol. */
192 extern symbol *startsymbol;
193 /** The location of the \c \%start declaration. */
194 extern location startsymbol_location;
195
196
197 /*---------------.
198 | Symbol table. |
199 `---------------*/
200
201
202 /** Create the symbol table. */
203 void symbols_new (void);
204
205 /** Free all the memory allocated for symbols. */
206 void symbols_free (void);
207
208 /** Check that all the symbols are defined.
209
210 Report any undefined symbols and consider them nonterminals. */
211 void symbols_check_defined (void);
212
213 /** Sanity checks and #token_translations construction.
214
215 Perform various sanity checks, assign symbol numbers, and set up
216 #token_translations. */
217 void symbols_pack (void);
218
219
220 /*-----------------------------------.
221 | Default %destructor and %printer. |
222 `-----------------------------------*/
223
224 /** Set the default \c \%destructor. */
225 void default_destructor_set (const char *destructor, location loc);
226
227 /** Set the default \c \%printer. */
228 void default_printer_set (const char *printer, location loc);
229
230 #endif /* !SYMTAB_H_ */