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 * 4. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 *	@(#)defs.h	5.6 (Berkeley) 5/24/93
33 * $FreeBSD$
34 */
35
36#include <assert.h>
37#include <ctype.h>
38#include <err.h>
39#include <stdio.h>
40
41
42/*  machine-dependent definitions			*/
43/*  the following definitions are for the Tahoe		*/
44/*  they might have to be changed for other machines	*/
45
46/*  MAXTABLE is the maximum table size			*/
47/*  BITS_PER_WORD is the number of bits in a C unsigned	*/
48/*  WORDSIZE computes the number of words needed to	*/
49/*	store n bits					*/
50/*  BIT returns the value of the n-th bit starting	*/
51/*	from r (0-indexed)				*/
52/*  SETBIT sets the n-th bit starting from r		*/
53
54#define MAXTABLE	32500
55#define BITS_PER_WORD	32
56#define	WORDSIZE(n)	(((n)+(BITS_PER_WORD-1))/BITS_PER_WORD)
57#define	BIT(r, n)	((((r)[(n)>>5])>>((n)&31))&1)
58#define	SETBIT(r, n)	((r)[(n)>>5]|=((unsigned)1<<((n)&31)))
59
60
61/*  character names  */
62
63#define	NUL		'\0'    /*  the null character  */
64#define	NEWLINE		'\n'    /*  line feed  */
65#define	SP		' '     /*  space  */
66#define	BS		'\b'    /*  backspace  */
67#define	HT		'\t'    /*  horizontal tab  */
68#define	VT		'\013'  /*  vertical tab  */
69#define	CR		'\r'    /*  carriage return  */
70#define	FF		'\f'    /*  form feed  */
71#define	QUOTE		'\''    /*  single quote  */
72#define	DOUBLE_QUOTE	'\"'    /*  double quote  */
73#define	BACKSLASH	'\\'    /*  backslash  */
74
75
76/* defines for constructing filenames */
77
78#define CODE_SUFFIX	".code.c"
79#define	DEFINES_SUFFIX	".tab.h"
80#define	OUTPUT_SUFFIX	".tab.c"
81#define	VERBOSE_SUFFIX	".output"
82
83
84/* keyword codes */
85
86#define TOKEN 0
87#define LEFT 1
88#define RIGHT 2
89#define NONASSOC 3
90#define MARK 4
91#define TEXT 5
92#define TYPE 6
93#define START 7
94#define UNION 8
95#define IDENT 9
96#define EXPECT 10
97
98
99/*  symbol classes  */
100
101#define UNKNOWN 0
102#define TERM 1
103#define NONTERM 2
104
105
106/*  the undefined value  */
107
108#define UNDEFINED (-1)
109
110
111/*  action codes  */
112
113#define SHIFT 1
114#define REDUCE 2
115
116
117/*  character macros  */
118
119#define IS_IDENT(c)	(isalnum(c) || (c) == '_' || (c) == '.' || (c) == '$')
120#define	IS_OCTAL(c)	((c) >= '0' && (c) <= '7')
121#define	NUMERIC_VALUE(c)	((c) - '0')
122
123
124/*  symbol macros  */
125
126#define ISTOKEN(s)	((s) < start_symbol)
127#define ISVAR(s)	((s) >= start_symbol)
128
129
130/*  storage allocation macros  */
131
132#define	NEW(t)		((t*)allocate(sizeof(t)))
133#define	NEW2(n,t)	((t*)allocate((n)*sizeof(t)))
134
135
136/*  the structure of a symbol table entry  */
137
138typedef struct bucket bucket;
139struct bucket
140{
141    struct bucket *link;
142    struct bucket *next;
143    char *name;
144    char *tag;
145    short value;
146    short index;
147    short prec;
148    char class;
149    char assoc;
150};
151
152
153/*  the structure of the LR(0) state machine  */
154
155typedef struct core core;
156struct core
157{
158    struct core *next;
159    struct core *link;
160    short number;
161    short accessing_symbol;
162    short nitems;
163    short items[1];
164};
165
166
167/*  the structure used to record shifts  */
168
169typedef struct shifts shifts;
170struct shifts
171{
172    struct shifts *next;
173    short number;
174    short nshifts;
175    short shift[1];
176};
177
178
179/*  the structure used to store reductions  */
180
181typedef struct reductions reductions;
182struct reductions
183{
184    struct reductions *next;
185    short number;
186    short nreds;
187    short rules[1];
188};
189
190
191/*  the structure used to represent parser actions  */
192
193typedef struct action action;
194struct action
195{
196    struct action *next;
197    short symbol;
198    short number;
199    short prec;
200    char action_code;
201    char assoc;
202    char suppressed;
203};
204
205
206/* global variables */
207
208extern char dflag;
209extern char lflag;
210extern char rflag;
211extern char tflag;
212extern char vflag;
213extern const char *symbol_prefix;
214
215extern char *cptr;
216extern char *line;
217extern int lineno;
218extern int outline;
219
220extern const char *banner[];
221extern const char *tables[];
222extern const char *header[];
223extern const char *body[];
224extern const char *trailer[];
225
226extern char *action_file_name;
227extern char *code_file_name;
228extern char *defines_file_name;
229extern const char *input_file_name;
230extern char *output_file_name;
231extern char *text_file_name;
232extern char *union_file_name;
233extern char *verbose_file_name;
234
235extern FILE *action_file;
236extern FILE *code_file;
237extern FILE *defines_file;
238extern FILE *input_file;
239extern FILE *output_file;
240extern FILE *text_file;
241extern FILE *union_file;
242extern FILE *verbose_file;
243
244extern int nitems;
245extern int nrules;
246extern int nsyms;
247extern int ntokens;
248extern int nvars;
249extern int ntags;
250
251extern char unionized;
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 SRexpect;
288extern int SRtotal;
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
299void *allocate(size_t);
300void closure(short *, int);
301void create_symbol_table(void);
302void default_action_warning(void);
303void dollar_error(int, char *, char *) __dead2;
304void dollar_warning(int, int);
305void done(int) __dead2;
306void fatal(const char *msg) __dead2;
307void finalize_closure(void);
308void free_parser(void);
309void free_symbols(void);
310void free_symbol_table(void);
311void illegal_character(char *) __dead2;
312void illegal_tag(int, char *, char *) __dead2;
313void lalr(void);
314bucket *lookup(char *);
315void lr0(void);
316bucket *make_bucket(const char *);
317void make_parser(void);
318void no_grammar(void) __dead2;
319void no_space(void) __dead2;
320void open_error(const char *) __dead2;
321void output(void);
322void over_unionized(char *) __dead2;
323void prec_redeclared(void);
324void reader(void);
325void reflexive_transitive_closure(unsigned *, int);
326void reprec_warning(char *);
327void restarted_warning(void);
328void retyped_warning(char *);
329void revalued_warning(char *);
330void set_first_derives(void);
331void syntax_error(int, char *, char *) __dead2;
332void terminal_lhs(int) __dead2;
333void terminal_start(char *) __dead2;
334void tokenized_start(char *) __dead2;
335void undefined_goal(char *) __dead2;
336void undefined_symbol_warning(char *);
337void unexpected_EOF(void) __dead2;
338void unknown_rhs(int) __dead2;
339void unterminated_action(int, char *, char *) __dead2;
340void unterminated_comment(int, char *, char *) __dead2;
341void unterminated_string(int, char *, char *) __dead2;
342void unterminated_text(int, char *, char *) __dead2;
343void unterminated_union(int, char *, char *) __dead2;
344void untyped_lhs(void) __dead2;
345void untyped_rhs(int, char *) __dead2;
346void used_reserved(char *) __dead2;
347void verbose(void);
348void write_section(const char **);
349