00001 #include <assert.h> 00002 #include <ctype.h> 00003 #include <stdio.h> 00004 00005 00006 /* machine-dependent definitions */ 00007 /* the following definitions are for the Tahoe */ 00008 /* they might have to be changed for other machines */ 00009 00010 /* MAXCHAR is the largest unsigned character value */ 00011 /* MAXSHORT is the largest value of a C short */ 00012 /* MINSHORT is the most negative value of a C short */ 00013 /* MAXTABLE is the maximum table size */ 00014 /* BITS_PER_WORD is the number of bits in a C unsigned */ 00015 /* WORDSIZE computes the number of words needed to */ 00016 /* store n bits */ 00017 /* BIT returns the value of the n-th bit starting */ 00018 /* from r (0-indexed) */ 00019 /* SETBIT sets the n-th bit starting from r */ 00020 00021 #if !__STDC__ 00022 #define MAXCHAR 255 00023 #define MAXSHORT 32767 00024 #define MINSHORT -32768 00025 #define BITS_PER_WORD 32 00026 #else 00027 #include <limits.h> 00028 #define MAXCHAR UCHAR_MAX 00029 #define MAXSHORT SHRT_MAX 00030 #define MINSHORT SHRT_MIN 00031 #define BITS_PER_WORD (INT_MAX == 32767 ? 16 : 32) 00032 #define BITS_PER_BPW (INT_MAX == 32767 ? 4 : 5) 00033 #endif 00034 #define MAXTABLE 32500 00035 #define WORDSIZE(n) (((n)+(BITS_PER_WORD-1))/BITS_PER_WORD) 00036 #define BIT(r, n) ((((r)[(n)>>BITS_PER_BPW])>>((n)&(BITS_PER_WORD-1)))&1) 00037 #define SETBIT(r, n) ((r)[(n)>>BITS_PER_BPW]|=((unsigned)1<<((n)&(BITS_PER_WORD-1)))) 00038 00039 00040 /* character names */ 00041 00042 #define NUL '\0' /* the null character */ 00043 #define NEWLINE '\n' /* line feed */ 00044 #define SP ' ' /* space */ 00045 #define BS '\b' /* backspace */ 00046 #define HT '\t' /* horizontal tab */ 00047 #define VT '\013' /* vertical tab */ 00048 #define CR '\r' /* carriage return */ 00049 #define FF '\f' /* form feed */ 00050 #define QUOTE '\'' /* single quote */ 00051 #define DOUBLE_QUOTE '\"' /* double quote */ 00052 #define BACKSLASH '\\' /* backslash */ 00053 00054 00055 /* defines for constructing filenames */ 00056 00057 #define CODE_SUFFIX ".code.c" 00058 #define DEFINES_SUFFIX ".tab.h" 00059 #define OUTPUT_SUFFIX ".tab.c" 00060 #define VERBOSE_SUFFIX ".output" 00061 00062 00063 /* keyword codes */ 00064 00065 #define TOKEN 0 00066 #define LEFT 1 00067 #define RIGHT 2 00068 #define NONASSOC 3 00069 #define MARK 4 00070 #define TEXT 5 00071 #define TYPE 6 00072 #define START 7 00073 #define UNION 8 00074 #define IDENT 9 00075 00076 00077 /* symbol classes */ 00078 00079 #define UNKNOWN 0 00080 #define TERM 1 00081 #define NONTERM 2 00082 00083 00084 /* the undefined value */ 00085 00086 #define UNDEFINED (-1) 00087 00088 00089 /* action codes */ 00090 00091 #define SHIFT 1 00092 #define REDUCE 2 00093 00094 00095 /* character macros */ 00096 00097 #define IS_IDENT(c) (isalnum(c) || (c) == '_' || (c) == '.' || (c) == '$') 00098 #define IS_OCTAL(c) ((c) >= '0' && (c) <= '7') 00099 #define NUMERIC_VALUE(c) ((c) - '0') 00100 00101 00102 /* symbol macros */ 00103 00104 #define ISTOKEN(s) ((s) < start_symbol) 00105 #define ISVAR(s) ((s) >= start_symbol) 00106 00107 00108 /* storage allocation macros */ 00109 00110 #define CALLOC(k,n) (calloc((unsigned)(k),(unsigned)(n))) 00111 #define FREE(x) (free((char*)(x))) 00112 #define MALLOC(n) (malloc((unsigned)(n))) 00113 #define NEW(t) ((t*)allocate(sizeof(t))) 00114 #define NEW2(n,t) ((t*)allocate((unsigned)((n)*sizeof(t)))) 00115 #define REALLOC(p,n) (realloc((char*)(p),(unsigned)(n))) 00116 00117 00118 /* the structure of a symbol table entry */ 00119 00120 typedef struct bucket bucket; 00121 struct bucket 00122 { 00123 struct bucket *link; 00124 struct bucket *next; 00125 char *name; 00126 char *tag; 00127 short value; 00128 short index; 00129 short prec; 00130 char class; 00131 char assoc; 00132 }; 00133 00134 00135 /* the structure of the LR(0) state machine */ 00136 00137 typedef struct core core; 00138 struct core 00139 { 00140 struct core *next; 00141 struct core *link; 00142 short number; 00143 short accessing_symbol; 00144 short nitems; 00145 short items[1]; 00146 }; 00147 00148 00149 /* the structure used to record shifts */ 00150 00151 typedef struct shifts shifts; 00152 struct shifts 00153 { 00154 struct shifts *next; 00155 short number; 00156 short nshifts; 00157 short shift[1]; 00158 }; 00159 00160 00161 /* the structure used to store reductions */ 00162 00163 typedef struct reductions reductions; 00164 struct reductions 00165 { 00166 struct reductions *next; 00167 short number; 00168 short nreds; 00169 short rules[1]; 00170 }; 00171 00172 00173 /* the structure used to represent parser actions */ 00174 00175 typedef struct action action; 00176 struct action 00177 { 00178 struct action *next; 00179 short symbol; 00180 short number; 00181 short prec; 00182 char action_code; 00183 char assoc; 00184 char suppressed; 00185 }; 00186 00187 00188 /* global variables */ 00189 00190 extern char dflag; 00191 extern char lflag; 00192 extern char rflag; 00193 extern char tflag; 00194 extern char vflag; 00195 extern char *symbol_prefix; 00196 00197 extern char *myname; 00198 extern char *cptr; 00199 extern char *line; 00200 extern int lineno; 00201 extern int outline; 00202 00203 extern char *banner[]; 00204 extern char *tables[]; 00205 extern char *header[]; 00206 extern char *body[]; 00207 extern char *trailer[]; 00208 00209 extern char *action_file_name; 00210 extern char *code_file_name; 00211 extern char *defines_file_name; 00212 extern char *input_file_name; 00213 extern char *output_file_name; 00214 extern char *text_file_name; 00215 extern char *union_file_name; 00216 extern char *verbose_file_name; 00217 00218 extern FILE *action_file; 00219 extern FILE *code_file; 00220 extern FILE *defines_file; 00221 extern FILE *input_file; 00222 extern FILE *output_file; 00223 extern FILE *text_file; 00224 extern FILE *union_file; 00225 extern FILE *verbose_file; 00226 00227 extern int nitems; 00228 extern int nrules; 00229 extern int nsyms; 00230 extern int ntokens; 00231 extern int nvars; 00232 extern int ntags; 00233 00234 extern char unionized; 00235 extern char line_format[]; 00236 00237 extern int start_symbol; 00238 extern char **symbol_name; 00239 extern short *symbol_value; 00240 extern short *symbol_prec; 00241 extern char *symbol_assoc; 00242 00243 extern short *ritem; 00244 extern short *rlhs; 00245 extern short *rrhs; 00246 extern short *rprec; 00247 extern char *rassoc; 00248 00249 extern short **derives; 00250 extern char *nullable; 00251 00252 extern bucket *first_symbol; 00253 extern bucket *last_symbol; 00254 00255 extern int nstates; 00256 extern core *first_state; 00257 extern shifts *first_shift; 00258 extern reductions *first_reduction; 00259 extern short *accessing_symbol; 00260 extern core **state_table; 00261 extern shifts **shift_table; 00262 extern reductions **reduction_table; 00263 extern unsigned *LA; 00264 extern short *LAruleno; 00265 extern short *lookaheads; 00266 extern short *goto_map; 00267 extern short *from_state; 00268 extern short *to_state; 00269 00270 extern action **parser; 00271 extern int SRtotal; 00272 extern int RRtotal; 00273 extern short *SRconflicts; 00274 extern short *RRconflicts; 00275 extern short *defred; 00276 extern short *rules_used; 00277 extern short nunused; 00278 extern short final_state; 00279 00280 /* global functions */ 00281 00282 extern char *allocate(); 00283 extern bucket *lookup(); 00284 extern bucket *make_bucket(); 00285 00286 00287 /* system variables */ 00288 00289 extern int errno; 00290 00291 00292 /* system functions */ 00293 00294 extern void free(); 00295 extern char *calloc(); 00296 extern char *malloc(); 00297 extern char *realloc(); 00298 extern char *strcpy();
1.5.8