xref: /original-bsd/usr.bin/yacc/defs.h (revision cf2124ff)
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.5 (Berkeley) 01/20/91
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 
199 extern char *myname;
200 extern char *cptr;
201 extern char *line;
202 extern int lineno;
203 extern int outline;
204 
205 extern char *banner[];
206 extern char *tables[];
207 extern char *header[];
208 extern char *body[];
209 extern char *trailer[];
210 
211 extern char *action_file_name;
212 extern char *code_file_name;
213 extern char *defines_file_name;
214 extern char *input_file_name;
215 extern char *output_file_name;
216 extern char *text_file_name;
217 extern char *union_file_name;
218 extern char *verbose_file_name;
219 
220 extern FILE *action_file;
221 extern FILE *code_file;
222 extern FILE *defines_file;
223 extern FILE *input_file;
224 extern FILE *output_file;
225 extern FILE *text_file;
226 extern FILE *union_file;
227 extern FILE *verbose_file;
228 
229 extern int nitems;
230 extern int nrules;
231 extern int nsyms;
232 extern int ntokens;
233 extern int nvars;
234 extern int ntags;
235 
236 extern char unionized;
237 extern char line_format[];
238 
239 extern int   start_symbol;
240 extern char  **symbol_name;
241 extern short *symbol_value;
242 extern short *symbol_prec;
243 extern char  *symbol_assoc;
244 
245 extern short *ritem;
246 extern short *rlhs;
247 extern short *rrhs;
248 extern short *rprec;
249 extern char  *rassoc;
250 
251 extern short **derives;
252 extern char *nullable;
253 
254 extern bucket *first_symbol;
255 extern bucket *last_symbol;
256 
257 extern int nstates;
258 extern core *first_state;
259 extern shifts *first_shift;
260 extern reductions *first_reduction;
261 extern short *accessing_symbol;
262 extern core **state_table;
263 extern shifts **shift_table;
264 extern reductions **reduction_table;
265 extern unsigned *LA;
266 extern short *LAruleno;
267 extern short *lookaheads;
268 extern short *goto_map;
269 extern short *from_state;
270 extern short *to_state;
271 
272 extern action **parser;
273 extern int SRtotal;
274 extern int RRtotal;
275 extern short *SRconflicts;
276 extern short *RRconflicts;
277 extern short *defred;
278 extern short *rules_used;
279 extern short nunused;
280 extern short final_state;
281 
282 /* global functions */
283 
284 extern char *allocate();
285 extern bucket *lookup();
286 extern bucket *make_bucket();
287 
288 
289 /* system variables */
290 
291 extern int errno;
292 
293 
294 /* system functions */
295 
296 extern void free();
297 extern char *calloc();
298 extern char *malloc();
299 extern char *realloc();
300 extern char *strcpy();
301