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