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