xref: /freebsd/contrib/byacc/defs.h (revision 4abd7edcbde21ba7a089c7d1a0bba8f87ebece06)
1 /* $Id: defs.h,v 1.37 2012/05/26 15:23:00 tom Exp $ */
2 
3 #ifdef HAVE_CONFIG_H
4 #include <config.h>
5 #endif
6 
7 #include <stdlib.h>
8 #include <string.h>
9 #include <errno.h>
10 #include <assert.h>
11 #include <ctype.h>
12 #include <stdio.h>
13 
14 #if defined(__cplusplus)	/* __cplusplus, etc. */
15 #define class myClass
16 #endif
17 
18 #define YYMAJOR 1
19 #define YYMINOR 9
20 
21 #define CONCAT(first,second)    first #second
22 #define CONCAT1(string,number)  CONCAT(string, number)
23 #define CONCAT2(first,second)   #first "." #second
24 
25 #ifdef YYPATCH
26 #define VSTRING(a,b) CONCAT2(a,b) CONCAT1(" ",YYPATCH)
27 #else
28 #define VSTRING(a,b) CONCAT2(a,b)
29 #endif
30 
31 #define VERSION VSTRING(YYMAJOR, YYMINOR)
32 
33 /*  machine-dependent definitions			*/
34 /*  the following definitions are for the Tahoe		*/
35 /*  they might have to be changed for other machines	*/
36 
37 /*  MAXCHAR is the largest unsigned character value	*/
38 /*  MAXSHORT is the largest value of a C short		*/
39 /*  MINSHORT is the most negative value of a C short	*/
40 /*  MAXTABLE is the maximum table size			*/
41 /*  BITS_PER_WORD is the number of bits in a C unsigned	*/
42 /*  WORDSIZE computes the number of words needed to	*/
43 /*	store n bits					*/
44 /*  BIT returns the value of the n-th bit starting	*/
45 /*	from r (0-indexed)				*/
46 /*  SETBIT sets the n-th bit starting from r		*/
47 
48 #define	MAXCHAR		255
49 #define	MAXSHORT	32767
50 #define MINSHORT	-32768
51 #define MAXTABLE	32500
52 #define BITS_PER_WORD	32
53 #define	WORDSIZE(n)	(((n)+(BITS_PER_WORD-1))/BITS_PER_WORD)
54 #define	BIT(r, n)	((((r)[(n)>>5])>>((n)&31))&1)
55 #define	SETBIT(r, n)	((r)[(n)>>5]|=((unsigned)1<<((n)&31)))
56 
57 /*  character names  */
58 
59 #define	NUL		'\0'	/*  the null character  */
60 #define	NEWLINE		'\n'	/*  line feed  */
61 #define	SP		' '	/*  space  */
62 #define	BS		'\b'	/*  backspace  */
63 #define	HT		'\t'	/*  horizontal tab  */
64 #define	VT		'\013'	/*  vertical tab  */
65 #define	CR		'\r'	/*  carriage return  */
66 #define	FF		'\f'	/*  form feed  */
67 #define	QUOTE		'\''	/*  single quote  */
68 #define	DOUBLE_QUOTE	'\"'	/*  double quote  */
69 #define	BACKSLASH	'\\'	/*  backslash  */
70 
71 #define UCH(c)          (unsigned char)(c)
72 
73 /* defines for constructing filenames */
74 
75 #if defined(VMS)
76 #define CODE_SUFFIX	"_code.c"
77 #define	DEFINES_SUFFIX	"_tab.h"
78 #define	EXTERNS_SUFFIX	"_tab.i"
79 #define	OUTPUT_SUFFIX	"_tab.c"
80 #else
81 #define CODE_SUFFIX	".code.c"
82 #define	DEFINES_SUFFIX	".tab.h"
83 #define	EXTERNS_SUFFIX	".tab.i"
84 #define	OUTPUT_SUFFIX	".tab.c"
85 #endif
86 #define	VERBOSE_SUFFIX	".output"
87 #define GRAPH_SUFFIX    ".dot"
88 
89 /* keyword codes */
90 
91 #define TOKEN 0
92 #define LEFT 1
93 #define RIGHT 2
94 #define NONASSOC 3
95 #define MARK 4
96 #define TEXT 5
97 #define TYPE 6
98 #define START 7
99 #define UNION 8
100 #define IDENT 9
101 #define EXPECT 10
102 #define EXPECT_RR 11
103 #define PURE_PARSER 12
104 #define PARSE_PARAM 13
105 #define LEX_PARAM 14
106 #define POSIX_YACC 15
107 
108 /*  symbol classes  */
109 
110 #define UNKNOWN 0
111 #define TERM 1
112 #define NONTERM 2
113 
114 /*  the undefined value  */
115 
116 #define UNDEFINED (-1)
117 
118 /*  action codes  */
119 
120 #define SHIFT 1
121 #define REDUCE 2
122 
123 /*  character macros  */
124 
125 #define IS_IDENT(c)	(isalnum(c) || (c) == '_' || (c) == '.' || (c) == '$')
126 #define	IS_OCTAL(c)	((c) >= '0' && (c) <= '7')
127 #define	NUMERIC_VALUE(c)	((c) - '0')
128 
129 /*  symbol macros  */
130 
131 #define ISTOKEN(s)	((s) < start_symbol)
132 #define ISVAR(s)	((s) >= start_symbol)
133 
134 /*  storage allocation macros  */
135 
136 #define CALLOC(k,n)	(calloc((size_t)(k),(size_t)(n)))
137 #define	FREE(x)		(free((char*)(x)))
138 #define MALLOC(n)	(malloc((size_t)(n)))
139 #define TMALLOC(t,n)	((t*) malloc((size_t)(n) * sizeof(t)))
140 #define	NEW(t)		((t*)allocate(sizeof(t)))
141 #define	NEW2(n,t)	((t*)allocate(((size_t)(n)*sizeof(t))))
142 #define REALLOC(p,n)	(realloc((char*)(p),(size_t)(n)))
143 #define TREALLOC(t,p,n)	((t*)realloc((char*)(p), (size_t)(n) * sizeof(t)))
144 
145 #define DO_FREE(x)	if (x) { FREE(x); x = 0; }
146 
147 #define NO_SPACE(p)	if (p == 0) no_space(); assert(p != 0)
148 
149 /* messages */
150 #define PLURAL(n) ((n) > 1 ? "s" : "")
151 
152 typedef char Assoc_t;
153 typedef char Class_t;
154 typedef short Index_t;
155 typedef short Value_t;
156 
157 /*  the structure of a symbol table entry  */
158 
159 typedef struct bucket bucket;
160 struct bucket
161 {
162     struct bucket *link;
163     struct bucket *next;
164     char *name;
165     char *tag;
166     Value_t value;
167     Index_t index;
168     Value_t prec;
169     Class_t class;
170     Assoc_t assoc;
171 };
172 
173 /*  the structure of the LR(0) state machine  */
174 
175 typedef struct core core;
176 struct core
177 {
178     struct core *next;
179     struct core *link;
180     Value_t number;
181     Value_t accessing_symbol;
182     Value_t nitems;
183     Value_t items[1];
184 };
185 
186 /*  the structure used to record shifts  */
187 
188 typedef struct shifts shifts;
189 struct shifts
190 {
191     struct shifts *next;
192     Value_t number;
193     Value_t nshifts;
194     Value_t shift[1];
195 };
196 
197 /*  the structure used to store reductions  */
198 
199 typedef struct reductions reductions;
200 struct reductions
201 {
202     struct reductions *next;
203     Value_t number;
204     Value_t nreds;
205     Value_t rules[1];
206 };
207 
208 /*  the structure used to represent parser actions  */
209 
210 typedef struct action action;
211 struct action
212 {
213     struct action *next;
214     Value_t symbol;
215     Value_t number;
216     Value_t prec;
217     char action_code;
218     Assoc_t assoc;
219     char suppressed;
220 };
221 
222 /*  the structure used to store parse/lex parameters  */
223 typedef struct param param;
224 struct param
225 {
226     struct param *next;
227     char *name;		/* parameter name */
228     char *type;		/* everything before parameter name */
229     char *type2;	/* everything after parameter name */
230 };
231 
232 /* global variables */
233 
234 extern char dflag;
235 extern char gflag;
236 extern char iflag;
237 extern char lflag;
238 extern char rflag;
239 extern char sflag;
240 extern char tflag;
241 extern char vflag;
242 extern const char *symbol_prefix;
243 
244 extern const char *myname;
245 extern char *cptr;
246 extern char *line;
247 extern int lineno;
248 extern int outline;
249 extern int exit_code;
250 extern int pure_parser;
251 
252 extern const char *const banner[];
253 extern const char *const xdecls[];
254 extern const char *const tables[];
255 extern const char *const global_vars[];
256 extern const char *const impure_vars[];
257 extern const char *const hdr_defs[];
258 extern const char *const hdr_vars[];
259 extern const char *const body_1[];
260 extern const char *const body_vars[];
261 extern const char *const body_2[];
262 extern const char *const body_3[];
263 extern const char *const trailer[];
264 extern const char *const trailer_2[];
265 
266 extern char *code_file_name;
267 extern char *input_file_name;
268 extern char *defines_file_name;
269 extern char *externs_file_name;
270 
271 extern FILE *action_file;
272 extern FILE *code_file;
273 extern FILE *defines_file;
274 extern FILE *externs_file;
275 extern FILE *input_file;
276 extern FILE *output_file;
277 extern FILE *text_file;
278 extern FILE *union_file;
279 extern FILE *verbose_file;
280 extern FILE *graph_file;
281 
282 extern int nitems;
283 extern int nrules;
284 extern int nsyms;
285 extern int ntokens;
286 extern int nvars;
287 extern int ntags;
288 
289 extern char unionized;
290 extern char line_format[];
291 
292 extern Value_t start_symbol;
293 extern char **symbol_name;
294 extern char **symbol_pname;
295 extern Value_t *symbol_value;
296 extern Value_t *symbol_prec;
297 extern char *symbol_assoc;
298 
299 extern Value_t *ritem;
300 extern Value_t *rlhs;
301 extern Value_t *rrhs;
302 extern Value_t *rprec;
303 extern Assoc_t *rassoc;
304 
305 extern Value_t **derives;
306 extern char *nullable;
307 
308 extern bucket *first_symbol;
309 extern bucket *last_symbol;
310 
311 extern int nstates;
312 extern core *first_state;
313 extern shifts *first_shift;
314 extern reductions *first_reduction;
315 extern Value_t *accessing_symbol;
316 extern core **state_table;
317 extern shifts **shift_table;
318 extern reductions **reduction_table;
319 extern unsigned *LA;
320 extern Value_t *LAruleno;
321 extern Value_t *lookaheads;
322 extern Value_t *goto_map;
323 extern Value_t *from_state;
324 extern Value_t *to_state;
325 
326 extern action **parser;
327 extern int SRexpect;
328 extern int RRexpect;
329 extern int SRtotal;
330 extern int RRtotal;
331 extern Value_t *SRconflicts;
332 extern Value_t *RRconflicts;
333 extern Value_t *defred;
334 extern Value_t *rules_used;
335 extern Value_t nunused;
336 extern Value_t final_state;
337 
338 extern Value_t *itemset;
339 extern Value_t *itemsetend;
340 extern unsigned *ruleset;
341 
342 extern param *lex_param;
343 extern param *parse_param;
344 
345 /* global functions */
346 
347 extern bucket *lookup(const char *);
348 extern bucket *make_bucket(const char *);
349 
350 #ifndef GCC_NORETURN
351 #if defined(__dead2)
352 #define GCC_NORETURN		__dead2
353 #elif defined(__dead)
354 #define GCC_NORETURN		__dead
355 #else
356 #define GCC_NORETURN		/* nothing */
357 #endif
358 #endif
359 
360 #ifndef GCC_UNUSED
361 #if defined(__unused)
362 #define GCC_UNUSED		__unused
363 #else
364 #define GCC_UNUSED		/* nothing */
365 #endif
366 #endif
367 
368 /* closure.c */
369 extern void closure(Value_t * nucleus, int n);
370 extern void finalize_closure(void);
371 extern void set_first_derives(void);
372 
373 /* error.c */
374 extern void default_action_warning(void);
375 extern void dollar_error(int a_lineno, char *a_line, char *a_cptr) GCC_NORETURN;
376 extern void dollar_warning(int a_lineno, int i);
377 extern void fatal(const char *msg) GCC_NORETURN;
378 extern void illegal_character(char *c_cptr) GCC_NORETURN;
379 extern void illegal_tag(int t_lineno, char *t_line, char *t_cptr) GCC_NORETURN;
380 extern void missing_brace(void) GCC_NORETURN;
381 extern void no_grammar(void) GCC_NORETURN;
382 extern void no_space(void) GCC_NORETURN;
383 extern void open_error(const char *filename) GCC_NORETURN;
384 extern void over_unionized(char *u_cptr) GCC_NORETURN;
385 extern void prec_redeclared(void);
386 extern void reprec_warning(char *s);
387 extern void restarted_warning(void);
388 extern void retyped_warning(char *s);
389 extern void revalued_warning(char *s);
390 extern void syntax_error(int st_lineno, char *st_line, char *st_cptr) GCC_NORETURN;
391 extern void terminal_lhs(int s_lineno) GCC_NORETURN;
392 extern void terminal_start(char *s) GCC_NORETURN;
393 extern void tokenized_start(char *s) GCC_NORETURN;
394 extern void undefined_goal(char *s) GCC_NORETURN;
395 extern void undefined_symbol_warning(char *s);
396 extern void unexpected_EOF(void) GCC_NORETURN;
397 extern void unknown_rhs(int i) GCC_NORETURN;
398 extern void unterminated_action(int a_lineno, char *a_line, char *a_cptr) GCC_NORETURN;
399 extern void unterminated_comment(int c_lineno, char *c_line, char *c_cptr) GCC_NORETURN;
400 extern void unterminated_string(int s_lineno, char *s_line, char *s_cptr) GCC_NORETURN;
401 extern void unterminated_text(int t_lineno, char *t_line, char *t_cptr) GCC_NORETURN;
402 extern void unterminated_union(int u_lineno, char *u_line, char *u_cptr) GCC_NORETURN;
403 extern void untyped_lhs(void) GCC_NORETURN;
404 extern void untyped_rhs(int i, char *s) GCC_NORETURN;
405 extern void used_reserved(char *s) GCC_NORETURN;
406 
407 /* graph.c */
408 extern void graph(void);
409 
410 /* lalr.c */
411 extern void create_symbol_table(void);
412 extern void free_symbol_table(void);
413 extern void free_symbols(void);
414 
415 /* lalr.c */
416 extern void lalr(void);
417 
418 /* lr0.c */
419 extern void lr0(void);
420 extern void show_cores(void);
421 extern void show_ritems(void);
422 extern void show_rrhs(void);
423 extern void show_shifts(void);
424 
425 /* main.c */
426 extern void *allocate(size_t n);
427 extern void done(int k) GCC_NORETURN;
428 
429 /* mkpar.c */
430 extern void free_parser(void);
431 extern void make_parser(void);
432 
433 /* output.c */
434 extern void output(void);
435 
436 /* reader.c */
437 extern void reader(void);
438 
439 /* skeleton.c */
440 extern void write_section(FILE * fp, const char *const section[]);
441 
442 /* verbose.c */
443 extern void verbose(void);
444 
445 /* warshall.c */
446 extern void reflexive_transitive_closure(unsigned *R, int n);
447 
448 #ifdef NO_LEAKS
449 extern void lr0_leaks(void);
450 extern void lalr_leaks(void);
451 extern void mkpar_leaks(void);
452 extern void output_leaks(void);
453 extern void reader_leaks(void);
454 #endif
455