home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / compcomp / byacc / defs.h < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-08  |  6.8 KB  |  306 lines

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