defs.h revision 240517
1240517Sbapt/* $Id: defs.h,v 1.37 2012/05/26 15:23:00 tom Exp $ */ 2234949Sbapt 3234949Sbapt#ifdef HAVE_CONFIG_H 4234949Sbapt#include <config.h> 5234949Sbapt#endif 6234949Sbapt 7234949Sbapt#include <stdlib.h> 8234949Sbapt#include <string.h> 9234949Sbapt#include <errno.h> 10234949Sbapt#include <assert.h> 11234949Sbapt#include <ctype.h> 12234949Sbapt#include <stdio.h> 13234949Sbapt 14240517Sbapt#if defined(__cplusplus) /* __cplusplus, etc. */ 15240517Sbapt#define class myClass 16240517Sbapt#endif 17240517Sbapt 18234949Sbapt#define YYMAJOR 1 19234949Sbapt#define YYMINOR 9 20234949Sbapt 21234949Sbapt#define CONCAT(first,second) first #second 22234949Sbapt#define CONCAT1(string,number) CONCAT(string, number) 23234949Sbapt#define CONCAT2(first,second) #first "." #second 24234949Sbapt 25234949Sbapt#ifdef YYPATCH 26234949Sbapt#define VSTRING(a,b) CONCAT2(a,b) CONCAT1(" ",YYPATCH) 27234949Sbapt#else 28234949Sbapt#define VSTRING(a,b) CONCAT2(a,b) 29234949Sbapt#endif 30234949Sbapt 31234949Sbapt#define VERSION VSTRING(YYMAJOR, YYMINOR) 32234949Sbapt 33234949Sbapt/* machine-dependent definitions */ 34234949Sbapt/* the following definitions are for the Tahoe */ 35234949Sbapt/* they might have to be changed for other machines */ 36234949Sbapt 37234949Sbapt/* MAXCHAR is the largest unsigned character value */ 38234949Sbapt/* MAXSHORT is the largest value of a C short */ 39234949Sbapt/* MINSHORT is the most negative value of a C short */ 40234949Sbapt/* MAXTABLE is the maximum table size */ 41234949Sbapt/* BITS_PER_WORD is the number of bits in a C unsigned */ 42234949Sbapt/* WORDSIZE computes the number of words needed to */ 43234949Sbapt/* store n bits */ 44234949Sbapt/* BIT returns the value of the n-th bit starting */ 45234949Sbapt/* from r (0-indexed) */ 46234949Sbapt/* SETBIT sets the n-th bit starting from r */ 47234949Sbapt 48234949Sbapt#define MAXCHAR 255 49234949Sbapt#define MAXSHORT 32767 50234949Sbapt#define MINSHORT -32768 51234949Sbapt#define MAXTABLE 32500 52234949Sbapt#define BITS_PER_WORD 32 53234949Sbapt#define WORDSIZE(n) (((n)+(BITS_PER_WORD-1))/BITS_PER_WORD) 54234949Sbapt#define BIT(r, n) ((((r)[(n)>>5])>>((n)&31))&1) 55234949Sbapt#define SETBIT(r, n) ((r)[(n)>>5]|=((unsigned)1<<((n)&31))) 56234949Sbapt 57234949Sbapt/* character names */ 58234949Sbapt 59234949Sbapt#define NUL '\0' /* the null character */ 60234949Sbapt#define NEWLINE '\n' /* line feed */ 61234949Sbapt#define SP ' ' /* space */ 62234949Sbapt#define BS '\b' /* backspace */ 63234949Sbapt#define HT '\t' /* horizontal tab */ 64234949Sbapt#define VT '\013' /* vertical tab */ 65234949Sbapt#define CR '\r' /* carriage return */ 66234949Sbapt#define FF '\f' /* form feed */ 67234949Sbapt#define QUOTE '\'' /* single quote */ 68234949Sbapt#define DOUBLE_QUOTE '\"' /* double quote */ 69234949Sbapt#define BACKSLASH '\\' /* backslash */ 70234949Sbapt 71234949Sbapt#define UCH(c) (unsigned char)(c) 72234949Sbapt 73234949Sbapt/* defines for constructing filenames */ 74234949Sbapt 75234949Sbapt#if defined(VMS) 76234949Sbapt#define CODE_SUFFIX "_code.c" 77234949Sbapt#define DEFINES_SUFFIX "_tab.h" 78234949Sbapt#define EXTERNS_SUFFIX "_tab.i" 79234949Sbapt#define OUTPUT_SUFFIX "_tab.c" 80234949Sbapt#else 81234949Sbapt#define CODE_SUFFIX ".code.c" 82234949Sbapt#define DEFINES_SUFFIX ".tab.h" 83234949Sbapt#define EXTERNS_SUFFIX ".tab.i" 84234949Sbapt#define OUTPUT_SUFFIX ".tab.c" 85234949Sbapt#endif 86234949Sbapt#define VERBOSE_SUFFIX ".output" 87234949Sbapt#define GRAPH_SUFFIX ".dot" 88234949Sbapt 89234949Sbapt/* keyword codes */ 90234949Sbapt 91234949Sbapt#define TOKEN 0 92234949Sbapt#define LEFT 1 93234949Sbapt#define RIGHT 2 94234949Sbapt#define NONASSOC 3 95234949Sbapt#define MARK 4 96234949Sbapt#define TEXT 5 97234949Sbapt#define TYPE 6 98234949Sbapt#define START 7 99234949Sbapt#define UNION 8 100234949Sbapt#define IDENT 9 101234949Sbapt#define EXPECT 10 102234949Sbapt#define EXPECT_RR 11 103234949Sbapt#define PURE_PARSER 12 104234949Sbapt#define PARSE_PARAM 13 105234949Sbapt#define LEX_PARAM 14 106234949Sbapt#define POSIX_YACC 15 107234949Sbapt 108234949Sbapt/* symbol classes */ 109234949Sbapt 110234949Sbapt#define UNKNOWN 0 111234949Sbapt#define TERM 1 112234949Sbapt#define NONTERM 2 113234949Sbapt 114234949Sbapt/* the undefined value */ 115234949Sbapt 116234949Sbapt#define UNDEFINED (-1) 117234949Sbapt 118234949Sbapt/* action codes */ 119234949Sbapt 120234949Sbapt#define SHIFT 1 121234949Sbapt#define REDUCE 2 122234949Sbapt 123234949Sbapt/* character macros */ 124234949Sbapt 125234949Sbapt#define IS_IDENT(c) (isalnum(c) || (c) == '_' || (c) == '.' || (c) == '$') 126234949Sbapt#define IS_OCTAL(c) ((c) >= '0' && (c) <= '7') 127234949Sbapt#define NUMERIC_VALUE(c) ((c) - '0') 128234949Sbapt 129234949Sbapt/* symbol macros */ 130234949Sbapt 131234949Sbapt#define ISTOKEN(s) ((s) < start_symbol) 132234949Sbapt#define ISVAR(s) ((s) >= start_symbol) 133234949Sbapt 134234949Sbapt/* storage allocation macros */ 135234949Sbapt 136234949Sbapt#define CALLOC(k,n) (calloc((size_t)(k),(size_t)(n))) 137234949Sbapt#define FREE(x) (free((char*)(x))) 138234949Sbapt#define MALLOC(n) (malloc((size_t)(n))) 139240517Sbapt#define TMALLOC(t,n) ((t*) malloc((size_t)(n) * sizeof(t))) 140234949Sbapt#define NEW(t) ((t*)allocate(sizeof(t))) 141234949Sbapt#define NEW2(n,t) ((t*)allocate(((size_t)(n)*sizeof(t)))) 142234949Sbapt#define REALLOC(p,n) (realloc((char*)(p),(size_t)(n))) 143240517Sbapt#define TREALLOC(t,p,n) ((t*)realloc((char*)(p), (size_t)(n) * sizeof(t))) 144234949Sbapt 145234949Sbapt#define DO_FREE(x) if (x) { FREE(x); x = 0; } 146234949Sbapt 147234949Sbapt#define NO_SPACE(p) if (p == 0) no_space(); assert(p != 0) 148234949Sbapt 149234949Sbapt/* messages */ 150234949Sbapt#define PLURAL(n) ((n) > 1 ? "s" : "") 151234949Sbapt 152234949Sbapttypedef char Assoc_t; 153234949Sbapttypedef char Class_t; 154234949Sbapttypedef short Index_t; 155234949Sbapttypedef short Value_t; 156234949Sbapt 157234949Sbapt/* the structure of a symbol table entry */ 158234949Sbapt 159234949Sbapttypedef struct bucket bucket; 160234949Sbaptstruct bucket 161234949Sbapt{ 162234949Sbapt struct bucket *link; 163234949Sbapt struct bucket *next; 164234949Sbapt char *name; 165234949Sbapt char *tag; 166234949Sbapt Value_t value; 167234949Sbapt Index_t index; 168234949Sbapt Value_t prec; 169234949Sbapt Class_t class; 170234949Sbapt Assoc_t assoc; 171234949Sbapt}; 172234949Sbapt 173234949Sbapt/* the structure of the LR(0) state machine */ 174234949Sbapt 175234949Sbapttypedef struct core core; 176234949Sbaptstruct core 177234949Sbapt{ 178234949Sbapt struct core *next; 179234949Sbapt struct core *link; 180234949Sbapt Value_t number; 181234949Sbapt Value_t accessing_symbol; 182234949Sbapt Value_t nitems; 183234949Sbapt Value_t items[1]; 184234949Sbapt}; 185234949Sbapt 186234949Sbapt/* the structure used to record shifts */ 187234949Sbapt 188234949Sbapttypedef struct shifts shifts; 189234949Sbaptstruct shifts 190234949Sbapt{ 191234949Sbapt struct shifts *next; 192234949Sbapt Value_t number; 193234949Sbapt Value_t nshifts; 194234949Sbapt Value_t shift[1]; 195234949Sbapt}; 196234949Sbapt 197234949Sbapt/* the structure used to store reductions */ 198234949Sbapt 199234949Sbapttypedef struct reductions reductions; 200234949Sbaptstruct reductions 201234949Sbapt{ 202234949Sbapt struct reductions *next; 203234949Sbapt Value_t number; 204234949Sbapt Value_t nreds; 205234949Sbapt Value_t rules[1]; 206234949Sbapt}; 207234949Sbapt 208234949Sbapt/* the structure used to represent parser actions */ 209234949Sbapt 210234949Sbapttypedef struct action action; 211234949Sbaptstruct action 212234949Sbapt{ 213234949Sbapt struct action *next; 214234949Sbapt Value_t symbol; 215234949Sbapt Value_t number; 216234949Sbapt Value_t prec; 217234949Sbapt char action_code; 218234949Sbapt Assoc_t assoc; 219234949Sbapt char suppressed; 220234949Sbapt}; 221234949Sbapt 222234949Sbapt/* the structure used to store parse/lex parameters */ 223234949Sbapttypedef struct param param; 224234949Sbaptstruct param 225234949Sbapt{ 226234949Sbapt struct param *next; 227234949Sbapt char *name; /* parameter name */ 228234949Sbapt char *type; /* everything before parameter name */ 229234949Sbapt char *type2; /* everything after parameter name */ 230234949Sbapt}; 231234949Sbapt 232234949Sbapt/* global variables */ 233234949Sbapt 234234949Sbaptextern char dflag; 235234949Sbaptextern char gflag; 236234949Sbaptextern char iflag; 237234949Sbaptextern char lflag; 238234949Sbaptextern char rflag; 239234949Sbaptextern char sflag; 240234949Sbaptextern char tflag; 241234949Sbaptextern char vflag; 242234949Sbaptextern const char *symbol_prefix; 243234949Sbapt 244234949Sbaptextern const char *myname; 245234949Sbaptextern char *cptr; 246234949Sbaptextern char *line; 247234949Sbaptextern int lineno; 248234949Sbaptextern int outline; 249234949Sbaptextern int exit_code; 250240517Sbaptextern int pure_parser; 251234949Sbapt 252234949Sbaptextern const char *const banner[]; 253234949Sbaptextern const char *const xdecls[]; 254234949Sbaptextern const char *const tables[]; 255234949Sbaptextern const char *const global_vars[]; 256234949Sbaptextern const char *const impure_vars[]; 257234949Sbaptextern const char *const hdr_defs[]; 258234949Sbaptextern const char *const hdr_vars[]; 259234949Sbaptextern const char *const body_1[]; 260234949Sbaptextern const char *const body_vars[]; 261234949Sbaptextern const char *const body_2[]; 262234949Sbaptextern const char *const body_3[]; 263234949Sbaptextern const char *const trailer[]; 264234949Sbaptextern const char *const trailer_2[]; 265234949Sbapt 266234949Sbaptextern char *code_file_name; 267234949Sbaptextern char *input_file_name; 268234949Sbaptextern char *defines_file_name; 269234949Sbaptextern char *externs_file_name; 270234949Sbapt 271234949Sbaptextern FILE *action_file; 272234949Sbaptextern FILE *code_file; 273234949Sbaptextern FILE *defines_file; 274234949Sbaptextern FILE *externs_file; 275234949Sbaptextern FILE *input_file; 276234949Sbaptextern FILE *output_file; 277234949Sbaptextern FILE *text_file; 278234949Sbaptextern FILE *union_file; 279234949Sbaptextern FILE *verbose_file; 280234949Sbaptextern FILE *graph_file; 281234949Sbapt 282234949Sbaptextern int nitems; 283234949Sbaptextern int nrules; 284234949Sbaptextern int nsyms; 285234949Sbaptextern int ntokens; 286234949Sbaptextern int nvars; 287234949Sbaptextern int ntags; 288234949Sbapt 289234949Sbaptextern char unionized; 290234949Sbaptextern char line_format[]; 291234949Sbapt 292234949Sbaptextern Value_t start_symbol; 293234949Sbaptextern char **symbol_name; 294234949Sbaptextern char **symbol_pname; 295234949Sbaptextern Value_t *symbol_value; 296234949Sbaptextern Value_t *symbol_prec; 297234949Sbaptextern char *symbol_assoc; 298234949Sbapt 299234949Sbaptextern Value_t *ritem; 300234949Sbaptextern Value_t *rlhs; 301234949Sbaptextern Value_t *rrhs; 302234949Sbaptextern Value_t *rprec; 303234949Sbaptextern Assoc_t *rassoc; 304234949Sbapt 305234949Sbaptextern Value_t **derives; 306234949Sbaptextern char *nullable; 307234949Sbapt 308234949Sbaptextern bucket *first_symbol; 309234949Sbaptextern bucket *last_symbol; 310234949Sbapt 311234949Sbaptextern int nstates; 312234949Sbaptextern core *first_state; 313234949Sbaptextern shifts *first_shift; 314234949Sbaptextern reductions *first_reduction; 315234949Sbaptextern Value_t *accessing_symbol; 316234949Sbaptextern core **state_table; 317234949Sbaptextern shifts **shift_table; 318234949Sbaptextern reductions **reduction_table; 319234949Sbaptextern unsigned *LA; 320234949Sbaptextern Value_t *LAruleno; 321234949Sbaptextern Value_t *lookaheads; 322234949Sbaptextern Value_t *goto_map; 323234949Sbaptextern Value_t *from_state; 324234949Sbaptextern Value_t *to_state; 325234949Sbapt 326234949Sbaptextern action **parser; 327234949Sbaptextern int SRexpect; 328234949Sbaptextern int RRexpect; 329234949Sbaptextern int SRtotal; 330234949Sbaptextern int RRtotal; 331234949Sbaptextern Value_t *SRconflicts; 332234949Sbaptextern Value_t *RRconflicts; 333234949Sbaptextern Value_t *defred; 334234949Sbaptextern Value_t *rules_used; 335234949Sbaptextern Value_t nunused; 336234949Sbaptextern Value_t final_state; 337234949Sbapt 338234949Sbaptextern Value_t *itemset; 339234949Sbaptextern Value_t *itemsetend; 340234949Sbaptextern unsigned *ruleset; 341234949Sbapt 342234949Sbaptextern param *lex_param; 343234949Sbaptextern param *parse_param; 344234949Sbapt 345234949Sbapt/* global functions */ 346234949Sbapt 347234949Sbaptextern bucket *lookup(const char *); 348234949Sbaptextern bucket *make_bucket(const char *); 349234949Sbapt 350234949Sbapt#ifndef GCC_NORETURN 351240517Sbapt#if defined(__dead2) 352240517Sbapt#define GCC_NORETURN __dead2 353240517Sbapt#elif defined(__dead) 354240517Sbapt#define GCC_NORETURN __dead 355240517Sbapt#else 356234949Sbapt#define GCC_NORETURN /* nothing */ 357234949Sbapt#endif 358240517Sbapt#endif 359234949Sbapt 360234949Sbapt#ifndef GCC_UNUSED 361240517Sbapt#if defined(__unused) 362240517Sbapt#define GCC_UNUSED __unused 363240517Sbapt#else 364234949Sbapt#define GCC_UNUSED /* nothing */ 365234949Sbapt#endif 366240517Sbapt#endif 367234949Sbapt 368234949Sbapt/* closure.c */ 369234949Sbaptextern void closure(Value_t * nucleus, int n); 370234949Sbaptextern void finalize_closure(void); 371234949Sbaptextern void set_first_derives(void); 372234949Sbapt 373234949Sbapt/* error.c */ 374234949Sbaptextern void default_action_warning(void); 375234949Sbaptextern void dollar_error(int a_lineno, char *a_line, char *a_cptr) GCC_NORETURN; 376234949Sbaptextern void dollar_warning(int a_lineno, int i); 377234949Sbaptextern void fatal(const char *msg) GCC_NORETURN; 378234949Sbaptextern void illegal_character(char *c_cptr) GCC_NORETURN; 379234949Sbaptextern void illegal_tag(int t_lineno, char *t_line, char *t_cptr) GCC_NORETURN; 380234949Sbaptextern void missing_brace(void) GCC_NORETURN; 381234949Sbaptextern void no_grammar(void) GCC_NORETURN; 382234949Sbaptextern void no_space(void) GCC_NORETURN; 383234949Sbaptextern void open_error(const char *filename) GCC_NORETURN; 384234949Sbaptextern void over_unionized(char *u_cptr) GCC_NORETURN; 385234949Sbaptextern void prec_redeclared(void); 386234949Sbaptextern void reprec_warning(char *s); 387234949Sbaptextern void restarted_warning(void); 388234949Sbaptextern void retyped_warning(char *s); 389234949Sbaptextern void revalued_warning(char *s); 390234949Sbaptextern void syntax_error(int st_lineno, char *st_line, char *st_cptr) GCC_NORETURN; 391234949Sbaptextern void terminal_lhs(int s_lineno) GCC_NORETURN; 392234949Sbaptextern void terminal_start(char *s) GCC_NORETURN; 393234949Sbaptextern void tokenized_start(char *s) GCC_NORETURN; 394234949Sbaptextern void undefined_goal(char *s) GCC_NORETURN; 395234949Sbaptextern void undefined_symbol_warning(char *s); 396234949Sbaptextern void unexpected_EOF(void) GCC_NORETURN; 397234949Sbaptextern void unknown_rhs(int i) GCC_NORETURN; 398234949Sbaptextern void unterminated_action(int a_lineno, char *a_line, char *a_cptr) GCC_NORETURN; 399234949Sbaptextern void unterminated_comment(int c_lineno, char *c_line, char *c_cptr) GCC_NORETURN; 400234949Sbaptextern void unterminated_string(int s_lineno, char *s_line, char *s_cptr) GCC_NORETURN; 401234949Sbaptextern void unterminated_text(int t_lineno, char *t_line, char *t_cptr) GCC_NORETURN; 402234949Sbaptextern void unterminated_union(int u_lineno, char *u_line, char *u_cptr) GCC_NORETURN; 403234949Sbaptextern void untyped_lhs(void) GCC_NORETURN; 404234949Sbaptextern void untyped_rhs(int i, char *s) GCC_NORETURN; 405234949Sbaptextern void used_reserved(char *s) GCC_NORETURN; 406234949Sbapt 407234949Sbapt/* graph.c */ 408234949Sbaptextern void graph(void); 409234949Sbapt 410234949Sbapt/* lalr.c */ 411234949Sbaptextern void create_symbol_table(void); 412234949Sbaptextern void free_symbol_table(void); 413234949Sbaptextern void free_symbols(void); 414234949Sbapt 415234949Sbapt/* lalr.c */ 416234949Sbaptextern void lalr(void); 417234949Sbapt 418234949Sbapt/* lr0.c */ 419234949Sbaptextern void lr0(void); 420234949Sbaptextern void show_cores(void); 421234949Sbaptextern void show_ritems(void); 422234949Sbaptextern void show_rrhs(void); 423234949Sbaptextern void show_shifts(void); 424234949Sbapt 425234949Sbapt/* main.c */ 426234949Sbaptextern void *allocate(size_t n); 427234949Sbaptextern void done(int k) GCC_NORETURN; 428234949Sbapt 429234949Sbapt/* mkpar.c */ 430234949Sbaptextern void free_parser(void); 431234949Sbaptextern void make_parser(void); 432234949Sbapt 433234949Sbapt/* output.c */ 434234949Sbaptextern void output(void); 435234949Sbapt 436234949Sbapt/* reader.c */ 437234949Sbaptextern void reader(void); 438234949Sbapt 439234949Sbapt/* skeleton.c */ 440240517Sbaptextern void write_section(FILE * fp, const char *const section[]); 441234949Sbapt 442234949Sbapt/* verbose.c */ 443234949Sbaptextern void verbose(void); 444234949Sbapt 445234949Sbapt/* warshall.c */ 446234949Sbaptextern void reflexive_transitive_closure(unsigned *R, int n); 447234949Sbapt 448234949Sbapt#ifdef NO_LEAKS 449234949Sbaptextern void lr0_leaks(void); 450234949Sbaptextern void lalr_leaks(void); 451234949Sbaptextern void mkpar_leaks(void); 452234949Sbaptextern void output_leaks(void); 453234949Sbaptextern void reader_leaks(void); 454234949Sbapt#endif 455