1 /* Copyright (c) 1991 by the Vrije Universiteit, Amsterdam, the Netherlands.
2 * For full copyright and restrictions on use see the file COPYING in the top
3 * level of the LLgen tree.
9 * An Extended LL(1) Parser Generator
11 * Author : Ceriel J.H. Jacobs
15 * $Id: types.h,v 2.13 1997/02/21 11:27:57 ceriel Exp $
16 * Type and structure definitions
19 typedef int *p_set; /* pointer to bitset */
20 typedef char *p_mem; /* pointer to some core */
24 * structure for a token
26 typedef struct token {
27 int t_tokno; /* Lexical token number */
29 string t_s; /* Attribute is either a string or */
30 int t_v; /* an integer */
32 # define t_string t_x.t_s
33 # define t_num t_x.t_v
40 * structure for the grammar elements
43 int x; /* for lay-out see comment below */
44 int g_lineno; /* element found on this line number */
46 int g_erroneous; /* 1 if element declared erroneous */
53 /* If this is an action with a %substart g_subparse
54 points to the list of startsymbols of the subparser */
55 struct ff_firsts *g_subparse;
61 * Layout of the field x:
63 * 15 ....... 7 6 ........ 3 2 .... 0
64 * -----------------------------------
65 * | unused | | nparams | | type |
66 * -----------------------------------
68 # define EORULE 00 /* End of (sub)rule */
69 # define ACTION 01 /* Imbedded action */
70 # define NONTERM 02 /* A nonterminal */
71 # define TERMINAL 03 /* A terminal */
72 # define TERM 04 /* Something between square brackets */
73 # define ALTERNATION 05 /* Alternation (|) */
74 # define LITERAL 06 /* Also a terminal */
77 * Access macros for the x-field of a grammar element
79 # define g_gettype(p) ((p)->x&07)
80 # define g_getcont(p) ((p)->g_i.g_index)
81 # define g_getterm(p) ((p)->g_i.g_term)
82 # define g_getlink(p) ((p)->g_i.g_link)
83 # define g_getnpar(p) (((p)->x>>3)&017)
84 # define g_settype(p,s) { assert(((unsigned)(s))<=07);(p)->x=((p)->x&~07)|(s);}
85 # define g_setcont(p,s) ((p)->g_i.g_index=(s))
86 # define g_setterm(p,s) ((p)->g_i.g_term = (s))
87 # define g_setlink(p,s) ((p)->g_i.g_link = (s))
88 # define g_setnpar(p,s) { assert(((unsigned)(s))<=017);(p)->x=((p)->x&~0170)|((s)<<3);}
90 # define g_getsubparse(p) ((p)->g_i.g_subparse)
91 # define g_setsubparse(p,s) ((p)->g_i.g_subparse = (s))
94 * Some constants to communicate with the symbol table search routine
96 # define UNKNOWN 00 /* Not equal to NONTERM, TERMINAL or LITERAL */
99 * Some constants for safety
101 # define SAFE 0 /* Indicates that a scan is done, and that the
104 # define SAFESCANDONE 1 /* Indicates that a scan is done, and that the
105 * token will not be skipped
107 # define SCANDONE 2 /* Indicates that a scan is done */
108 # define NOSCANDONE 3 /* Indicates that no scan is done */
109 # define NOSAFETY 4 /* Safety not yet computed */
112 * nonterminal structure
115 int n_flags; /* low order four bits are reserved
116 * the parameter count
118 # define getntparams(p) ((p)->n_flags&017)
119 # define setntparams(p,i) {assert(((unsigned)(i))<=017);(p)->n_flags&=~017;(p)->n_flags|=(i);}
120 # define GENSTATIC 01000 /* set if routine can be made static */
121 # define RECURSIVE 02000 /* Set if the default rule is recursive */
122 # define PARAMS 04000 /* tells if a nonterminal has parameters */
123 # define EMPTY 010000 /* tells if a nonterminal produces empty */
124 # define LOCALS 020000 /* local declarations ? */
125 # define REACHABLE 040000 /* can this nonterminal be reached ? */
126 # define VERBOSE 0100000 /* Set if in LL.output file */
129 # define getntsafe(p) ((p)->n_insafety)
130 # define setntsafe(p,i) {assert(((unsigned)(i))<=NOSAFETY);(p)->n_insafety=(i);}
131 # define getntout(p) ((p)->n_outsafety)
132 # define setntout(p,i) {assert(((unsigned)(i))<=NOSAFETY);(p)->n_outsafety=(i);}
133 int n_count; /* pieces of code before this rule */
134 int n_lineno; /* declared on line ... */
135 p_gram n_rule; /* pointer to right hand side of rule */
137 p_set n_f; /* ptr to "first" set */
138 string n_s; /* If this nonterminal is not declared,
139 * This field indicates the filename in
143 # define n_first n_x.n_f
144 # define n_string n_x.n_s
145 #ifdef NON_CORRECTING
146 p_set n_nc_first; /* Pointer to non-corr first set */
147 p_set n_nc_follow; /* Pointer to non-corr follow set */
149 p_set n_follow; /* pointer to the "follow" set */
150 p_set n_contains; /* pointer to symbols that can be produced */
151 string n_name; /* name of nonterminal */
152 int n_next; /* index of next nonterminal */
153 long n_off; /* index of parameters in action file */
157 * hash table structure
159 typedef struct h_entry {
160 string h_name; /* pointer to name */
161 t_gram h_type; /* Type and index */
162 struct h_entry *h_next; /* next in hash chain */
166 * link structure to link alternations
168 typedef struct link {
170 # define COND 01 /* Set if this alternative has a %if */
171 # define DEF 02 /* This alternative is default */
172 # define PREFERING 010 /* %prefer */
173 # define AVOIDING 020 /* %avoid */
174 # define NOCONF 01000 /* Set if there is a resolver without
177 p_gram l_rule; /* pointer to this rule */
178 p_set l_symbs; /* set, when to take this rule */
179 #ifdef NON_CORRECTING
182 p_set l_others; /* set, when to take another rule */
186 * Structure for a repitition specification
188 typedef int t_reps,*p_reps;
190 # define FIXED 00 /* a fixed number */
191 # define STAR 01 /* 0 or more times */
192 # define PLUS 02 /* 1 or more times */
193 # define OPT 03 /* 0 or 1 times */
196 * Access macros for repitition in term
198 # define r_getkind(q) ((q)->t_repeats&03)
199 # define r_getnum(q) (((q)->t_repeats>>2)&037777)
200 # define r_setkind(q,s) { assert(((unsigned)(s))<=03);(q)->t_repeats=((q)->t_repeats&0177774)|(s);}
201 # define r_setnum(q,s) { assert(((unsigned)(s))<=037777);(q)->t_repeats=((q)->t_repeats&03)|((s)<<2);}
204 * header structure for a term
206 typedef struct term {
208 int t_flags; /* Low order three bits for safety */
209 # define gettout(q) ((q)->t_flags&07)
210 # define settout(q,i) {assert(((unsigned)(i))<=NOSAFETY);(q)->t_flags&=~07;(q)->t_flags|=i;}
211 # define PERSISTENT 010 /* Set if this term has %persistent */
212 # define RESOLVER 020 /* Set if this term has %while */
213 # define EMPTYFIRST 0100 /* Error, empty first */
214 # define EMPTYTERM 0200 /* Error, term can produce empty */
215 /* # define NOCONF 01000 see link structure */
217 p_gram t_rule; /* pointer to this term */
218 p_set t_follow; /* set of followers */
219 p_set t_first; /* set of firsts */
220 #ifdef NON_CORRECTING
221 p_set t_nc_first; /* set of non corr firsts */
222 p_set t_nc_follow; /* set of non corr followers */
224 p_set t_contains; /* contains set */
228 * structure for firstmacros list
230 typedef struct ff_firsts {
231 string ff_name; /* Name of the macro */
232 int ff_nont; /* for this nonterminal */
233 struct ff_firsts *ff_next;
237 * structure for start symbol list
239 typedef t_first t_start;
240 typedef p_first p_start;
243 * structure for file names and info
245 typedef struct f_file {
246 string f_name; /* File name */
247 p_first f_firsts; /* ptr to list of firstmacros that must be
248 * generated in the target file for this
251 int f_nonterminals; /* list of nonterminals in this file */
252 int f_terminals; /* list of terminals in this file */
253 p_set f_used; /* set of nonterminals used in this file */
256 typedef struct info_alloc {
258 * Structure used for dynamically growing arrays
260 unsigned i_size; /* Size of the array */
261 unsigned i_esize; /* Size of an element */
262 unsigned i_incr; /* When filled, add room for i_incr elements */
263 p_mem i_ptr; /* ptr to base of array */
264 p_mem i_max; /* ptr to first free */
265 p_mem i_top; /* ptr to top of array */
269 # define STATIC static
270 # else /* not NDEBUG */
271 # define STATIC extern
272 # endif /* not NDEBUG */