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