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