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