xref: /original-bsd/usr.bin/yacc/defs.h (revision d08c9fe5)
1 /*
2  * Copyright (c) 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Robert Paul Corbett.
7  *
8  * %sccs.include.redist.c%
9  *
10  *	@(#)defs.h	5.6 (Berkeley) 05/24/93
11  */
12 
13 #include <assert.h>
14 #include <ctype.h>
15 #include <stdio.h>
16 
17 
18 /*  machine-dependent definitions			*/
19 /*  the following definitions are for the Tahoe		*/
20 /*  they might have to be changed for other machines	*/
21 
22 /*  MAXCHAR is the largest unsigned character value	*/
23 /*  MAXSHORT is the largest value of a C short		*/
24 /*  MINSHORT is the most negative value of a C short	*/
25 /*  MAXTABLE is the maximum table size			*/
26 /*  BITS_PER_WORD is the number of bits in a C unsigned	*/
27 /*  WORDSIZE computes the number of words needed to	*/
28 /*	store n bits					*/
29 /*  BIT returns the value of the n-th bit starting	*/
30 /*	from r (0-indexed)				*/
31 /*  SETBIT sets the n-th bit starting from r		*/
32 
33 #define	MAXCHAR		255
34 #define	MAXSHORT	32767
35 #define MINSHORT	-32768
36 #define MAXTABLE	32500
37 #define BITS_PER_WORD	32
38 #define	WORDSIZE(n)	(((n)+(BITS_PER_WORD-1))/BITS_PER_WORD)
39 #define	BIT(r, n)	((((r)[(n)>>5])>>((n)&31))&1)
40 #define	SETBIT(r, n)	((r)[(n)>>5]|=((unsigned)1<<((n)&31)))
41 
42 
43 /*  character names  */
44 
45 #define	NUL		'\0'    /*  the null character  */
46 #define	NEWLINE		'\n'    /*  line feed  */
47 #define	SP		' '     /*  space  */
48 #define	BS		'\b'    /*  backspace  */
49 #define	HT		'\t'    /*  horizontal tab  */
50 #define	VT		'\013'  /*  vertical tab  */
51 #define	CR		'\r'    /*  carriage return  */
52 #define	FF		'\f'    /*  form feed  */
53 #define	QUOTE		'\''    /*  single quote  */
54 #define	DOUBLE_QUOTE	'\"'    /*  double quote  */
55 #define	BACKSLASH	'\\'    /*  backslash  */
56 
57 
58 /* defines for constructing filenames */
59 
60 #define CODE_SUFFIX	".code.c"
61 #define	DEFINES_SUFFIX	".tab.h"
62 #define	OUTPUT_SUFFIX	".tab.c"
63 #define	VERBOSE_SUFFIX	".output"
64 
65 
66 /* keyword codes */
67 
68 #define TOKEN 0
69 #define LEFT 1
70 #define RIGHT 2
71 #define NONASSOC 3
72 #define MARK 4
73 #define TEXT 5
74 #define TYPE 6
75 #define START 7
76 #define UNION 8
77 #define IDENT 9
78 
79 
80 /*  symbol classes  */
81 
82 #define UNKNOWN 0
83 #define TERM 1
84 #define NONTERM 2
85 
86 
87 /*  the undefined value  */
88 
89 #define UNDEFINED (-1)
90 
91 
92 /*  action codes  */
93 
94 #define SHIFT 1
95 #define REDUCE 2
96 
97 
98 /*  character macros  */
99 
100 #define IS_IDENT(c)	(isalnum(c) || (c) == '_' || (c) == '.' || (c) == '$')
101 #define	IS_OCTAL(c)	((c) >= '0' && (c) <= '7')
102 #define	NUMERIC_VALUE(c)	((c) - '0')
103 
104 
105 /*  symbol macros  */
106 
107 #define ISTOKEN(s)	((s) < start_symbol)
108 #define ISVAR(s)	((s) >= start_symbol)
109 
110 
111 /*  storage allocation macros  */
112 
113 #define CALLOC(k,n)	(calloc((unsigned)(k),(unsigned)(n)))
114 #define	FREE(x)		(free((char*)(x)))
115 #define MALLOC(n)	(malloc((unsigned)(n)))
116 #define	NEW(t)		((t*)allocate(sizeof(t)))
117 #define	NEW2(n,t)	((t*)allocate((unsigned)((n)*sizeof(t))))
118 #define REALLOC(p,n)	(realloc((char*)(p),(unsigned)(n)))
119 
120 
121 /*  the structure of a symbol table entry  */
122 
123 typedef struct bucket bucket;
124 struct bucket
125 {
126     struct bucket *link;
127     struct bucket *next;
128     char *name;
129     char *tag;
130     short value;
131     short index;
132     short prec;
133     char class;
134     char assoc;
135 };
136 
137 
138 /*  the structure of the LR(0) state machine  */
139 
140 typedef struct core core;
141 struct core
142 {
143     struct core *next;
144     struct core *link;
145     short number;
146     short accessing_symbol;
147     short nitems;
148     short items[1];
149 };
150 
151 
152 /*  the structure used to record shifts  */
153 
154 typedef struct shifts shifts;
155 struct shifts
156 {
157     struct shifts *next;
158     short number;
159     short nshifts;
160     short shift[1];
161 };
162 
163 
164 /*  the structure used to store reductions  */
165 
166 typedef struct reductions reductions;
167 struct reductions
168 {
169     struct reductions *next;
170     short number;
171     short nreds;
172     short rules[1];
173 };
174 
175 
176 /*  the structure used to represent parser actions  */
177 
178 typedef struct action action;
179 struct action
180 {
181     struct action *next;
182     short symbol;
183     short number;
184     short prec;
185     char action_code;
186     char assoc;
187     char suppressed;
188 };
189 
190 
191 /* global variables */
192 
193 extern char dflag;
194 extern char lflag;
195 extern char rflag;
196 extern char tflag;
197 extern char vflag;
198 extern char *symbol_prefix;
199 
200 extern char *myname;
201 extern char *cptr;
202 extern char *line;
203 extern int lineno;
204 extern int outline;
205 
206 extern char *banner[];
207 extern char *tables[];
208 extern char *header[];
209 extern char *body[];
210 extern char *trailer[];
211 
212 extern char *action_file_name;
213 extern char *code_file_name;
214 extern char *defines_file_name;
215 extern char *input_file_name;
216 extern char *output_file_name;
217 extern char *text_file_name;
218 extern char *union_file_name;
219 extern char *verbose_file_name;
220 
221 extern FILE *action_file;
222 extern FILE *code_file;
223 extern FILE *defines_file;
224 extern FILE *input_file;
225 extern FILE *output_file;
226 extern FILE *text_file;
227 extern FILE *union_file;
228 extern FILE *verbose_file;
229 
230 extern int nitems;
231 extern int nrules;
232 extern int nsyms;
233 extern int ntokens;
234 extern int nvars;
235 extern int ntags;
236 
237 extern char unionized;
238 extern char line_format[];
239 
240 extern int   start_symbol;
241 extern char  **symbol_name;
242 extern short *symbol_value;
243 extern short *symbol_prec;
244 extern char  *symbol_assoc;
245 
246 extern short *ritem;
247 extern short *rlhs;
248 extern short *rrhs;
249 extern short *rprec;
250 extern char  *rassoc;
251 
252 extern short **derives;
253 extern char *nullable;
254 
255 extern bucket *first_symbol;
256 extern bucket *last_symbol;
257 
258 extern int nstates;
259 extern core *first_state;
260 extern shifts *first_shift;
261 extern reductions *first_reduction;
262 extern short *accessing_symbol;
263 extern core **state_table;
264 extern shifts **shift_table;
265 extern reductions **reduction_table;
266 extern unsigned *LA;
267 extern short *LAruleno;
268 extern short *lookaheads;
269 extern short *goto_map;
270 extern short *from_state;
271 extern short *to_state;
272 
273 extern action **parser;
274 extern int SRtotal;
275 extern int RRtotal;
276 extern short *SRconflicts;
277 extern short *RRconflicts;
278 extern short *defred;
279 extern short *rules_used;
280 extern short nunused;
281 extern short final_state;
282 
283 /* global functions */
284 
285 extern char *allocate();
286 extern bucket *lookup();
287 extern bucket *make_bucket();
288 
289 
290 /* system variables */
291 
292 extern int errno;
293 
294 
295 /* system functions */
296 
297 extern void free();
298 extern char *calloc();
299 extern char *malloc();
300 extern char *realloc();
301 extern char *strcpy();
302