]> git.saurik.com Git - bison.git/blob - src/symtab.h
00770a6e24807fbea6c3eb27db14d48cadbb9de9
[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 %type and associated printer and destructor. */
65 uniqstr type_name;
66 location type_location;
67
68 /** Does not own the memory. */
69 const char *destructor;
70 location destructor_location;
71
72 /** Printer. */
73 const char *printer;
74 location printer_location;
75
76 symbol_number number;
77 location prec_location;
78 int prec;
79 assoc assoc;
80 int user_token_number;
81
82 /* Points to the other in the identifier-symbol pair for an alias.
83 Special value USER_NUMBER_ALIAS in the identifier half of the
84 identifier-symbol pair for an alias. */
85 symbol *alias;
86 symbol_class class;
87 bool declared;
88 };
89
90
91 /** Undefined user number. */
92 #define USER_NUMBER_UNDEFINED -1
93
94 /* `symbol->user_token_number == USER_NUMBER_ALIAS' means this symbol
95 *has* (not is) a string literal alias. For instance, `%token foo
96 "foo"' has `"foo"' numbered regularly, and `foo' numbered as
97 USER_NUMBER_ALIAS. */
98 #define USER_NUMBER_ALIAS -9991
99
100 /* Undefined internal token number. */
101 #define NUMBER_UNDEFINED (-1)
102
103 /** Print a symbol (for debugging). */
104 void symbol_print (symbol *s, FILE *f);
105
106 /** Fetch (or create) the symbol associated to KEY. */
107 symbol *symbol_from_uniqstr (const uniqstr key, location loc);
108
109 /** Fetch (or create) the symbol associated to KEY. */
110 symbol *symbol_get (const char *key, location loc);
111
112 /** Generate a dummy nonterminal.
113
114 Its name cannot conflict with the user's names. */
115 symbol *dummy_symbol_get (location loc);
116
117 /** Declare the new symbol \c sym. Make it an alias of \c symval. */
118 void symbol_make_alias (symbol *sym, symbol *symval, location loc);
119
120 /** Set the \c type_name associated with \c sym.
121
122 Do nothing if passed 0 as \c type_name. */
123 void symbol_type_set (symbol *sym, uniqstr type_name, location loc);
124
125 /** Set the \c destructor associated with \c sym. */
126 void symbol_destructor_set (symbol *sym, const char *destructor, location loc);
127
128 /** Set the \c printer associated with \c sym. */
129 void symbol_printer_set (symbol *sym, const char *printer, location loc);
130
131 /* Set the \c precedence associated with \c sym.
132
133 Ensure that \a symbol is a terminal.
134 Do nothing if invoked with \c undef_assoc as \c assoc. */
135 void symbol_precedence_set (symbol *sym, int prec, assoc a, location loc);
136
137 /** Set the \c class associated with \c sym. */
138 void symbol_class_set (symbol *sym, symbol_class class, location loc,
139 bool declaring);
140
141 /** Set the \c user_token_number associated with \c sym. */
142 void symbol_user_token_number_set (symbol *sym, int user_number, location loc);
143
144
145 /** The error token. */
146 extern symbol *errtoken;
147 /** The token for unknown tokens. */
148 extern symbol *undeftoken;
149 /** The end of input token. */
150 extern symbol *endtoken;
151 /** The genuine start symbol.
152
153 $accept: start-symbol $end */
154 extern symbol *accept;
155
156 /** The user start symbol. */
157 extern symbol *startsymbol;
158 /** The location of the \c %start declaration. */
159 extern location startsymbol_location;
160
161
162 /*---------------.
163 | Symbol table. |
164 `---------------*/
165
166
167 /** Create the symbol table. */
168 void symbols_new (void);
169
170 /** Free all the memory allocated for symbols. */
171 void symbols_free (void);
172
173 /** Check that all the symbols are defined.
174
175 Report any undefined symbols and consider them nonterminals. */
176 void symbols_check_defined (void);
177
178 /** Sanity checks and #token_translations construction.
179
180 Perform various sanity checks, assign symbol numbers, and set up
181 #token_translations. */
182 void symbols_pack (void);
183
184 #endif /* !SYMTAB_H_ */