Initial import from FreeBSD RELENG_4:
[games.git] / usr.bin / yacc / defs.h
1 /*
2  * Copyright (c) 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Robert Paul Corbett.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *      @(#)defs.h      5.6 (Berkeley) 5/24/93
37  * $FreeBSD: src/usr.bin/yacc/defs.h,v 1.8.2.1 2001/10/05 03:00:19 obrien Exp $
38  */
39
40 #include <sys/cdefs.h>  /* for __P macro */
41 #include <assert.h>
42 #include <ctype.h>
43 #include <err.h>
44 #include <stdio.h>
45
46
47 /*  machine-dependent definitions                       */
48 /*  the following definitions are for the Tahoe         */
49 /*  they might have to be changed for other machines    */
50
51 /*  MAXCHAR is the largest unsigned character value     */
52 /*  MAXSHORT is the largest value of a C short          */
53 /*  MINSHORT is the most negative value of a C short    */
54 /*  MAXTABLE is the maximum table size                  */
55 /*  BITS_PER_WORD is the number of bits in a C unsigned */
56 /*  WORDSIZE computes the number of words needed to     */
57 /*      store n bits                                    */
58 /*  BIT returns the value of the n-th bit starting      */
59 /*      from r (0-indexed)                              */
60 /*  SETBIT sets the n-th bit starting from r            */
61
62 #define MAXCHAR         255
63 #define MAXSHORT        32767
64 #define MINSHORT        -32768
65 #define MAXTABLE        32500
66 #define BITS_PER_WORD   32
67 #define WORDSIZE(n)     (((n)+(BITS_PER_WORD-1))/BITS_PER_WORD)
68 #define BIT(r, n)       ((((r)[(n)>>5])>>((n)&31))&1)
69 #define SETBIT(r, n)    ((r)[(n)>>5]|=((unsigned)1<<((n)&31)))
70
71
72 /*  character names  */
73
74 #define NUL             '\0'    /*  the null character  */
75 #define NEWLINE         '\n'    /*  line feed  */
76 #define SP              ' '     /*  space  */
77 #define BS              '\b'    /*  backspace  */
78 #define HT              '\t'    /*  horizontal tab  */
79 #define VT              '\013'  /*  vertical tab  */
80 #define CR              '\r'    /*  carriage return  */
81 #define FF              '\f'    /*  form feed  */
82 #define QUOTE           '\''    /*  single quote  */
83 #define DOUBLE_QUOTE    '\"'    /*  double quote  */
84 #define BACKSLASH       '\\'    /*  backslash  */
85
86
87 /* defines for constructing filenames */
88
89 #define CODE_SUFFIX     ".code.c"
90 #define DEFINES_SUFFIX  ".tab.h"
91 #define OUTPUT_SUFFIX   ".tab.c"
92 #define VERBOSE_SUFFIX  ".output"
93
94
95 /* keyword codes */
96
97 #define TOKEN 0
98 #define LEFT 1
99 #define RIGHT 2
100 #define NONASSOC 3
101 #define MARK 4
102 #define TEXT 5
103 #define TYPE 6
104 #define START 7
105 #define UNION 8
106 #define IDENT 9
107 #define EXPECT 10
108
109
110 /*  symbol classes  */
111
112 #define UNKNOWN 0
113 #define TERM 1
114 #define NONTERM 2
115
116
117 /*  the undefined value  */
118
119 #define UNDEFINED (-1)
120
121
122 /*  action codes  */
123
124 #define SHIFT 1
125 #define REDUCE 2
126
127
128 /*  character macros  */
129
130 #define IS_IDENT(c)     (isalnum(c) || (c) == '_' || (c) == '.' || (c) == '$')
131 #define IS_OCTAL(c)     ((c) >= '0' && (c) <= '7')
132 #define NUMERIC_VALUE(c)        ((c) - '0')
133
134
135 /*  symbol macros  */
136
137 #define ISTOKEN(s)      ((s) < start_symbol)
138 #define ISVAR(s)        ((s) >= start_symbol)
139
140
141 /*  storage allocation macros  */
142
143 #define CALLOC(k,n)     (calloc((unsigned)(k),(unsigned)(n)))
144 #define FREE(x)         (free((char*)(x)))
145 #define MALLOC(n)       (malloc((unsigned)(n)))
146 #define NEW(t)          ((t*)allocate(sizeof(t)))
147 #define NEW2(n,t)       ((t*)allocate((unsigned)((n)*sizeof(t))))
148 #define REALLOC(p,n)    (realloc((char*)(p),(unsigned)(n)))
149
150
151 /*  the structure of a symbol table entry  */
152
153 typedef struct bucket bucket;
154 struct bucket
155 {
156     struct bucket *link;
157     struct bucket *next;
158     char *name;
159     char *tag;
160     short value;
161     short index;
162     short prec;
163     char class;
164     char assoc;
165 };
166
167
168 /*  the structure of the LR(0) state machine  */
169
170 typedef struct core core;
171 struct core
172 {
173     struct core *next;
174     struct core *link;
175     short number;
176     short accessing_symbol;
177     short nitems;
178     short items[1];
179 };
180
181
182 /*  the structure used to record shifts  */
183
184 typedef struct shifts shifts;
185 struct shifts
186 {
187     struct shifts *next;
188     short number;
189     short nshifts;
190     short shift[1];
191 };
192
193
194 /*  the structure used to store reductions  */
195
196 typedef struct reductions reductions;
197 struct reductions
198 {
199     struct reductions *next;
200     short number;
201     short nreds;
202     short rules[1];
203 };
204
205
206 /*  the structure used to represent parser actions  */
207
208 typedef struct action action;
209 struct action
210 {
211     struct action *next;
212     short symbol;
213     short number;
214     short prec;
215     char action_code;
216     char assoc;
217     char suppressed;
218 };
219
220
221 /* global variables */
222
223 extern char dflag;
224 extern char lflag;
225 extern char rflag;
226 extern char tflag;
227 extern char vflag;
228 extern char *symbol_prefix;
229
230 extern char *myname;
231 extern char *cptr;
232 extern char *line;
233 extern int lineno;
234 extern int outline;
235
236 extern char *banner[];
237 extern char *tables[];
238 extern char *header[];
239 extern char *body[];
240 extern char *trailer[];
241
242 extern char *action_file_name;
243 extern char *code_file_name;
244 extern char *defines_file_name;
245 extern char *input_file_name;
246 extern char *output_file_name;
247 extern char *text_file_name;
248 extern char *union_file_name;
249 extern char *verbose_file_name;
250
251 extern FILE *action_file;
252 extern FILE *code_file;
253 extern FILE *defines_file;
254 extern FILE *input_file;
255 extern FILE *output_file;
256 extern FILE *text_file;
257 extern FILE *union_file;
258 extern FILE *verbose_file;
259
260 extern int nitems;
261 extern int nrules;
262 extern int nsyms;
263 extern int ntokens;
264 extern int nvars;
265 extern int ntags;
266
267 extern char unionized;
268
269 extern int   start_symbol;
270 extern char  **symbol_name;
271 extern short *symbol_value;
272 extern short *symbol_prec;
273 extern char  *symbol_assoc;
274
275 extern short *ritem;
276 extern short *rlhs;
277 extern short *rrhs;
278 extern short *rprec;
279 extern char  *rassoc;
280
281 extern short **derives;
282 extern char *nullable;
283
284 extern bucket *first_symbol;
285 extern bucket *last_symbol;
286
287 extern int nstates;
288 extern core *first_state;
289 extern shifts *first_shift;
290 extern reductions *first_reduction;
291 extern short *accessing_symbol;
292 extern core **state_table;
293 extern shifts **shift_table;
294 extern reductions **reduction_table;
295 extern unsigned *LA;
296 extern short *LAruleno;
297 extern short *lookaheads;
298 extern short *goto_map;
299 extern short *from_state;
300 extern short *to_state;
301
302 extern action **parser;
303 extern int SRexpect;
304 extern int SRtotal;
305 extern int RRtotal;
306 extern short *SRconflicts;
307 extern short *RRconflicts;
308 extern short *defred;
309 extern short *rules_used;
310 extern short nunused;
311 extern short final_state;
312
313 /* global functions */
314
315 char *allocate __P((unsigned));
316 void closure __P((short *, int));
317 void create_symbol_table __P((void));
318 void default_action_warning __P((void));
319 void dollar_error __P((int, char *, char *));
320 void dollar_warning __P((int, int));
321 void done __P((int));
322 void fatal __P((char *msg));
323 void finalize_closure __P((void));
324 void free_parser __P((void));
325 void free_symbols __P((void));
326 void free_symbol_table __P((void));
327 void illegal_character __P((char *));
328 void illegal_tag __P((int, char *, char *));
329 void lalr __P((void));
330 bucket *lookup __P((char *));
331 void lr0 __P((void));
332 bucket *make_bucket __P((char *));
333 void make_parser __P((void));
334 void no_grammar __P((void));
335 void no_space __P((void));
336 void open_error __P((char *));
337 void output __P((void));
338 void over_unionized __P((char *));
339 void prec_redeclared __P((void));
340 void reader __P((void));
341 void reflexive_transitive_closure __P((unsigned *, int));
342 void reprec_warning __P((char *));
343 void restarted_warning __P((void));
344 void retyped_warning __P((char *));
345 void revalued_warning __P((char *));
346 void set_first_derives __P((void));
347 void syntax_error __P((int, char *, char *));
348 void terminal_lhs __P((int));
349 void terminal_start __P((char *));
350 void tokenized_start __P((char *));
351 void undefined_goal __P((char *));
352 void undefined_symbol_warning __P((char *));
353 void unexpected_EOF __P((void));
354 void unknown_rhs __P((int));
355 void unterminated_action __P((int, char *, char *));
356 void unterminated_comment __P((int, char *, char *));
357 void unterminated_string __P((int, char *, char *));
358 void unterminated_text __P((int, char *, char *));
359 void unterminated_union __P((int, char *, char *));
360 void untyped_lhs __P((void));
361 void untyped_rhs __P((int, char *));
362 void used_reserved __P((char *));
363 void verbose __P((void));
364 void write_section __P((char **));