]> git.saurik.com Git - bison.git/blob - src/symtab.h
ecf28cd097195977dc62aaeb743e4fc5bf30c935
[bison.git] / src / symtab.h
1 /* Definitions for symtab.c and callers, part of bison,
2 Copyright (C) 1984, 1989, 1992, 2000, 2001, 2002
3 Free Software Foundation, Inc.
4
5 This file is part of Bison, the GNU Compiler Compiler.
6
7 Bison is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 Bison is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Bison; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #ifndef SYMTAB_H_
23 # define SYMTAB_H_
24
25 # include "struniq.h"
26 # include "location.h"
27 # include "assoc.h"
28
29 /*----------.
30 | Symbols. |
31 `----------*/
32
33 /* Symbol classes. */
34 typedef enum
35 {
36 unknown_sym,
37 token_sym, /* terminal symbol */
38 nterm_sym /* non-terminal */
39 } symbol_class;
40
41
42 /* Internal token numbers. */
43 typedef short symbol_number_t;
44 #define SYMBOL_NUMBER_MAX ((symbol_number_t) SHRT_MAX)
45
46
47 typedef struct symbol_s symbol_t;
48 struct symbol_s
49 {
50 /* The key, name of the symbol. */
51 char *tag;
52 /* The location of its first occurence. */
53 location_t location;
54
55 /* Its %type and associated printer and destructor. */
56 struniq_t type_name;
57 char *destructor;
58 location_t destructor_location;
59 char *printer;
60 location_t printer_location;
61
62 symbol_number_t number;
63 short prec;
64 assoc_t assoc;
65 int user_token_number;
66
67 /* Points to the other in the identifier-symbol pair for an alias.
68 Special value USER_NUMBER_ALIAS in the identifier half of the
69 identifier-symbol pair for an alias. */
70 symbol_t *alias;
71 symbol_class class;
72 };
73
74 /* Undefined user number. */
75 #define USER_NUMBER_UNDEFINED -1
76
77 /* `symbol->user_token_number == USER_NUMBER_ALIAS' means this symbol
78 *has* (not is) a string literal alias. For instance, `%token foo
79 "foo"' has `"foo"' numbered regularly, and `foo' numbered as
80 USER_NUMBER_ALIAS. */
81 #define USER_NUMBER_ALIAS -9991
82
83 /* Undefined internal token number. */
84 #define NUMBER_UNDEFINED ((symbol_number_t) -1)
85
86
87 /* Fetch (or create) the symbol associated to KEY. */
88 symbol_t *symbol_get (const char *key, location_t location);
89
90 /* Generate a dummy nonterminal, whose name cannot conflict with the
91 user's names. */
92 symbol_t *dummy_symbol_get (location_t location);
93
94 /* Declare the new SYMBOL. Make it an alias of SYMVAL. */
95 void symbol_make_alias (symbol_t *symbol, symbol_t *symval,
96 location_t location);
97
98 /* Set the TYPE_NAME associated to SYMBOL. Does nothing if passed 0 as
99 TYPE_NAME. */
100 void symbol_type_set (symbol_t *symbol,
101 char *type_name, location_t location);
102
103 /* Set the DESTRUCTOR associated to SYMBOL. */
104 void symbol_destructor_set (symbol_t *symbol,
105 char *destructor, location_t location);
106
107 /* Set the PRINTER associated to SYMBOL. */
108 void symbol_printer_set (symbol_t *symbol,
109 char *printer, location_t location);
110
111 /* Set the PRECEDENCE associated to SYMBOL. Ensures that SYMBOL is a
112 terminal. Does nothing if invoked with UNDEF_ASSOC as ASSOC. */
113 void symbol_precedence_set (symbol_t *symbol,
114 int prec, assoc_t assoc, location_t location);
115
116 /* Set the CLASS associated to SYMBOL. */
117 void symbol_class_set (symbol_t *symbol,
118 symbol_class class, location_t location);
119
120 /* Set the USER_TOKEN_NUMBER associated to SYMBOL. */
121 void symbol_user_token_number_set (symbol_t *symbol,
122 int user_number, location_t location);
123
124
125 /* Distinguished symbols. AXIOM is the real start symbol, that used
126 by the automaton. STARTSYMBOL is the one specified by the user.
127 */
128 extern symbol_t *errtoken;
129 extern symbol_t *undeftoken;
130 extern symbol_t *endtoken;
131 extern symbol_t *accept;
132 extern symbol_t *startsymbol;
133 extern location_t startsymbol_location;
134
135
136 /*---------------.
137 | Symbol table. |
138 `---------------*/
139
140
141 /* Create the symbol table. */
142 void symbols_new (void);
143
144 /* A function to apply to each symbol. */
145 typedef bool (*symbol_processor) (symbol_t *);
146
147 /* Apply PROCESSOR to all the symbols. PROCESSOR must return true: on
148 false, the processing stops. */
149 void symbols_do (symbol_processor processor, void *processor_data);
150
151 /* Free all the memory allocated for symbols. */
152 void symbols_free (void);
153
154 /* Check that all the symbols are defined. Report any undefined
155 symbols and consider them nonterminals. */
156 void symbols_check_defined (void);
157
158 /* Perform various sanity checks, assign symbol numbers, and set up
159 TOKEN_TRANSLATIONS. */
160 void symbols_pack (void);
161
162 #endif /* !SYMTAB_H_ */