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