1/*	$OpenBSD: defs.h,v 1.20 2020/05/24 17:31:54 espie Exp $	*/
2/*	$NetBSD: defs.h,v 1.6 1996/03/19 03:21:30 jtc Exp $	*/
3
4/*
5 * Copyright (c) 1989 The Regents of the University of California.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Robert Paul Corbett.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 *	@(#)defs.h	5.6 (Berkeley) 5/24/93
36 */
37
38#include <assert.h>
39#include <ctype.h>
40#include <stdio.h>
41#include <string.h>
42#include <stdlib.h>
43
44/*  machine-dependent definitions			*/
45/*  the following definitions are for the Tahoe		*/
46/*  they might have to be changed for other machines	*/
47
48/*  MAXCHAR is the largest unsigned character value	*/
49/*  MAXSHORT is the largest value of a C short		*/
50/*  MINSHORT is the most negative value of a C short	*/
51/*  MAXTABLE is the maximum table size			*/
52/*  BITS_PER_WORD is the number of bits in a C unsigned	*/
53/*  WORDSIZE computes the number of words needed to	*/
54/*	store n bits					*/
55/*  BIT returns the value of the n-th bit starting	*/
56/*	from r (0-indexed)				*/
57/*  SETBIT sets the n-th bit starting from r		*/
58
59#define	MAXCHAR		255
60#define	MAXSHORT	32767
61#define MINSHORT	-32768
62#define MAXTABLE	32500
63#define BITS_PER_WORD	32
64#define	WORDSIZE(n)	(((n)+(BITS_PER_WORD-1))/BITS_PER_WORD)
65#define	BIT(r, n)	((((r)[(n)>>5])>>((n)&31))&1)
66#define	SETBIT(r, n)	((r)[(n)>>5]|=((unsigned)1<<((n)&31)))
67
68
69/*  character names  */
70
71#define	NUL		'\0'    /*  the null character  */
72#define	NEWLINE		'\n'    /*  line feed  */
73#define	SP		' '     /*  space  */
74#define	BS		'\b'    /*  backspace  */
75#define	HT		'\t'    /*  horizontal tab  */
76#define	VT		'\013'  /*  vertical tab  */
77#define	CR		'\r'    /*  carriage return  */
78#define	FF		'\f'    /*  form feed  */
79#define	QUOTE		'\''    /*  single quote  */
80#define	DOUBLE_QUOTE	'\"'    /*  double quote  */
81#define	BACKSLASH	'\\'    /*  backslash  */
82
83
84/* defines for constructing filenames */
85
86#define CODE_SUFFIX	".code.c"
87#define	DEFINES_SUFFIX	".tab.h"
88#define	OUTPUT_SUFFIX	".tab.c"
89#define	VERBOSE_SUFFIX	".output"
90
91
92/* keyword codes */
93
94#define TOKEN 0
95#define LEFT 1
96#define RIGHT 2
97#define NONASSOC 3
98#define MARK 4
99#define TEXT 5
100#define TYPE 6
101#define START 7
102#define UNION 8
103#define IDENT 9
104#define EXPECT 10
105
106
107/*  symbol classes  */
108
109#define UNKNOWN 0
110#define TERM 1
111#define NONTERM 2
112
113
114/*  the undefined value  */
115
116#define UNDEFINED (-1)
117
118
119/*  action codes  */
120
121#define SHIFT 1
122#define REDUCE 2
123
124
125/*  character macros  */
126
127#define IS_IDENT(c)	(isalnum(c) || (c) == '_' || (c) == '.' || (c) == '$')
128#define	NUMERIC_VALUE(c)	((c) - '0')
129
130
131/*  symbol macros  */
132
133#define ISTOKEN(s)	((s) < start_symbol)
134#define ISVAR(s)	((s) >= start_symbol)
135
136
137/*  storage allocation macros  */
138
139#define	NEW(t)		((t*)allocate(sizeof(t)))
140#define	NEW2(n,t)	((t*)allocate((n)*sizeof(t)))
141
142
143/*  the structure of a symbol table entry  */
144
145typedef struct bucket bucket;
146struct bucket {
147	struct bucket *link;
148	struct bucket *next;
149	char *name;
150	char *tag;
151	short value;
152	short index;
153	short prec;
154	char class;
155	char assoc;
156};
157
158
159/*  the structure of the LR(0) state machine  */
160
161typedef struct core core;
162struct core {
163	struct core *next;
164	struct core *link;
165	short number;
166	short accessing_symbol;
167	short nitems;
168	short items[1];
169};
170
171
172/*  the structure used to record shifts  */
173
174typedef struct shifts shifts;
175struct shifts {
176	struct shifts *next;
177	short number;
178	short nshifts;
179	short shift[1];
180};
181
182
183/*  the structure used to store reductions  */
184
185typedef struct reductions reductions;
186struct reductions {
187	struct reductions *next;
188	short number;
189	short nreds;
190	short rules[1];
191};
192
193
194/*  the structure used to represent parser actions  */
195
196typedef struct action action;
197struct action {
198	struct action *next;
199	short symbol;
200	short number;
201	short prec;
202	char action_code;
203	char assoc;
204	char suppressed;
205};
206
207
208/* global variables */
209
210extern char dflag;
211extern char lflag;
212extern char rflag;
213extern char tflag;
214extern char vflag;
215extern char *symbol_prefix;
216
217extern char *cptr;
218extern char *line;
219extern int lineno;
220extern int outline;
221
222extern char *banner[];
223extern char *tables[];
224extern char *header[];
225extern char *body[];
226extern char *trailer[];
227
228extern char *code_file_name;
229extern char *defines_file_name;
230extern char *input_file_name;
231extern char *output_file_name;
232extern char *verbose_file_name;
233
234extern FILE *action_file;
235extern FILE *code_file;
236extern FILE *defines_file;
237extern FILE *input_file;
238extern FILE *output_file;
239extern FILE *text_file;
240extern FILE *union_file;
241extern FILE *verbose_file;
242
243extern int nitems;
244extern int nrules;
245extern int nsyms;
246extern int ntokens;
247extern int nvars;
248extern int ntags;
249
250extern char unionized;
251extern char line_format[];
252
253extern int   start_symbol;
254extern char  **symbol_name;
255extern short *symbol_value;
256extern short *symbol_prec;
257extern char  *symbol_assoc;
258
259extern short *ritem;
260extern short *rlhs;
261extern short *rrhs;
262extern short *rprec;
263extern char  *rassoc;
264
265extern short **derives;
266extern char *nullable;
267
268extern bucket *first_symbol;
269extern bucket *last_symbol;
270
271extern int nstates;
272extern core *first_state;
273extern shifts *first_shift;
274extern reductions *first_reduction;
275extern short *accessing_symbol;
276extern core **state_table;
277extern shifts **shift_table;
278extern reductions **reduction_table;
279extern unsigned *LA;
280extern short *LAruleno;
281extern short *lookaheads;
282extern short *goto_map;
283extern short *from_state;
284extern short *to_state;
285
286extern action **parser;
287extern int SRtotal;
288extern int SRexpect;
289extern int RRtotal;
290extern short *SRconflicts;
291extern short *RRconflicts;
292extern short *defred;
293extern short *rules_used;
294extern short nunused;
295extern short final_state;
296
297/* global functions */
298
299extern void *allocate(size_t);
300extern bucket *lookup(char *);
301extern bucket *make_bucket(char *);
302extern void set_first_derives(void);
303extern void closure(short *, int);
304extern void finalize_closure(void);
305
306extern __dead void fatal(char *);
307
308extern void reflexive_transitive_closure(unsigned *, int);
309
310extern __dead void no_space(void);
311extern __dead void open_error(char *);
312extern __dead void tempfile_error(void);
313extern __dead void open_write_error(char *);
314extern __dead void unexpected_EOF(void);
315extern void print_pos(char *, char *);
316extern __dead void syntax_error(int, char *, char *);
317extern __dead void unterminated_comment(int, char *, char *);
318extern __dead void unterminated_string(int, char *, char *);
319extern __dead void unterminated_text(int, char *, char *);
320extern __dead void unterminated_union(int, char *, char *);
321extern __dead void over_unionized(char *);
322extern __dead void illegal_tag(int, char *, char *);
323extern __dead void illegal_character(char *);
324extern __dead void used_reserved(char *);
325extern __dead void tokenized_start(char *);
326extern void retyped_warning(char *);
327extern void reprec_warning(char *);
328extern void revalued_warning(char *);
329extern __dead void terminal_start(char *);
330extern void restarted_warning(void);
331extern __dead void no_grammar(void);
332extern __dead void terminal_lhs(int);
333extern void prec_redeclared(void);
334extern __dead void unterminated_action(int, char *, char *);
335extern void dollar_warning(int, int);
336extern __dead void dollar_error(int, char *, char *);
337extern __dead void untyped_lhs(void);
338extern __dead void untyped_rhs(int, char *);
339extern __dead void unknown_rhs(int);
340extern void default_action_warning(void);
341extern __dead void undefined_goal(char *);
342extern void undefined_symbol_warning(char *);
343
344extern void lalr(void);
345
346extern void reader(void);
347extern void lr0(void);
348extern void free_nullable(void);
349extern void free_derives(void);
350extern void make_parser(void);
351extern void verbose(void);
352extern void output(void);
353extern void free_parser(void);
354extern void write_section(char *[]);
355
356extern void create_symbol_table(void);
357extern void free_symbol_table(void);
358extern void free_symbols(void);
359
360
361/* system variables */
362
363extern char *__progname;
364