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