| 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 | * $FreeBSD: src/usr.bin/yacc/defs.h,v 1.12 2002/06/11 11:27:20 robert Exp $ | 
|---|
| 38 | */ | 
|---|
| 39 |  | 
|---|
| 40 | #include <assert.h> | 
|---|
| 41 | #include <ctype.h> | 
|---|
| 42 | #include <err.h> | 
|---|
| 43 | #include <stdio.h> | 
|---|
| 44 |  | 
|---|
| 45 |  | 
|---|
| 46 | /*  machine-dependent definitions                       */ | 
|---|
| 47 | /*  the following definitions are for the Tahoe         */ | 
|---|
| 48 | /*  they might have to be changed for other machines    */ | 
|---|
| 49 |  | 
|---|
| 50 | /*  MAXTABLE is the maximum table size                  */ | 
|---|
| 51 | /*  BITS_PER_WORD is the number of bits in a C unsigned */ | 
|---|
| 52 | /*  WORDSIZE computes the number of words needed to     */ | 
|---|
| 53 | /*      store n bits                                    */ | 
|---|
| 54 | /*  BIT returns the value of the n-th bit starting      */ | 
|---|
| 55 | /*      from r (0-indexed)                              */ | 
|---|
| 56 | /*  SETBIT sets the n-th bit starting from r            */ | 
|---|
| 57 |  | 
|---|
| 58 | #define MAXTABLE        32500 | 
|---|
| 59 | #define BITS_PER_WORD   32 | 
|---|
| 60 | #define WORDSIZE(n)     (((n)+(BITS_PER_WORD-1))/BITS_PER_WORD) | 
|---|
| 61 | #define BIT(r, n)       ((((r)[(n)>>5])>>((n)&31))&1) | 
|---|
| 62 | #define SETBIT(r, n)    ((r)[(n)>>5]|=((unsigned)1<<((n)&31))) | 
|---|
| 63 |  | 
|---|
| 64 |  | 
|---|
| 65 | /*  character names  */ | 
|---|
| 66 |  | 
|---|
| 67 | #define NUL             '\0'    /*  the null character  */ | 
|---|
| 68 | #define NEWLINE         '\n'    /*  line feed  */ | 
|---|
| 69 | #define SP              ' '     /*  space  */ | 
|---|
| 70 | #define BS              '\b'    /*  backspace  */ | 
|---|
| 71 | #define HT              '\t'    /*  horizontal tab  */ | 
|---|
| 72 | #define VT              '\013'  /*  vertical tab  */ | 
|---|
| 73 | #define CR              '\r'    /*  carriage return  */ | 
|---|
| 74 | #define FF              '\f'    /*  form feed  */ | 
|---|
| 75 | #define QUOTE           '\''    /*  single quote  */ | 
|---|
| 76 | #define DOUBLE_QUOTE    '\"'    /*  double quote  */ | 
|---|
| 77 | #define BACKSLASH       '\\'    /*  backslash  */ | 
|---|
| 78 |  | 
|---|
| 79 |  | 
|---|
| 80 | /* defines for constructing filenames */ | 
|---|
| 81 |  | 
|---|
| 82 | #define CODE_SUFFIX     ".code.c" | 
|---|
| 83 | #define DEFINES_SUFFIX  ".tab.h" | 
|---|
| 84 | #define OUTPUT_SUFFIX   ".tab.c" | 
|---|
| 85 | #define VERBOSE_SUFFIX  ".output" | 
|---|
| 86 |  | 
|---|
| 87 |  | 
|---|
| 88 | /* keyword codes */ | 
|---|
| 89 |  | 
|---|
| 90 | #define TOKEN 0 | 
|---|
| 91 | #define LEFT 1 | 
|---|
| 92 | #define RIGHT 2 | 
|---|
| 93 | #define NONASSOC 3 | 
|---|
| 94 | #define MARK 4 | 
|---|
| 95 | #define TEXT 5 | 
|---|
| 96 | #define TYPE 6 | 
|---|
| 97 | #define START 7 | 
|---|
| 98 | #define UNION 8 | 
|---|
| 99 | #define IDENT 9 | 
|---|
| 100 | #define EXPECT 10 | 
|---|
| 101 |  | 
|---|
| 102 |  | 
|---|
| 103 | /*  symbol classes  */ | 
|---|
| 104 |  | 
|---|
| 105 | #define UNKNOWN 0 | 
|---|
| 106 | #define TERM 1 | 
|---|
| 107 | #define NONTERM 2 | 
|---|
| 108 |  | 
|---|
| 109 |  | 
|---|
| 110 | /*  the undefined value  */ | 
|---|
| 111 |  | 
|---|
| 112 | #define UNDEFINED (-1) | 
|---|
| 113 |  | 
|---|
| 114 |  | 
|---|
| 115 | /*  action codes  */ | 
|---|
| 116 |  | 
|---|
| 117 | #define SHIFT 1 | 
|---|
| 118 | #define REDUCE 2 | 
|---|
| 119 |  | 
|---|
| 120 |  | 
|---|
| 121 | /*  character macros  */ | 
|---|
| 122 |  | 
|---|
| 123 | #define IS_IDENT(c)     (isalnum(c) || (c) == '_' || (c) == '.' || (c) == '$') | 
|---|
| 124 | #define IS_OCTAL(c)     ((c) >= '0' && (c) <= '7') | 
|---|
| 125 | #define NUMERIC_VALUE(c)        ((c) - '0') | 
|---|
| 126 |  | 
|---|
| 127 |  | 
|---|
| 128 | /*  symbol macros  */ | 
|---|
| 129 |  | 
|---|
| 130 | #define ISTOKEN(s)      ((s) < start_symbol) | 
|---|
| 131 | #define ISVAR(s)        ((s) >= start_symbol) | 
|---|
| 132 |  | 
|---|
| 133 |  | 
|---|
| 134 | /*  storage allocation macros  */ | 
|---|
| 135 |  | 
|---|
| 136 | #define CALLOC(k,n)     (calloc((unsigned)(k),(unsigned)(n))) | 
|---|
| 137 | #define FREE(x)         (free((char*)(x))) | 
|---|
| 138 | #define MALLOC(n)       (malloc((unsigned)(n))) | 
|---|
| 139 | #define NEW(t)          ((t*)allocate(sizeof(t))) | 
|---|
| 140 | #define NEW2(n,t)       ((t*)allocate((unsigned)((n)*sizeof(t)))) | 
|---|
| 141 | #define REALLOC(p,n)    (realloc((char*)(p),(unsigned)(n))) | 
|---|
| 142 |  | 
|---|
| 143 |  | 
|---|
| 144 | /*  the structure of a symbol table entry  */ | 
|---|
| 145 |  | 
|---|
| 146 | typedef struct bucket bucket; | 
|---|
| 147 | struct bucket | 
|---|
| 148 | { | 
|---|
| 149 | struct bucket *link; | 
|---|
| 150 | struct bucket *next; | 
|---|
| 151 | char *name; | 
|---|
| 152 | char *tag; | 
|---|
| 153 | short value; | 
|---|
| 154 | short index; | 
|---|
| 155 | short prec; | 
|---|
| 156 | char class; | 
|---|
| 157 | char assoc; | 
|---|
| 158 | }; | 
|---|
| 159 |  | 
|---|
| 160 |  | 
|---|
| 161 | /*  the structure of the LR(0) state machine  */ | 
|---|
| 162 |  | 
|---|
| 163 | typedef struct core core; | 
|---|
| 164 | struct core | 
|---|
| 165 | { | 
|---|
| 166 | struct core *next; | 
|---|
| 167 | struct core *link; | 
|---|
| 168 | short number; | 
|---|
| 169 | short accessing_symbol; | 
|---|
| 170 | short nitems; | 
|---|
| 171 | short items[1]; | 
|---|
| 172 | }; | 
|---|
| 173 |  | 
|---|
| 174 |  | 
|---|
| 175 | /*  the structure used to record shifts  */ | 
|---|
| 176 |  | 
|---|
| 177 | typedef struct shifts shifts; | 
|---|
| 178 | struct shifts | 
|---|
| 179 | { | 
|---|
| 180 | struct shifts *next; | 
|---|
| 181 | short number; | 
|---|
| 182 | short nshifts; | 
|---|
| 183 | short shift[1]; | 
|---|
| 184 | }; | 
|---|
| 185 |  | 
|---|
| 186 |  | 
|---|
| 187 | /*  the structure used to store reductions  */ | 
|---|
| 188 |  | 
|---|
| 189 | typedef struct reductions reductions; | 
|---|
| 190 | struct reductions | 
|---|
| 191 | { | 
|---|
| 192 | struct reductions *next; | 
|---|
| 193 | short number; | 
|---|
| 194 | short nreds; | 
|---|
| 195 | short rules[1]; | 
|---|
| 196 | }; | 
|---|
| 197 |  | 
|---|
| 198 |  | 
|---|
| 199 | /*  the structure used to represent parser actions  */ | 
|---|
| 200 |  | 
|---|
| 201 | typedef struct action action; | 
|---|
| 202 | struct action | 
|---|
| 203 | { | 
|---|
| 204 | struct action *next; | 
|---|
| 205 | short symbol; | 
|---|
| 206 | short number; | 
|---|
| 207 | short prec; | 
|---|
| 208 | char action_code; | 
|---|
| 209 | char assoc; | 
|---|
| 210 | char suppressed; | 
|---|
| 211 | }; | 
|---|
| 212 |  | 
|---|
| 213 |  | 
|---|
| 214 | /* global variables */ | 
|---|
| 215 |  | 
|---|
| 216 | extern char dflag; | 
|---|
| 217 | extern char lflag; | 
|---|
| 218 | extern char rflag; | 
|---|
| 219 | extern char tflag; | 
|---|
| 220 | extern char vflag; | 
|---|
| 221 | extern const char *symbol_prefix; | 
|---|
| 222 |  | 
|---|
| 223 | extern char *myname; | 
|---|
| 224 | extern char *cptr; | 
|---|
| 225 | extern char *line; | 
|---|
| 226 | extern int lineno; | 
|---|
| 227 | extern int outline; | 
|---|
| 228 |  | 
|---|
| 229 | extern const char *banner[]; | 
|---|
| 230 | extern const char *tables[]; | 
|---|
| 231 | extern const char *header[]; | 
|---|
| 232 | extern const char *body[]; | 
|---|
| 233 | extern const char *trailer[]; | 
|---|
| 234 |  | 
|---|
| 235 | extern char *action_file_name; | 
|---|
| 236 | extern char *code_file_name; | 
|---|
| 237 | extern char *defines_file_name; | 
|---|
| 238 | extern const char *input_file_name; | 
|---|
| 239 | extern char *output_file_name; | 
|---|
| 240 | extern char *text_file_name; | 
|---|
| 241 | extern char *union_file_name; | 
|---|
| 242 | extern char *verbose_file_name; | 
|---|
| 243 |  | 
|---|
| 244 | extern FILE *action_file; | 
|---|
| 245 | extern FILE *code_file; | 
|---|
| 246 | extern FILE *defines_file; | 
|---|
| 247 | extern FILE *input_file; | 
|---|
| 248 | extern FILE *output_file; | 
|---|
| 249 | extern FILE *text_file; | 
|---|
| 250 | extern FILE *union_file; | 
|---|
| 251 | extern FILE *verbose_file; | 
|---|
| 252 |  | 
|---|
| 253 | extern int nitems; | 
|---|
| 254 | extern int nrules; | 
|---|
| 255 | extern int nsyms; | 
|---|
| 256 | extern int ntokens; | 
|---|
| 257 | extern int nvars; | 
|---|
| 258 | extern int ntags; | 
|---|
| 259 |  | 
|---|
| 260 | extern char unionized; | 
|---|
| 261 |  | 
|---|
| 262 | extern int   start_symbol; | 
|---|
| 263 | extern char  **symbol_name; | 
|---|
| 264 | extern short *symbol_value; | 
|---|
| 265 | extern short *symbol_prec; | 
|---|
| 266 | extern char  *symbol_assoc; | 
|---|
| 267 |  | 
|---|
| 268 | extern short *ritem; | 
|---|
| 269 | extern short *rlhs; | 
|---|
| 270 | extern short *rrhs; | 
|---|
| 271 | extern short *rprec; | 
|---|
| 272 | extern char  *rassoc; | 
|---|
| 273 |  | 
|---|
| 274 | extern short **derives; | 
|---|
| 275 | extern char *nullable; | 
|---|
| 276 |  | 
|---|
| 277 | extern bucket *first_symbol; | 
|---|
| 278 | extern bucket *last_symbol; | 
|---|
| 279 |  | 
|---|
| 280 | extern int nstates; | 
|---|
| 281 | extern core *first_state; | 
|---|
| 282 | extern shifts *first_shift; | 
|---|
| 283 | extern reductions *first_reduction; | 
|---|
| 284 | extern short *accessing_symbol; | 
|---|
| 285 | extern core **state_table; | 
|---|
| 286 | extern shifts **shift_table; | 
|---|
| 287 | extern reductions **reduction_table; | 
|---|
| 288 | extern unsigned *LA; | 
|---|
| 289 | extern short *LAruleno; | 
|---|
| 290 | extern short *lookaheads; | 
|---|
| 291 | extern short *goto_map; | 
|---|
| 292 | extern short *from_state; | 
|---|
| 293 | extern short *to_state; | 
|---|
| 294 |  | 
|---|
| 295 | extern action **parser; | 
|---|
| 296 | extern int SRexpect; | 
|---|
| 297 | extern int SRtotal; | 
|---|
| 298 | extern int RRtotal; | 
|---|
| 299 | extern short *SRconflicts; | 
|---|
| 300 | extern short *RRconflicts; | 
|---|
| 301 | extern short *defred; | 
|---|
| 302 | extern short *rules_used; | 
|---|
| 303 | extern short nunused; | 
|---|
| 304 | extern short final_state; | 
|---|
| 305 |  | 
|---|
| 306 | /* global functions */ | 
|---|
| 307 |  | 
|---|
| 308 | char *allocate(unsigned); | 
|---|
| 309 | void closure(short *, int); | 
|---|
| 310 | void create_symbol_table(void); | 
|---|
| 311 | void default_action_warning(void); | 
|---|
| 312 | void dollar_error(int, char *, char *); | 
|---|
| 313 | void dollar_warning(int, int); | 
|---|
| 314 | void done(int); | 
|---|
| 315 | void fatal(const char *msg); | 
|---|
| 316 | void finalize_closure(void); | 
|---|
| 317 | void free_parser(void); | 
|---|
| 318 | void free_symbols(void); | 
|---|
| 319 | void free_symbol_table(void); | 
|---|
| 320 | void illegal_character(char *); | 
|---|
| 321 | void illegal_tag(int, char *, char *); | 
|---|
| 322 | void lalr(void); | 
|---|
| 323 | bucket *lookup(char *); | 
|---|
| 324 | void lr0(void); | 
|---|
| 325 | bucket *make_bucket(const char *); | 
|---|
| 326 | void make_parser(void); | 
|---|
| 327 | void no_grammar(void); | 
|---|
| 328 | void no_space(void); | 
|---|
| 329 | void open_error(const char *); | 
|---|
| 330 | void output(void); | 
|---|
| 331 | void over_unionized(char *); | 
|---|
| 332 | void prec_redeclared(void); | 
|---|
| 333 | void reader(void); | 
|---|
| 334 | void reflexive_transitive_closure(unsigned *, int); | 
|---|
| 335 | void reprec_warning(char *); | 
|---|
| 336 | void restarted_warning(void); | 
|---|
| 337 | void retyped_warning(char *); | 
|---|
| 338 | void revalued_warning(char *); | 
|---|
| 339 | void set_first_derives(void); | 
|---|
| 340 | void syntax_error(int, char *, char *); | 
|---|
| 341 | void terminal_lhs(int); | 
|---|
| 342 | void terminal_start(char *); | 
|---|
| 343 | void tokenized_start(char *); | 
|---|
| 344 | void undefined_goal(char *); | 
|---|
| 345 | void undefined_symbol_warning(char *); | 
|---|
| 346 | void unexpected_EOF(void); | 
|---|
| 347 | void unknown_rhs(int); | 
|---|
| 348 | void unterminated_action(int, char *, char *); | 
|---|
| 349 | void unterminated_comment(int, char *, char *); | 
|---|
| 350 | void unterminated_string(int, char *, char *); | 
|---|
| 351 | void unterminated_text(int, char *, char *); | 
|---|
| 352 | void unterminated_union(int, char *, char *); | 
|---|
| 353 | void untyped_lhs(void); | 
|---|
| 354 | void untyped_rhs(int, char *); | 
|---|
| 355 | void used_reserved(char *); | 
|---|
| 356 | void verbose(void); | 
|---|
| 357 | void write_section(const char **); | 
|---|