xref: /freebsd/contrib/byacc/defs.h (revision 6574b8ed19b093f0af09501d2c9676c28993cb97)
1 /* $Id: defs.h,v 1.49 2014/04/22 23:34:34 tom Exp $ */
2 
3 #ifdef HAVE_CONFIG_H
4 #include <config.h>
5 #endif
6 
7 #include <limits.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <errno.h>
11 #include <assert.h>
12 #include <ctype.h>
13 #include <stdio.h>
14 
15 #if defined(__cplusplus)	/* __cplusplus, etc. */
16 #define class myClass
17 #endif
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 
36 /*  MAXCHAR is the largest unsigned character value	*/
37 /*  MAXTABLE is the maximum table size			*/
38 /*  YYINT is the smallest C integer type that can be	*/
39 /*	used to address a table of size MAXTABLE	*/
40 /*  MAXYYINT is the largest value of a YYINT		*/
41 /*  MINYYINT is the most negative value of a YYINT	*/
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		UCHAR_MAX
50 #ifndef MAXTABLE
51 #define MAXTABLE	32500
52 #endif
53 #if MAXTABLE <= SHRT_MAX
54 #define YYINT		short
55 #define MAXYYINT	SHRT_MAX
56 #define MINYYINT	SHRT_MIN
57 #elif MAXTABLE <= INT_MAX
58 #define YYINT		int
59 #define MAXYYINT	INT_MAX
60 #define MINYYINT	INT_MIN
61 #else
62 #error "MAXTABLE is too large for this machine architecture!"
63 #endif
64 
65 #define BITS_PER_WORD	((int) sizeof (unsigned) * CHAR_BIT)
66 #define WORDSIZE(n)	(((n)+(BITS_PER_WORD-1))/BITS_PER_WORD)
67 #define BIT(r, n)	((((r)[(n)/BITS_PER_WORD])>>((n)&(BITS_PER_WORD-1)))&1)
68 #define SETBIT(r, n)	((r)[(n)/BITS_PER_WORD]|=((unsigned)1<<((n)&(BITS_PER_WORD-1))))
69 
70 /*  character names  */
71 
72 #define	NUL		'\0'	/*  the null character  */
73 #define	NEWLINE		'\n'	/*  line feed  */
74 #define	SP		' '	/*  space  */
75 #define	BS		'\b'	/*  backspace  */
76 #define	HT		'\t'	/*  horizontal tab  */
77 #define	VT		'\013'	/*  vertical tab  */
78 #define	CR		'\r'	/*  carriage return  */
79 #define	FF		'\f'	/*  form feed  */
80 #define	QUOTE		'\''	/*  single quote  */
81 #define	DOUBLE_QUOTE	'\"'	/*  double quote  */
82 #define	BACKSLASH	'\\'	/*  backslash  */
83 
84 #define UCH(c)          (unsigned char)(c)
85 
86 /* defines for constructing filenames */
87 
88 #if defined(VMS)
89 #define CODE_SUFFIX	"_code.c"
90 #define	DEFINES_SUFFIX	"_tab.h"
91 #define	EXTERNS_SUFFIX	"_tab.i"
92 #define	OUTPUT_SUFFIX	"_tab.c"
93 #else
94 #define CODE_SUFFIX	".code.c"
95 #define	DEFINES_SUFFIX	".tab.h"
96 #define	EXTERNS_SUFFIX	".tab.i"
97 #define	OUTPUT_SUFFIX	".tab.c"
98 #endif
99 #define	VERBOSE_SUFFIX	".output"
100 #define GRAPH_SUFFIX    ".dot"
101 
102 /* keyword codes */
103 
104 #define TOKEN 0
105 #define LEFT 1
106 #define RIGHT 2
107 #define NONASSOC 3
108 #define MARK 4
109 #define TEXT 5
110 #define TYPE 6
111 #define START 7
112 #define UNION 8
113 #define IDENT 9
114 #define EXPECT 10
115 #define EXPECT_RR 11
116 #define PURE_PARSER 12
117 #define PARSE_PARAM 13
118 #define LEX_PARAM 14
119 #define POSIX_YACC 15
120 #define TOKEN_TABLE 16
121 
122 #if defined(YYBTYACC)
123 #define LOCATIONS 17
124 #define DESTRUCTOR 18
125 #endif
126 
127 /*  symbol classes  */
128 
129 #define UNKNOWN 0
130 #define TERM 1
131 #define NONTERM 2
132 #define ACTION 3
133 #define ARGUMENT 4
134 
135 /*  the undefined value  */
136 
137 #define UNDEFINED (-1)
138 
139 /*  action codes  */
140 
141 #define SHIFT 1
142 #define REDUCE 2
143 
144 /*  character macros  */
145 
146 #define IS_IDENT(c)	(isalnum(c) || (c) == '_' || (c) == '.' || (c) == '$')
147 #define	IS_OCTAL(c)	((c) >= '0' && (c) <= '7')
148 #define	NUMERIC_VALUE(c)	((c) - '0')
149 
150 /*  symbol macros  */
151 
152 #define ISTOKEN(s)	((s) < start_symbol)
153 #define ISVAR(s)	((s) >= start_symbol)
154 
155 /*  storage allocation macros  */
156 
157 #define CALLOC(k,n)	(calloc((size_t)(k),(size_t)(n)))
158 #define	FREE(x)		(free((char*)(x)))
159 #define MALLOC(n)	(malloc((size_t)(n)))
160 #define TMALLOC(t,n)	((t*) malloc((size_t)(n) * sizeof(t)))
161 #define	NEW(t)		((t*)allocate(sizeof(t)))
162 #define	NEW2(n,t)	((t*)allocate(((size_t)(n)*sizeof(t))))
163 #define REALLOC(p,n)	(realloc((char*)(p),(size_t)(n)))
164 #define TREALLOC(t,p,n)	((t*)realloc((char*)(p), (size_t)(n) * sizeof(t)))
165 
166 #define DO_FREE(x)	if (x) { FREE(x); x = 0; }
167 
168 #define NO_SPACE(p)	if (p == 0) no_space(); assert(p != 0)
169 
170 /* messages */
171 #define PLURAL(n) ((n) > 1 ? "s" : "")
172 
173 /*
174  * Features which depend indirectly on the btyacc configuration, but are not
175  * essential.
176  */
177 #if defined(YYBTYACC)
178 #define USE_HEADER_GUARDS 1
179 #else
180 #define USE_HEADER_GUARDS 0
181 #endif
182 
183 typedef char Assoc_t;
184 typedef char Class_t;
185 typedef YYINT Index_t;
186 typedef YYINT Value_t;
187 
188 /*  the structure of a symbol table entry  */
189 
190 typedef struct bucket bucket;
191 struct bucket
192 {
193     struct bucket *link;
194     struct bucket *next;
195     char *name;
196     char *tag;
197 #if defined(YYBTYACC)
198     char **argnames;
199     char **argtags;
200     int  args;
201     char *destructor;
202 #endif
203     Value_t value;
204     Index_t index;
205     Value_t prec;
206     Class_t class;
207     Assoc_t assoc;
208 };
209 
210 /*  the structure of the LR(0) state machine  */
211 
212 typedef struct core core;
213 struct core
214 {
215     struct core *next;
216     struct core *link;
217     Value_t number;
218     Value_t accessing_symbol;
219     Value_t nitems;
220     Value_t items[1];
221 };
222 
223 /*  the structure used to record shifts  */
224 
225 typedef struct shifts shifts;
226 struct shifts
227 {
228     struct shifts *next;
229     Value_t number;
230     Value_t nshifts;
231     Value_t shift[1];
232 };
233 
234 /*  the structure used to store reductions  */
235 
236 typedef struct reductions reductions;
237 struct reductions
238 {
239     struct reductions *next;
240     Value_t number;
241     Value_t nreds;
242     Value_t rules[1];
243 };
244 
245 /*  the structure used to represent parser actions  */
246 
247 typedef struct action action;
248 struct action
249 {
250     struct action *next;
251     Value_t symbol;
252     Value_t number;
253     Value_t prec;
254     char action_code;
255     Assoc_t assoc;
256     char suppressed;
257 };
258 
259 /*  the structure used to store parse/lex parameters  */
260 typedef struct param param;
261 struct param
262 {
263     struct param *next;
264     char *name;		/* parameter name */
265     char *type;		/* everything before parameter name */
266     char *type2;	/* everything after parameter name */
267 };
268 
269 /* global variables */
270 
271 extern char dflag;
272 extern char gflag;
273 extern char iflag;
274 extern char lflag;
275 extern char rflag;
276 extern char sflag;
277 extern char tflag;
278 extern char vflag;
279 extern const char *symbol_prefix;
280 
281 extern const char *myname;
282 extern char *cptr;
283 extern char *line;
284 extern int lineno;
285 extern int outline;
286 extern int exit_code;
287 extern int pure_parser;
288 extern int token_table;
289 #if defined(YYBTYACC)
290 extern int locations;
291 extern int backtrack;
292 extern int destructor;
293 #endif
294 
295 extern const char *const banner[];
296 extern const char *const xdecls[];
297 extern const char *const tables[];
298 extern const char *const global_vars[];
299 extern const char *const impure_vars[];
300 extern const char *const hdr_defs[];
301 extern const char *const hdr_vars[];
302 extern const char *const body_1[];
303 extern const char *const body_vars[];
304 extern const char *const body_2[];
305 extern const char *const trailer[];
306 
307 extern char *code_file_name;
308 extern char *input_file_name;
309 extern char *defines_file_name;
310 extern char *externs_file_name;
311 
312 extern FILE *action_file;
313 extern FILE *code_file;
314 extern FILE *defines_file;
315 extern FILE *externs_file;
316 extern FILE *input_file;
317 extern FILE *output_file;
318 extern FILE *text_file;
319 extern FILE *union_file;
320 extern FILE *verbose_file;
321 extern FILE *graph_file;
322 
323 extern Value_t nitems;
324 extern Value_t nrules;
325 extern Value_t nsyms;
326 extern Value_t ntokens;
327 extern Value_t nvars;
328 extern int ntags;
329 
330 extern char unionized;
331 extern char line_format[];
332 
333 extern Value_t start_symbol;
334 extern char **symbol_name;
335 extern char **symbol_pname;
336 extern Value_t *symbol_value;
337 extern Value_t *symbol_prec;
338 extern char *symbol_assoc;
339 
340 #if defined(YYBTYACC)
341 extern Value_t *symbol_pval;
342 extern char **symbol_destructor;
343 extern char **symbol_type_tag;
344 #endif
345 
346 extern Value_t *ritem;
347 extern Value_t *rlhs;
348 extern Value_t *rrhs;
349 extern Value_t *rprec;
350 extern Assoc_t *rassoc;
351 
352 extern Value_t **derives;
353 extern char *nullable;
354 
355 extern bucket *first_symbol;
356 extern bucket *last_symbol;
357 
358 extern int nstates;
359 extern core *first_state;
360 extern shifts *first_shift;
361 extern reductions *first_reduction;
362 extern Value_t *accessing_symbol;
363 extern core **state_table;
364 extern shifts **shift_table;
365 extern reductions **reduction_table;
366 extern unsigned *LA;
367 extern Value_t *LAruleno;
368 extern Value_t *lookaheads;
369 extern Value_t *goto_map;
370 extern Value_t *from_state;
371 extern Value_t *to_state;
372 
373 extern action **parser;
374 extern int SRexpect;
375 extern int RRexpect;
376 extern int SRtotal;
377 extern int RRtotal;
378 extern Value_t *SRconflicts;
379 extern Value_t *RRconflicts;
380 extern Value_t *defred;
381 extern Value_t *rules_used;
382 extern Value_t nunused;
383 extern Value_t final_state;
384 
385 extern Value_t *itemset;
386 extern Value_t *itemsetend;
387 extern unsigned *ruleset;
388 
389 extern param *lex_param;
390 extern param *parse_param;
391 
392 /* global functions */
393 
394 #ifndef GCC_NORETURN
395 #if defined(__dead2)
396 #define GCC_NORETURN		__dead2
397 #elif defined(__dead)
398 #define GCC_NORETURN		__dead
399 #else
400 #define GCC_NORETURN		/* nothing */
401 #endif
402 #endif
403 
404 #ifndef GCC_UNUSED
405 #if defined(__unused)
406 #define GCC_UNUSED		__unused
407 #else
408 #define GCC_UNUSED		/* nothing */
409 #endif
410 #endif
411 
412 #ifndef GCC_PRINTFLIKE
413 #define GCC_PRINTFLIKE(fmt,var) /*nothing*/
414 #endif
415 
416 /* closure.c */
417 extern void closure(Value_t * nucleus, int n);
418 extern void finalize_closure(void);
419 extern void set_first_derives(void);
420 
421 /* error.c */
422 extern void arg_number_disagree_warning(int a_lineno, char *a_name);
423 extern void arg_type_disagree_warning(int a_lineno, int i, char *a_name);
424 extern void at_error(int a_lineno, char *a_line, char *a_cptr) GCC_NORETURN;
425 extern void at_warning(int a_lineno, int i);
426 extern void bad_formals(void) GCC_NORETURN;
427 extern void default_action_warning(void);
428 extern void destructor_redeclared_warning(int a_lineno, char *a_line, char *a_cptr);
429 extern void dollar_error(int a_lineno, char *a_line, char *a_cptr) GCC_NORETURN;
430 extern void dollar_warning(int a_lineno, int i);
431 extern void fatal(const char *msg) GCC_NORETURN;
432 extern void illegal_character(char *c_cptr) GCC_NORETURN;
433 extern void illegal_tag(int t_lineno, char *t_line, char *t_cptr) GCC_NORETURN;
434 extern void missing_brace(void) GCC_NORETURN;
435 extern void no_grammar(void) GCC_NORETURN;
436 extern void no_space(void) GCC_NORETURN;
437 extern void open_error(const char *filename) GCC_NORETURN;
438 extern void over_unionized(char *u_cptr) GCC_NORETURN;
439 extern void prec_redeclared(void);
440 extern void reprec_warning(char *s);
441 extern void restarted_warning(void);
442 extern void retyped_warning(char *s);
443 extern void revalued_warning(char *s);
444 extern void start_requires_args(char *a_name);
445 extern void syntax_error(int st_lineno, char *st_line, char *st_cptr) GCC_NORETURN;
446 extern void terminal_lhs(int s_lineno) GCC_NORETURN;
447 extern void terminal_start(char *s) GCC_NORETURN;
448 extern void tokenized_start(char *s) GCC_NORETURN;
449 extern void undefined_goal(char *s) GCC_NORETURN;
450 extern void undefined_symbol_warning(char *s);
451 extern void unexpected_EOF(void) GCC_NORETURN;
452 extern void unknown_arg_warning(int d_lineno, const char *dlr_opt, const char *d_arg, const char *d_line, const char *d_cptr);
453 extern void unknown_rhs(int i) GCC_NORETURN;
454 extern void unsupported_flag_warning(const char *flag, const char *details);
455 extern void unterminated_action(int a_lineno, char *a_line, char *a_cptr) GCC_NORETURN;
456 extern void unterminated_comment(int c_lineno, char *c_line, char *c_cptr) GCC_NORETURN;
457 extern void unterminated_string(int s_lineno, char *s_line, char *s_cptr) GCC_NORETURN;
458 extern void unterminated_text(int t_lineno, char *t_line, char *t_cptr) GCC_NORETURN;
459 extern void unterminated_union(int u_lineno, char *u_line, char *u_cptr) GCC_NORETURN;
460 extern void untyped_arg_warning(int a_lineno, const char *dlr_opt, const char *a_name);
461 extern void untyped_lhs(void) GCC_NORETURN;
462 extern void untyped_rhs(int i, char *s) GCC_NORETURN;
463 extern void used_reserved(char *s) GCC_NORETURN;
464 extern void unterminated_arglist(int a_lineno, char *a_line, char *a_cptr) GCC_NORETURN;
465 extern void wrong_number_args_warning(const char *which, const char *a_name);
466 extern void wrong_type_for_arg_warning(int i, char *a_name);
467 
468 /* graph.c */
469 extern void graph(void);
470 
471 /* lalr.c */
472 extern void lalr(void);
473 
474 /* lr0.c */
475 extern void lr0(void);
476 extern void show_cores(void);
477 extern void show_ritems(void);
478 extern void show_rrhs(void);
479 extern void show_shifts(void);
480 
481 /* main.c */
482 extern void *allocate(size_t n);
483 extern void done(int k) GCC_NORETURN;
484 
485 /* mkpar.c */
486 extern void free_parser(void);
487 extern void make_parser(void);
488 
489 /* mstring.c */
490 struct mstring
491 {
492     char *base, *ptr, *end;
493 };
494 
495 extern void msprintf(struct mstring *, const char *, ...) GCC_PRINTFLIKE(2,3);
496 extern int mputchar(struct mstring *, int);
497 extern struct mstring *msnew(void);
498 extern char *msdone(struct mstring *);
499 extern int strnscmp(const char *, const char *);
500 extern unsigned int strnshash(const char *);
501 
502 #define mputc(m, ch)	(((m)->ptr == (m)->end) \
503 			 ? mputchar(m,ch) \
504 			 : (*(m)->ptr++ = (char) (ch)))
505 
506 /* output.c */
507 extern void output(void);
508 
509 /* reader.c */
510 extern void reader(void);
511 
512 /* skeleton.c (generated by skel2c) */
513 extern void write_section(FILE * fp, const char *const section[]);
514 
515 /* symtab.c */
516 extern bucket *make_bucket(const char *);
517 extern bucket *lookup(const char *);
518 extern void create_symbol_table(void);
519 extern void free_symbol_table(void);
520 extern void free_symbols(void);
521 
522 /* verbose.c */
523 extern void verbose(void);
524 
525 /* warshall.c */
526 extern void reflexive_transitive_closure(unsigned *R, int n);
527 
528 #ifdef DEBUG
529     /* closure.c */
530 extern void print_closure(int n);
531 extern void print_EFF(void);
532 extern void print_first_derives(void);
533     /* lr0.c */
534 extern void print_derives(void);
535 #endif
536 
537 #ifdef NO_LEAKS
538 extern void lr0_leaks(void);
539 extern void lalr_leaks(void);
540 extern void mkpar_leaks(void);
541 extern void output_leaks(void);
542 extern void mstring_leaks(void);
543 extern void reader_leaks(void);
544 #endif
545