1/* $NetBSD: defs.h,v 1.1.1.4 2011/09/10 21:22:00 christos Exp $ */ 2 3#if HAVE_NBTOOL_CONFIG_H 4#include "nbtool_config.h" 5#endif 6/* Id: defs.h,v 1.35 2011/09/07 08:55:03 tom Exp */ 7 8#ifdef HAVE_CONFIG_H 9#include <config.h> 10#endif 11 12#include <stdlib.h> 13#include <string.h> 14#include <errno.h> 15#include <assert.h> 16#include <ctype.h> 17#include <stdio.h> 18 19#define YYMAJOR 1 20#define YYMINOR 9 21 22#define CONCAT(first,second) first #second 23#define CONCAT1(string,number) CONCAT(string, number) 24#define CONCAT2(first,second) #first "." #second 25 26#ifdef YYPATCH 27#define VSTRING(a,b) CONCAT2(a,b) CONCAT1(" ",YYPATCH) 28#else 29#define VSTRING(a,b) CONCAT2(a,b) 30#endif 31 32#define VERSION VSTRING(YYMAJOR, YYMINOR) 33 34/* machine-dependent definitions */ 35/* the following definitions are for the Tahoe */ 36/* they might have to be changed for other machines */ 37 38/* MAXCHAR is the largest unsigned character value */ 39/* MAXSHORT is the largest value of a C short */ 40/* MINSHORT is the most negative value of a C short */ 41/* MAXTABLE is the maximum table size */ 42/* BITS_PER_WORD is the number of bits in a C unsigned */ 43/* WORDSIZE computes the number of words needed to */ 44/* store n bits */ 45/* BIT returns the value of the n-th bit starting */ 46/* from r (0-indexed) */ 47/* SETBIT sets the n-th bit starting from r */ 48 49#define MAXCHAR 255 50#define MAXSHORT 32767 51#define MINSHORT -32768 52#define MAXTABLE 32500 53#define BITS_PER_WORD 32 54#define WORDSIZE(n) (((n)+(BITS_PER_WORD-1))/BITS_PER_WORD) 55#define BIT(r, n) ((((r)[(n)>>5])>>((n)&31))&1) 56#define SETBIT(r, n) ((r)[(n)>>5]|=((unsigned)1<<((n)&31))) 57 58/* character names */ 59 60#define NUL '\0' /* the null character */ 61#define NEWLINE '\n' /* line feed */ 62#define SP ' ' /* space */ 63#define BS '\b' /* backspace */ 64#define HT '\t' /* horizontal tab */ 65#define VT '\013' /* vertical tab */ 66#define CR '\r' /* carriage return */ 67#define FF '\f' /* form feed */ 68#define QUOTE '\'' /* single quote */ 69#define DOUBLE_QUOTE '\"' /* double quote */ 70#define BACKSLASH '\\' /* backslash */ 71 72#define UCH(c) (unsigned char)(c) 73 74/* defines for constructing filenames */ 75 76#if defined(VMS) 77#define CODE_SUFFIX "_code.c" 78#define DEFINES_SUFFIX "_tab.h" 79#define EXTERNS_SUFFIX "_tab.i" 80#define OUTPUT_SUFFIX "_tab.c" 81#else 82#define CODE_SUFFIX ".code.c" 83#define DEFINES_SUFFIX ".tab.h" 84#define EXTERNS_SUFFIX ".tab.i" 85#define OUTPUT_SUFFIX ".tab.c" 86#endif 87#define VERBOSE_SUFFIX ".output" 88#define GRAPH_SUFFIX ".dot" 89 90/* keyword codes */ 91 92#define TOKEN 0 93#define LEFT 1 94#define RIGHT 2 95#define NONASSOC 3 96#define MARK 4 97#define TEXT 5 98#define TYPE 6 99#define START 7 100#define UNION 8 101#define IDENT 9 102#define EXPECT 10 103#define EXPECT_RR 11 104#define PURE_PARSER 12 105#define PARSE_PARAM 13 106#define LEX_PARAM 14 107#define POSIX_YACC 15 108 109/* symbol classes */ 110 111#define UNKNOWN 0 112#define TERM 1 113#define NONTERM 2 114 115/* the undefined value */ 116 117#define UNDEFINED (-1) 118 119/* action codes */ 120 121#define SHIFT 1 122#define REDUCE 2 123 124/* character macros */ 125 126#define IS_IDENT(c) (isalnum(c) || (c) == '_' || (c) == '.' || (c) == '$') 127#define IS_OCTAL(c) ((c) >= '0' && (c) <= '7') 128#define NUMERIC_VALUE(c) ((c) - '0') 129 130/* symbol macros */ 131 132#define ISTOKEN(s) ((s) < start_symbol) 133#define ISVAR(s) ((s) >= start_symbol) 134 135/* storage allocation macros */ 136 137#define CALLOC(k,n) (calloc((size_t)(k),(size_t)(n))) 138#define FREE(x) (free((char*)(x))) 139#define MALLOC(n) (malloc((size_t)(n))) 140#define NEW(t) ((t*)allocate(sizeof(t))) 141#define NEW2(n,t) ((t*)allocate(((size_t)(n)*sizeof(t)))) 142#define REALLOC(p,n) (realloc((char*)(p),(size_t)(n))) 143 144#define DO_FREE(x) if (x) { FREE(x); x = 0; } 145 146#define NO_SPACE(p) if (p == 0) no_space(); assert(p != 0) 147 148/* messages */ 149#define PLURAL(n) ((n) > 1 ? "s" : "") 150 151typedef char Assoc_t; 152typedef char Class_t; 153typedef short Index_t; 154typedef short Value_t; 155 156/* the structure of a symbol table entry */ 157 158typedef struct bucket bucket; 159struct bucket 160{ 161 struct bucket *link; 162 struct bucket *next; 163 char *name; 164 char *tag; 165 Value_t value; 166 Index_t index; 167 Value_t prec; 168 Class_t class; 169 Assoc_t assoc; 170}; 171 172/* the structure of the LR(0) state machine */ 173 174typedef struct core core; 175struct core 176{ 177 struct core *next; 178 struct core *link; 179 Value_t number; 180 Value_t accessing_symbol; 181 Value_t nitems; 182 Value_t items[1]; 183}; 184 185/* the structure used to record shifts */ 186 187typedef struct shifts shifts; 188struct shifts 189{ 190 struct shifts *next; 191 Value_t number; 192 Value_t nshifts; 193 Value_t shift[1]; 194}; 195 196/* the structure used to store reductions */ 197 198typedef struct reductions reductions; 199struct reductions 200{ 201 struct reductions *next; 202 Value_t number; 203 Value_t nreds; 204 Value_t rules[1]; 205}; 206 207/* the structure used to represent parser actions */ 208 209typedef struct action action; 210struct action 211{ 212 struct action *next; 213 Value_t symbol; 214 Value_t number; 215 Value_t prec; 216 char action_code; 217 Assoc_t assoc; 218 char suppressed; 219}; 220 221/* the structure used to store parse/lex parameters */ 222typedef struct param param; 223struct param 224{ 225 struct param *next; 226 char *name; /* parameter name */ 227 char *type; /* everything before parameter name */ 228 char *type2; /* everything after parameter name */ 229}; 230 231/* global variables */ 232 233extern char dflag; 234extern char gflag; 235extern char iflag; 236extern char lflag; 237extern char rflag; 238extern char tflag; 239extern char vflag; 240extern const char *symbol_prefix; 241 242extern const char *myname; 243extern char *cptr; 244extern char *line; 245extern int lineno; 246extern int outline; 247extern int exit_code; 248extern int pure_parser; 249 250extern const char *const banner[]; 251extern const char *const xdecls[]; 252extern const char *const tables[]; 253extern const char *const global_vars[]; 254extern const char *const impure_vars[]; 255extern const char *const hdr_defs[]; 256extern const char *const hdr_vars[]; 257extern const char *const body_1[]; 258extern const char *const body_vars[]; 259extern const char *const body_2[]; 260extern const char *const body_3[]; 261extern const char *const trailer[]; 262extern const char *const trailer_2[]; 263 264extern char *code_file_name; 265extern char *input_file_name; 266extern char *defines_file_name; 267extern char *externs_file_name; 268 269extern FILE *action_file; 270extern FILE *code_file; 271extern FILE *defines_file; 272extern FILE *externs_file; 273extern FILE *input_file; 274extern FILE *output_file; 275extern FILE *text_file; 276extern FILE *union_file; 277extern FILE *verbose_file; 278extern FILE *graph_file; 279 280extern int nitems; 281extern int nrules; 282extern int nsyms; 283extern int ntokens; 284extern int nvars; 285extern int ntags; 286 287extern char unionized; 288extern char line_format[]; 289 290extern Value_t start_symbol; 291extern char **symbol_name; 292extern char **symbol_pname; 293extern Value_t *symbol_value; 294extern Value_t *symbol_prec; 295extern char *symbol_assoc; 296 297extern Value_t *ritem; 298extern Value_t *rlhs; 299extern Value_t *rrhs; 300extern Value_t *rprec; 301extern Assoc_t *rassoc; 302 303extern Value_t **derives; 304extern char *nullable; 305 306extern bucket *first_symbol; 307extern bucket *last_symbol; 308 309extern int pure_parser; 310extern int nstates; 311extern core *first_state; 312extern shifts *first_shift; 313extern reductions *first_reduction; 314extern Value_t *accessing_symbol; 315extern core **state_table; 316extern shifts **shift_table; 317extern reductions **reduction_table; 318extern unsigned *LA; 319extern Value_t *LAruleno; 320extern Value_t *lookaheads; 321extern Value_t *goto_map; 322extern Value_t *from_state; 323extern Value_t *to_state; 324 325extern action **parser; 326extern int SRexpect; 327extern int RRexpect; 328extern int SRtotal; 329extern int RRtotal; 330extern Value_t *SRconflicts; 331extern Value_t *RRconflicts; 332extern Value_t *defred; 333extern Value_t *rules_used; 334extern Value_t nunused; 335extern Value_t final_state; 336 337extern Value_t *itemset; 338extern Value_t *itemsetend; 339extern unsigned *ruleset; 340 341extern param *lex_param; 342extern param *parse_param; 343 344/* global functions */ 345 346extern bucket *lookup(const char *); 347extern bucket *make_bucket(const char *); 348 349#ifndef GCC_NORETURN 350#define GCC_NORETURN /* nothing */ 351#endif 352 353#ifndef GCC_UNUSED 354#define GCC_UNUSED /* nothing */ 355#endif 356 357/* closure.c */ 358extern void closure(Value_t * nucleus, int n); 359extern void finalize_closure(void); 360extern void set_first_derives(void); 361 362/* error.c */ 363extern void default_action_warning(void); 364extern void dollar_error(int a_lineno, char *a_line, char *a_cptr) GCC_NORETURN; 365extern void dollar_warning(int a_lineno, int i); 366extern void fatal(const char *msg) GCC_NORETURN; 367extern void illegal_character(char *c_cptr) GCC_NORETURN; 368extern void illegal_tag(int t_lineno, char *t_line, char *t_cptr) GCC_NORETURN; 369extern void missing_brace(void) GCC_NORETURN; 370extern void no_grammar(void) GCC_NORETURN; 371extern void no_space(void) GCC_NORETURN; 372extern void open_error(const char *filename) GCC_NORETURN; 373extern void over_unionized(char *u_cptr) GCC_NORETURN; 374extern void prec_redeclared(void); 375extern void reprec_warning(char *s); 376extern void restarted_warning(void); 377extern void retyped_warning(char *s); 378extern void revalued_warning(char *s); 379extern void syntax_error(int st_lineno, char *st_line, char *st_cptr) GCC_NORETURN; 380extern void terminal_lhs(int s_lineno) GCC_NORETURN; 381extern void terminal_start(char *s) GCC_NORETURN; 382extern void tokenized_start(char *s) GCC_NORETURN; 383extern void undefined_goal(char *s) GCC_NORETURN; 384extern void undefined_symbol_warning(char *s); 385extern void unexpected_EOF(void) GCC_NORETURN; 386extern void unknown_rhs(int i) GCC_NORETURN; 387extern void unterminated_action(int a_lineno, char *a_line, char *a_cptr) GCC_NORETURN; 388extern void unterminated_comment(int c_lineno, char *c_line, char *c_cptr) GCC_NORETURN; 389extern void unterminated_string(int s_lineno, char *s_line, char *s_cptr) GCC_NORETURN; 390extern void unterminated_text(int t_lineno, char *t_line, char *t_cptr) GCC_NORETURN; 391extern void unterminated_union(int u_lineno, char *u_line, char *u_cptr) GCC_NORETURN; 392extern void untyped_lhs(void) GCC_NORETURN; 393extern void untyped_rhs(int i, char *s) GCC_NORETURN; 394extern void used_reserved(char *s) GCC_NORETURN; 395 396/* graph.c */ 397extern void graph(void); 398 399/* lalr.c */ 400extern void create_symbol_table(void); 401extern void free_symbol_table(void); 402extern void free_symbols(void); 403 404/* lalr.c */ 405extern void lalr(void); 406 407/* lr0.c */ 408extern void lr0(void); 409extern void show_cores(void); 410extern void show_ritems(void); 411extern void show_rrhs(void); 412extern void show_shifts(void); 413 414/* main.c */ 415extern void *allocate(size_t n); 416extern void done(int k) GCC_NORETURN; 417 418/* mkpar.c */ 419extern void free_parser(void); 420extern void make_parser(void); 421 422/* output.c */ 423extern void output(void); 424 425/* reader.c */ 426extern void reader(void); 427 428/* skeleton.c */ 429extern void write_section(FILE *fp, const char *const section[]); 430 431/* verbose.c */ 432extern void verbose(void); 433 434/* warshall.c */ 435extern void reflexive_transitive_closure(unsigned *R, int n); 436 437#ifdef NO_LEAKS 438extern void lr0_leaks(void); 439extern void lalr_leaks(void); 440extern void mkpar_leaks(void); 441extern void output_leaks(void); 442extern void reader_leaks(void); 443#endif 444