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.4 (Berkeley) 06/01/90 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 VAX */ 20 /* they might have to be changed for other machines */ 21 22 /* MAXCHAR is the largest 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] |= (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 DEFINES_SUFFIX ".tab.h" 61 #define OUTPUT_SUFFIX ".tab.c" 62 #define VERBOSE_SUFFIX ".output" 63 64 65 /* keyword codes */ 66 67 #define TOKEN 0 68 #define LEFT 1 69 #define RIGHT 2 70 #define NONASSOC 3 71 #define MARK 4 72 #define TEXT 5 73 #define TYPE 6 74 #define START 7 75 #define UNION 8 76 #define IDENT 9 77 78 79 /* symbol classes */ 80 81 #define UNKNOWN 0 82 #define TERM 1 83 #define NONTERM 2 84 85 86 /* the undefined value */ 87 88 #define UNDEFINED (-1) 89 90 91 /* action codes */ 92 93 #define SHIFT 1 94 #define REDUCE 2 95 #define ERROR 3 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 FREE(x) (free((char*)(x))) 114 #define MALLOC(n) (malloc((unsigned)(n))) 115 #define NEW(t) ((t*)allocate(sizeof(t))) 116 #define NEW2(n,t) ((t*)allocate((unsigned)((n)*sizeof(t)))) 117 #define REALLOC(p,n) (realloc((char*)(p),(unsigned)(n))) 118 119 120 /* the structure of a symbol table entry */ 121 122 typedef struct bucket bucket; 123 struct bucket 124 { 125 struct bucket *link; 126 struct bucket *next; 127 char *name; 128 char *tag; 129 short value; 130 short index; 131 short prec; 132 char class; 133 char assoc; 134 }; 135 136 137 /* the structure of the LR(0) state machine */ 138 139 typedef struct core core; 140 struct core 141 { 142 struct core *next; 143 struct core *link; 144 short number; 145 short accessing_symbol; 146 short nitems; 147 short items[1]; 148 }; 149 150 151 /* the structure used to record shifts */ 152 153 typedef struct shifts shifts; 154 struct shifts 155 { 156 struct shifts *next; 157 short number; 158 short nshifts; 159 short shift[1]; 160 }; 161 162 163 /* the structure used to store reductions */ 164 165 typedef struct reductions reductions; 166 struct reductions 167 { 168 struct reductions *next; 169 short number; 170 short nreds; 171 short rules[1]; 172 }; 173 174 175 /* the structure used to represent parser actions */ 176 177 typedef struct action action; 178 struct action 179 { 180 struct action *next; 181 short symbol; 182 short number; 183 short prec; 184 char action_code; 185 char assoc; 186 char suppressed; 187 }; 188 189 190 /* global variables */ 191 192 extern char dflag; 193 extern char lflag; 194 extern char tflag; 195 extern char vflag; 196 197 extern char *myname; 198 extern char *cptr; 199 extern char *line; 200 extern int lineno; 201 extern int outline; 202 203 extern char *banner[]; 204 extern char *header[]; 205 extern char *body[]; 206 extern char *trailer[]; 207 208 extern char *action_file_name; 209 extern char *defines_file_name; 210 extern char *input_file_name; 211 extern char *output_file_name; 212 extern char *text_file_name; 213 extern char *union_file_name; 214 extern char *verbose_file_name; 215 216 extern FILE *action_file; 217 extern FILE *defines_file; 218 extern FILE *input_file; 219 extern FILE *output_file; 220 extern FILE *text_file; 221 extern FILE *union_file; 222 extern FILE *verbose_file; 223 224 extern int nitems; 225 extern int nrules; 226 extern int nsyms; 227 extern int ntokens; 228 extern int nvars; 229 extern int ntags; 230 231 extern char unionized; 232 extern char line_format[]; 233 234 extern int start_symbol; 235 extern char **symbol_name; 236 extern short *symbol_value; 237 extern short *symbol_prec; 238 extern char *symbol_assoc; 239 240 extern short *ritem; 241 extern short *rlhs; 242 extern short *rrhs; 243 extern short *rprec; 244 extern char *rassoc; 245 246 extern short **derives; 247 extern char *nullable; 248 249 extern bucket *first_symbol; 250 extern bucket *last_symbol; 251 252 extern int nstates; 253 extern core *first_state; 254 extern shifts *first_shift; 255 extern reductions *first_reduction; 256 extern short *accessing_symbol; 257 extern core **state_table; 258 extern shifts **shift_table; 259 extern reductions **reduction_table; 260 extern unsigned *LA; 261 extern short *LAruleno; 262 extern short *lookaheads; 263 extern short *goto_map; 264 extern short *from_state; 265 extern short *to_state; 266 267 extern action **parser; 268 extern int SRtotal; 269 extern int RRtotal; 270 extern short *SRconflicts; 271 extern short *RRconflicts; 272 extern short *defred; 273 extern short *rules_used; 274 extern short nunused; 275 extern short final_state; 276 277 /* global functions */ 278 279 extern char *allocate(); 280 extern bucket *lookup(); 281 extern bucket *make_bucket(); 282 283 284 /* system variables */ 285 286 extern int errno; 287 288 289 /* system functions */ 290 291 extern void free(); 292 extern char *calloc(); 293 extern char *malloc(); 294 extern char *realloc(); 295 extern char *strcpy(); 296