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