xref: /netbsd/external/bsd/byacc/dist/defs.h (revision 6550d01e)
1 /*	$NetBSD: defs.h,v 1.4 2010/12/24 02:58:20 christos Exp $	*/
2 
3 #if HAVE_NBTOOL_CONFIG_H
4 #include "nbtool_config.h"
5 #endif
6 /* Id: defs.h,v 1.30 2010/11/26 15:19:36 tom Exp */
7 
8 #ifdef HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11 
12 #include <stdlib.h>
13 #include <string.h>
14 #include <errno.h>
15 #include <assert.h>
16 #include <ctype.h>
17 #include <stdio.h>
18 
19 #define YYMAJOR 1
20 #define YYMINOR 9
21 
22 #define CONCAT(first,second)    first #second
23 #define CONCAT1(string,number)  CONCAT(string, number)
24 #define CONCAT2(first,second)   #first "." #second
25 
26 #ifdef YYPATCH
27 #define VSTRING(a,b) CONCAT2(a,b) CONCAT1(" ",YYPATCH)
28 #else
29 #define VSTRING(a,b) CONCAT2(a,b)
30 #endif
31 
32 #define VERSION VSTRING(YYMAJOR, YYMINOR)
33 
34 /*  machine-dependent definitions			*/
35 /*  the following definitions are for the Tahoe		*/
36 /*  they might have to be changed for other machines	*/
37 
38 /*  MAXCHAR is the largest unsigned character value	*/
39 /*  MAXSHORT is the largest value of a C short		*/
40 /*  MINSHORT is the most negative value of a C short	*/
41 /*  MAXTABLE is the maximum table size			*/
42 /*  BITS_PER_WORD is the number of bits in a C unsigned	*/
43 /*  WORDSIZE computes the number of words needed to	*/
44 /*	store n bits					*/
45 /*  BIT returns the value of the n-th bit starting	*/
46 /*	from r (0-indexed)				*/
47 /*  SETBIT sets the n-th bit starting from r		*/
48 
49 #define	MAXCHAR		255
50 #define	MAXSHORT	32767
51 #define MINSHORT	-32768
52 #define MAXTABLE	32500
53 #define BITS_PER_WORD	32
54 #define	WORDSIZE(n)	(((n)+(BITS_PER_WORD-1))/BITS_PER_WORD)
55 #define	BIT(r, n)	((((r)[(n)>>5])>>((n)&31))&1)
56 #define	SETBIT(r, n)	((r)[(n)>>5]|=((unsigned)1<<((n)&31)))
57 
58 /*  character names  */
59 
60 #define	NUL		'\0'	/*  the null character  */
61 #define	NEWLINE		'\n'	/*  line feed  */
62 #define	SP		' '	/*  space  */
63 #define	BS		'\b'	/*  backspace  */
64 #define	HT		'\t'	/*  horizontal tab  */
65 #define	VT		'\013'	/*  vertical tab  */
66 #define	CR		'\r'	/*  carriage return  */
67 #define	FF		'\f'	/*  form feed  */
68 #define	QUOTE		'\''	/*  single quote  */
69 #define	DOUBLE_QUOTE	'\"'	/*  double quote  */
70 #define	BACKSLASH	'\\'	/*  backslash  */
71 
72 #define UCH(c)          (unsigned char)(c)
73 
74 /* defines for constructing filenames */
75 
76 #if defined(VMS)
77 #define CODE_SUFFIX	"_code.c"
78 #define	DEFINES_SUFFIX	"_tab.h"
79 #define	OUTPUT_SUFFIX	"_tab.c"
80 #else
81 #define CODE_SUFFIX	".code.c"
82 #define	DEFINES_SUFFIX	".tab.h"
83 #define	OUTPUT_SUFFIX	".tab.c"
84 #endif
85 #define	VERBOSE_SUFFIX	".output"
86 #define GRAPH_SUFFIX    ".dot"
87 
88 /* keyword codes */
89 
90 #define TOKEN 0
91 #define LEFT 1
92 #define RIGHT 2
93 #define NONASSOC 3
94 #define MARK 4
95 #define TEXT 5
96 #define TYPE 6
97 #define START 7
98 #define UNION 8
99 #define IDENT 9
100 #define EXPECT 10
101 #define EXPECT_RR 11
102 #define PURE_PARSER 12
103 #define PARSE_PARAM 13
104 #define LEX_PARAM 14
105 #define POSIX_YACC 15
106 
107 /*  symbol classes  */
108 
109 #define UNKNOWN 0
110 #define TERM 1
111 #define NONTERM 2
112 
113 /*  the undefined value  */
114 
115 #define UNDEFINED (-1)
116 
117 /*  action codes  */
118 
119 #define SHIFT 1
120 #define REDUCE 2
121 
122 /*  character macros  */
123 
124 #define IS_IDENT(c)	(isalnum(c) || (c) == '_' || (c) == '.' || (c) == '$')
125 #define	IS_OCTAL(c)	((c) >= '0' && (c) <= '7')
126 #define	NUMERIC_VALUE(c)	((c) - '0')
127 
128 /*  symbol macros  */
129 
130 #define ISTOKEN(s)	((s) < start_symbol)
131 #define ISVAR(s)	((s) >= start_symbol)
132 
133 /*  storage allocation macros  */
134 
135 #define CALLOC(k,n)	(calloc((size_t)(k),(size_t)(n)))
136 #define	FREE(x)		(free((char*)(x)))
137 #define MALLOC(n)	(malloc((size_t)(n)))
138 #define	NEW(t)		((t*)allocate(sizeof(t)))
139 #define	NEW2(n,t)	((t*)allocate(((size_t)(n)*sizeof(t))))
140 #define REALLOC(p,n)	(realloc((char*)(p),(size_t)(n)))
141 
142 #define DO_FREE(x)	if (x) { FREE(x); x = 0; }
143 
144 #define NO_SPACE(p)	if (p == 0) no_space(); assert(p != 0)
145 
146 /* messages */
147 #define PLURAL(n) ((n) > 1 ? "s" : "")
148 
149 typedef char Assoc_t;
150 typedef char Class_t;
151 typedef short Index_t;
152 typedef short Value_t;
153 
154 /*  the structure of a symbol table entry  */
155 
156 typedef struct bucket bucket;
157 struct bucket
158 {
159     struct bucket *link;
160     struct bucket *next;
161     char *name;
162     char *tag;
163     Value_t value;
164     Index_t index;
165     Value_t prec;
166     Class_t class;
167     Assoc_t assoc;
168 };
169 
170 /*  the structure of the LR(0) state machine  */
171 
172 typedef struct core core;
173 struct core
174 {
175     struct core *next;
176     struct core *link;
177     Value_t number;
178     Value_t accessing_symbol;
179     Value_t nitems;
180     Value_t items[1];
181 };
182 
183 /*  the structure used to record shifts  */
184 
185 typedef struct shifts shifts;
186 struct shifts
187 {
188     struct shifts *next;
189     Value_t number;
190     Value_t nshifts;
191     Value_t shift[1];
192 };
193 
194 /*  the structure used to store reductions  */
195 
196 typedef struct reductions reductions;
197 struct reductions
198 {
199     struct reductions *next;
200     Value_t number;
201     Value_t nreds;
202     Value_t rules[1];
203 };
204 
205 /*  the structure used to represent parser actions  */
206 
207 typedef struct action action;
208 struct action
209 {
210     struct action *next;
211     Value_t symbol;
212     Value_t number;
213     Value_t prec;
214     char action_code;
215     Assoc_t assoc;
216     char suppressed;
217 };
218 
219 /*  the structure used to store parse/lex parameters  */
220 typedef struct param param;
221 struct param
222 {
223     struct param *next;
224     char *name;		/* parameter name */
225     char *type;		/* everything before parameter name */
226     char *type2;	/* everything after parameter name */
227 };
228 
229 /* global variables */
230 
231 extern char dflag;
232 extern char gflag;
233 extern char lflag;
234 extern char rflag;
235 extern char tflag;
236 extern char vflag;
237 extern const char *symbol_prefix;
238 
239 extern const char *myname;
240 extern char *cptr;
241 extern char *line;
242 extern int lineno;
243 extern int outline;
244 extern int exit_code;
245 extern int pure_parser;
246 
247 extern const char * const banner[];
248 extern const char * const xdecls[];
249 extern const char * const tables[];
250 extern const char * const hdr_defs[];
251 extern const char * const hdr_vars[];
252 extern const char * const body_1[];
253 extern const char * const body_vars[];
254 extern const char * const body_2[];
255 extern const char * const body_3[];
256 extern const char * const trailer[];
257 extern const char * const trailer_2[];
258 
259 extern char *code_file_name;
260 extern char *input_file_name;
261 
262 extern FILE *action_file;
263 extern FILE *code_file;
264 extern FILE *defines_file;
265 extern FILE *input_file;
266 extern FILE *output_file;
267 extern FILE *text_file;
268 extern FILE *union_file;
269 extern FILE *verbose_file;
270 extern FILE *graph_file;
271 
272 extern int nitems;
273 extern int nrules;
274 extern int nsyms;
275 extern int ntokens;
276 extern int nvars;
277 extern int ntags;
278 
279 extern char unionized;
280 extern char line_format[];
281 
282 extern Value_t start_symbol;
283 extern char **symbol_name;
284 extern char **symbol_pname;
285 extern Value_t *symbol_value;
286 extern Value_t *symbol_prec;
287 extern char *symbol_assoc;
288 
289 extern Value_t *ritem;
290 extern Value_t *rlhs;
291 extern Value_t *rrhs;
292 extern Value_t *rprec;
293 extern Assoc_t *rassoc;
294 
295 extern Value_t **derives;
296 extern char *nullable;
297 
298 extern bucket *first_symbol;
299 extern bucket *last_symbol;
300 
301 extern int pure_parser;
302 extern int nstates;
303 extern core *first_state;
304 extern shifts *first_shift;
305 extern reductions *first_reduction;
306 extern Value_t *accessing_symbol;
307 extern core **state_table;
308 extern shifts **shift_table;
309 extern reductions **reduction_table;
310 extern unsigned *LA;
311 extern Value_t *LAruleno;
312 extern Value_t *lookaheads;
313 extern Value_t *goto_map;
314 extern Value_t *from_state;
315 extern Value_t *to_state;
316 
317 extern action **parser;
318 extern int SRexpect;
319 extern int RRexpect;
320 extern int SRtotal;
321 extern int RRtotal;
322 extern Value_t *SRconflicts;
323 extern Value_t *RRconflicts;
324 extern Value_t *defred;
325 extern Value_t *rules_used;
326 extern Value_t nunused;
327 extern Value_t final_state;
328 
329 extern Value_t *itemset;
330 extern Value_t *itemsetend;
331 extern unsigned *ruleset;
332 
333 extern param *lex_param;
334 extern param *parse_param;
335 
336 /* global functions */
337 
338 extern bucket *lookup(const char *);
339 extern bucket *make_bucket(const char *);
340 
341 #ifndef GCC_NORETURN
342 #define GCC_NORETURN		/* nothing */
343 #endif
344 
345 #ifndef GCC_UNUSED
346 #define GCC_UNUSED		/* nothing */
347 #endif
348 
349 /* closure.c */
350 extern void closure(Value_t * nucleus, int n);
351 extern void finalize_closure(void);
352 extern void set_first_derives(void);
353 
354 /* error.c */
355 extern void default_action_warning(void);
356 extern void dollar_error(int a_lineno, char *a_line, char *a_cptr);
357 extern void dollar_warning(int a_lineno, int i);
358 extern void fatal(const char *msg);
359 extern void illegal_character(char *c_cptr);
360 extern void illegal_tag(int t_lineno, char *t_line, char *t_cptr);
361 extern void no_grammar(void);
362 extern void no_space(void);
363 extern void open_error(const char *filename);
364 extern void over_unionized(char *u_cptr);
365 extern void prec_redeclared(void);
366 extern void reprec_warning(char *s);
367 extern void restarted_warning(void);
368 extern void retyped_warning(char *s);
369 extern void revalued_warning(char *s);
370 extern __dead void syntax_error(int st_lineno, char *st_line, char *st_cptr) GCC_NORETURN;
371 extern void terminal_lhs(int s_lineno);
372 extern void terminal_start(char *s);
373 extern void tokenized_start(char *s);
374 extern void undefined_goal(char *s);
375 extern void undefined_symbol_warning(char *s);
376 extern void unexpected_EOF(void);
377 extern void unknown_rhs(int i);
378 extern void unterminated_action(int a_lineno, char *a_line, char *a_cptr);
379 extern void unterminated_comment(int c_lineno, char *c_line, char *c_cptr);
380 extern void unterminated_string(int s_lineno, char *s_line, char *s_cptr);
381 extern void unterminated_text(int t_lineno, char *t_line, char *t_cptr);
382 extern void unterminated_union(int u_lineno, char *u_line, char *u_cptr);
383 extern void untyped_lhs(void);
384 extern void untyped_rhs(int i, char *s);
385 extern void used_reserved(char *s);
386 
387 /* graph.c */
388 extern void graph(void);
389 
390 /* lalr.c */
391 extern void create_symbol_table(void);
392 extern void free_symbol_table(void);
393 extern void free_symbols(void);
394 
395 /* lalr.c */
396 extern void lalr(void);
397 
398 /* lr0.c */
399 extern void lr0(void);
400 extern void show_cores(void);
401 extern void show_ritems(void);
402 extern void show_rrhs(void);
403 extern void show_shifts(void);
404 
405 /* main.c */
406 extern char *allocate(size_t n);
407 extern void done(int k) GCC_NORETURN;
408 
409 /* mkpar.c */
410 extern void free_parser(void);
411 extern void make_parser(void);
412 
413 /* output.c */
414 extern void output(void);
415 
416 /* reader.c */
417 extern void reader(void);
418 
419 /* skeleton.c */
420 extern void write_section(const char * const section[]);
421 
422 /* verbose.c */
423 extern void verbose(void);
424 
425 /* warshall.c */
426 extern void reflexive_transitive_closure(unsigned *R, int n);
427 
428 #ifdef NO_LEAKS
429 extern void lr0_leaks(void);
430 extern void lalr_leaks(void);
431 extern void mkpar_leaks(void);
432 extern void output_leaks(void);
433 extern void reader_leaks(void);
434 #endif
435