defs.h revision 1591
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, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 *	@(#)defs.h	5.6 (Berkeley) 5/24/93
37 */
38
39#include <assert.h>
40#include <ctype.h>
41#include <stdio.h>
42
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
105
106/*  symbol classes  */
107
108#define UNKNOWN 0
109#define TERM 1
110#define NONTERM 2
111
112
113/*  the undefined value  */
114
115#define UNDEFINED (-1)
116
117
118/*  action codes  */
119
120#define SHIFT 1
121#define REDUCE 2
122
123
124/*  character macros  */
125
126#define IS_IDENT(c)	(isalnum(c) || (c) == '_' || (c) == '.' || (c) == '$')
127#define	IS_OCTAL(c)	((c) >= '0' && (c) <= '7')
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 CALLOC(k,n)	(calloc((unsigned)(k),(unsigned)(n)))
140#define	FREE(x)		(free((char*)(x)))
141#define MALLOC(n)	(malloc((unsigned)(n)))
142#define	NEW(t)		((t*)allocate(sizeof(t)))
143#define	NEW2(n,t)	((t*)allocate((unsigned)((n)*sizeof(t))))
144#define REALLOC(p,n)	(realloc((char*)(p),(unsigned)(n)))
145
146
147/*  the structure of a symbol table entry  */
148
149typedef struct bucket bucket;
150struct bucket
151{
152    struct bucket *link;
153    struct bucket *next;
154    char *name;
155    char *tag;
156    short value;
157    short index;
158    short prec;
159    char class;
160    char assoc;
161};
162
163
164/*  the structure of the LR(0) state machine  */
165
166typedef struct core core;
167struct core
168{
169    struct core *next;
170    struct core *link;
171    short number;
172    short accessing_symbol;
173    short nitems;
174    short items[1];
175};
176
177
178/*  the structure used to record shifts  */
179
180typedef struct shifts shifts;
181struct shifts
182{
183    struct shifts *next;
184    short number;
185    short nshifts;
186    short shift[1];
187};
188
189
190/*  the structure used to store reductions  */
191
192typedef struct reductions reductions;
193struct reductions
194{
195    struct reductions *next;
196    short number;
197    short nreds;
198    short rules[1];
199};
200
201
202/*  the structure used to represent parser actions  */
203
204typedef struct action action;
205struct action
206{
207    struct action *next;
208    short symbol;
209    short number;
210    short prec;
211    char action_code;
212    char assoc;
213    char suppressed;
214};
215
216
217/* global variables */
218
219extern char dflag;
220extern char lflag;
221extern char rflag;
222extern char tflag;
223extern char vflag;
224extern char *symbol_prefix;
225
226extern char *myname;
227extern char *cptr;
228extern char *line;
229extern int lineno;
230extern int outline;
231
232extern char *banner[];
233extern char *tables[];
234extern char *header[];
235extern char *body[];
236extern char *trailer[];
237
238extern char *action_file_name;
239extern char *code_file_name;
240extern char *defines_file_name;
241extern char *input_file_name;
242extern char *output_file_name;
243extern char *text_file_name;
244extern char *union_file_name;
245extern char *verbose_file_name;
246
247extern FILE *action_file;
248extern FILE *code_file;
249extern FILE *defines_file;
250extern FILE *input_file;
251extern FILE *output_file;
252extern FILE *text_file;
253extern FILE *union_file;
254extern FILE *verbose_file;
255
256extern int nitems;
257extern int nrules;
258extern int nsyms;
259extern int ntokens;
260extern int nvars;
261extern int ntags;
262
263extern char unionized;
264extern char line_format[];
265
266extern int   start_symbol;
267extern char  **symbol_name;
268extern short *symbol_value;
269extern short *symbol_prec;
270extern char  *symbol_assoc;
271
272extern short *ritem;
273extern short *rlhs;
274extern short *rrhs;
275extern short *rprec;
276extern char  *rassoc;
277
278extern short **derives;
279extern char *nullable;
280
281extern bucket *first_symbol;
282extern bucket *last_symbol;
283
284extern int nstates;
285extern core *first_state;
286extern shifts *first_shift;
287extern reductions *first_reduction;
288extern short *accessing_symbol;
289extern core **state_table;
290extern shifts **shift_table;
291extern reductions **reduction_table;
292extern unsigned *LA;
293extern short *LAruleno;
294extern short *lookaheads;
295extern short *goto_map;
296extern short *from_state;
297extern short *to_state;
298
299extern action **parser;
300extern int SRtotal;
301extern int RRtotal;
302extern short *SRconflicts;
303extern short *RRconflicts;
304extern short *defred;
305extern short *rules_used;
306extern short nunused;
307extern short final_state;
308
309/* global functions */
310
311extern char *allocate();
312extern bucket *lookup();
313extern bucket *make_bucket();
314
315
316/* system variables */
317
318extern int errno;
319
320
321/* system functions */
322
323extern void free();
324extern char *calloc();
325extern char *malloc();
326extern char *realloc();
327extern char *strcpy();
328