defs.h 5.8 KB

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