]> pd.if.org Git - pccts/blob - antlr/syn.h
auto commit for import
[pccts] / antlr / syn.h
1 /*
2  * syn.h
3  *
4  * $Id: syn.h,v 1.6 95/09/26 12:58:32 parrt Exp $
5  * $Revision: 1.6 $
6  *
7  * This file includes definitions and macros associated with syntax diagrams
8  *
9  * SOFTWARE RIGHTS
10  *
11  * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
12  * Set (PCCTS) -- PCCTS is in the public domain.  An individual or
13  * company may do whatever they wish with source code distributed with
14  * PCCTS or the code generated by PCCTS, including the incorporation of
15  * PCCTS, or its output, into commerical software.
16  * 
17  * We encourage users to develop software with PCCTS.  However, we do ask
18  * that credit is given to us for developing PCCTS.  By "credit",
19  * we mean that if you incorporate our source code into one of your
20  * programs (commercial product, research project, or otherwise) that you
21  * acknowledge this fact somewhere in the documentation, research report,
22  * etc...  If you like PCCTS and have developed a nice tool with the
23  * output, please mention that you developed it using PCCTS.  In
24  * addition, we ask that this header remain intact in our source code.
25  * As long as these guidelines are kept, we expect to continue enhancing
26  * this system and expect to make other tools available as they are
27  * completed.
28  *
29  * ANTLR 1.33
30  * Terence Parr
31  * Parr Research Corporation
32  * with Purdue University and AHPCRC, University of Minnesota
33  * 1989-1995
34  */
35
36 #define NumNodeTypes    4
37 #define NumJuncTypes    9
38
39 /* List the different node types */
40 #define nJunction               1
41 #define nRuleRef                2
42 #define nToken                  3
43 #define nAction                 4
44
45 /* Different types of junctions */
46 #define aSubBlk                 1
47 #define aOptBlk                 2
48 #define aLoopBlk                3
49 #define EndBlk                  4
50 #define RuleBlk                 5
51 #define Generic                 6       /* just a junction--no unusual characteristics */
52 #define EndRule                 7
53 #define aPlusBlk                8
54 #define aLoopBegin              9
55
56 typedef int NodeType;
57
58 #define TreeBlockAllocSize              500
59 #define JunctionBlockAllocSize  200
60 #define ActionBlockAllocSize    50
61 #define RRefBlockAllocSize              100
62 #define TokenBlockAllocSize             100
63
64 #ifdef __cplusplus
65 class ActionNode;
66 class Junction;
67 #endif
68
69 /* note that 'right' is used by the tree node allocator as a ptr for linked list */
70 typedef struct _tree {
71                         struct _tree *down, *right;
72                         int token;
73                         union {
74                                 int rk; /* if token==EpToken, => how many more tokens req'd */
75                                 struct _tree *tref;     /* if token==TREE_REF */
76                                 set sref;                       /* if token==SET */
77                         } v;
78 #ifdef TREE_DEBUG
79                         int in_use;
80 #endif
81                 } Tree;
82
83 /* a predicate is defined to be a predicate action and a token tree with
84  * context info (if used); later, this struct may include the
85  * "hoisting distance" when we hoist past tokens.
86  *
87  * A tree is used to indicate && vs ||
88  *
89  *    p
90  *    |
91  *    q--r
92  *
93  * indicates p && (q||r).
94  *
95  * If expr is PRED_AND_LIST or PRED_OR_LIST, then it's an operation node
96  * and indicates the start of an && or || list.
97  */
98
99 typedef struct _Predicate {
100         struct _Predicate *down, *right;        /* these have to be first */
101         struct _Predicate *up, *left;           /* doubly-link me */
102         char *expr;
103         Tree *tcontext; /* used if lookahead depth of > one is needed (tree) */
104         int k;                  /* lookahead depth for this tcontext */
105         set scontext[2];/* used if lookahead depth of one is needed (set) */
106                                         /* scontext[0] is not used; only needed so genExprSets()
107                                            routine works (it expects an array)
108                                          */
109         set completion; /* which lookahead depths are required to complete tcontext? */
110 #ifdef __cplusplus
111         ActionNode *source;     /* where did this predicate come from? */
112 #else
113         struct _anode *source;  /* where did this predicate come from? */
114 #endif
115 } Predicate;
116
117 typedef struct _ExceptionHandler {
118                         char *signalname;
119                         char *action;
120                 } ExceptionHandler;
121
122 typedef struct _ExceptionGroup {
123                         struct _ListNode *handlers; /* list of ExceptionHandler's */
124                         char *label;            /* label==""; implies not attached to any
125                                                                  * particular rule ref.
126                                                                  */
127                         char *altID;            /* which alt did it come from (blk#:alt#) */
128                 } ExceptionGroup ;
129
130
131 #define TokenString(_i)                 ((TokenInd!=NULL)?TokenStr[TokenInd[_i]]:TokenStr[_i])
132 #define ExprString(_i)                  ((TokenInd!=NULL)?ExprStr[TokenInd[_i]]:ExprStr[_i])
133
134
135                                 /* M e s s a g e  P a s s i n g  T o  N o d e s */
136
137 /*
138  * assumes a 'Junction *r' exists.  This macro calls a function with
139  * the pointer to the node to operate on and a pointer to the rule
140  * in which it is enclosed.
141  */
142 #define TRANS(p)        {if ( (p)==NULL ) fatal("TRANS: NULL object");          \
143                                         if ( (p)->ntype == nJunction ) (*(fpJTrans[((Junction *)(p))->jtype]))( p );\
144                                         else (*(fpTrans[(p)->ntype]))( p );}
145
146 #define PRINT(p)        {if ( (p)==NULL ) fatal("PRINT: NULL object");\
147                                         (*(fpPrint[(p)->ntype]))( p );}
148
149 #define REACH(p,k,rk,a) {if ( (p)==NULL ) fatal("REACH: NULL object");\
150                                         (a) = (*(fpReach[(p)->ntype]))( p, k, rk );}
151
152 #define TRAV(p,k,rk,a) {if ( (p)==NULL ) {\
153                                           if ( ContextGuardTRAV ) (a)=NULL; \
154                                           else fatal("TRAV: NULL object");\
155                                     } \
156                                         else (a) = (*(fpTraverse[(p)->ntype]))( p, k, rk );}
157
158 /*
159 #define TRAV(p,k,rk,a) {if ( (p)==NULL ) fatal("TRAV: NULL object");\
160                                         (a) = (*(fpTraverse[(p)->ntype]))( p, k, rk );}
161 */
162
163 /* All syntax diagram nodes derive from Node -- superclass
164  */
165 #ifdef __cplusplus
166 class Node {
167 public:
168                         NodeType ntype;
169                         char *rname;            /* what rule does this element live in? */
170                         int file;                       /* index in FileStr */
171                         int line;                       /* line number that element occurs on */
172                 };
173 #else
174 typedef struct _node {
175                         NodeType ntype;
176                         char *rname;            /* what rule does this element live in? */
177                         int file;                       /* index in FileStr */
178                         int line;                       /* line number that element occurs on */
179                 } Node;
180 #endif
181
182 #ifdef __cplusplus
183 class ActionNode : public Node {
184 public:
185 #else
186 typedef struct _anode {
187                         NodeType ntype;
188                         char *rname;            /* what rule does this action live in? */
189                         int file;                       /* index in FileStr (name of file with action) */
190                         int line;                       /* line number that action occurs on */
191 #endif
192                         Node *next;
193                         char *action;
194                         int is_predicate;       /* true if action is a <<...>>? predicate action */
195                         int done;                       /* don't dump if action dumped (used for predicates) */
196                         int init_action;        /* is this the 1st action of 1st prod of block? */
197                         char *pred_fail;        /* what to do/print when predicate fails */
198                         Predicate *guardpred;   /* if '(context)? =>' was present, already done */
199                         unsigned char frmwarned;/* have we dumped a warning for pred yet? */
200                         unsigned char ctxwarned;/* have we dumped a warning for pred yet? */
201 #ifdef __cplusplus
202                 };
203 #else
204                 } ActionNode;
205 #endif
206
207 #ifdef __cplusplus
208 class TokNode : public Node {
209 public:
210 #else
211 typedef struct _toknode {
212                         NodeType ntype;
213                         char *rname;            /* name of rule it's in */
214                         int file;                       /* index in FileStr (name of file with rule) */
215                         int line;                       /* line number that token occurs on */
216 #endif
217                         Node *next;
218                         int token;
219                         int astnode;            /* leaf/root/excluded (used to build AST's) */
220                         unsigned char label;/* token label or expression ? */
221                         unsigned char remapped;
222                                                                 /* used if token id's are forced to certain positions;
223                                                                  * a function walks the tree reassigning token numbers */
224                         unsigned char upper_range;
225                                                                 /* used only if Token is of type T1..T2; in this case,
226                                                                  * use token..upper_range as the range; else
227                                                                  * upper_range must be 0 */
228                         unsigned char wild_card;
229                                                                 /* indicates that the token is the "." wild-card;
230                                                                  * field token is ignored if wild_card is set
231                                                                  */
232                         unsigned int elnum; /* element number within the alternative */
233 #ifdef __cplusplus
234                         Junction *altstart;     /* pointer to node that starts alt */
235 #else
236                         struct _junct *altstart;        /* pointer to node that starts alt */
237 #endif
238                         struct _TCnode *tclass;         /* token class if tokclass ref */
239                         set tset;                       /* set of tokens represented by meta token */
240                         char *el_label;         /* el_label:toknode */
241                         unsigned char complement;       /* complement the set? */
242                         ExceptionGroup *ex_group;       /* any exception[el_label] attached? */
243             unsigned char use_def_MT_handler;
244 #ifdef __cplusplus
245                 };
246 #else
247                 } TokNode;
248 #endif
249
250 #ifdef __cplusplus
251 class RuleRefNode : public Node {
252 public:
253 #else
254 typedef struct _rrnode {
255                         NodeType ntype;
256                         char *rname;            /* name of rule it's in */
257                         int file;                       /* index in FileStr (name of file with rule)
258                                                                    it's in */
259                         int line;                       /* line number that rule ref occurs on */
260 #endif
261                         Node *next;
262                         char *text;                     /* reference to which rule */
263                         char *parms;            /* point to parameters of rule invocation
264                                                                    (if present) */
265                         char *assign;           /* point to left-hand-side of assignment
266                                                                    (if any) */
267                         int linked;                     /* Has a FoLink already been established? */
268                         int astnode;            /* excluded? (used to build AST's) */
269                         unsigned int elnum; /* element number within the alternative */
270 #ifdef __cplusplus
271                         Junction *altstart;
272 #else
273                         struct _junct *altstart;
274 #endif
275                         char *el_label;         /* el_label:rrnode */
276                         ExceptionGroup *ex_group;       /* any exception[el_label] attached? */
277 #ifdef __cplusplus
278                 };
279 #else
280                 } RuleRefNode;
281 #endif
282
283 #ifdef __cplusplus
284 class Junction : public Node {
285 public:
286 #else
287 typedef struct _junct {
288                         NodeType ntype;
289                         char *rname;            /* name of rule junction is in */
290                         int file;                       /* index in FileStr (name of file with rule)
291                                                                    if blk == RuleBlk */
292                         int line;                       /* line number that rule occurs on */
293 #endif
294                         char ignore;            /* used by FIRST computation to ignore
295                                                                    empty alt added for the (...)+ blks */
296                         char visited;           /* used by recursive routines to avoid
297                                                                    infinite recursion */
298                         char pvisited;          /* used by print routines to avoid
299                                                                    infinite recursion */
300                         char fvisited;          /* used by FoLink() to avoid
301                                                                    infinite recursion */
302                         char *lock;                     /* used by REACH to track infinite recursion */
303                         char *pred_lock;        /* used by find_predicates to track infinite recursion */
304                         int altnum;                     /* used in subblocks. altnum==0 means not an
305                                                                    alt of subrule */
306                         int jtype;                      /* annotation for code-gen/FIRST/FOLLOW.
307                                                                    Junction type */
308 #ifdef __cplusplus
309                         Junction *end;          /* pointer to node with EndBlk in it
310                                                                    if blk == a block type */
311 #else
312                         struct _junct *end;     /* pointer to node with EndBlk in it
313                                                                    if blk == a block type */
314 #endif
315                         Node *p1, *p2;
316                         int halt;                       /* never move past a junction with halt==TRUE */
317                         char *pdecl;            /* point to declaration of parameters on rule
318                                                                    (if present) */
319                         char *parm;                     /* point to parameter of block invocation
320                                                                    (if present) */
321                         int predparm;           /* indicates that the 'parm' is a predicate
322                                                                  * to be used in the while loop generated
323                                                                  * for blocks */
324                         char *ret;                      /* point to return type of rule (if present) */
325                         char *erraction;        /* point to error action (if present) */
326                         int blockid;            /* this is a unique ID */
327                         char *exception_label;  /* goto label for this alt */
328                         set *fset;                      /* used for code generation */
329                         Tree *ftree;            /* used for code generation */
330                         Predicate *predicate;/* predicate that can be used to disambiguate */
331                         char guess;                     /* true if (...)? block */
332                         char approx;            /* limit block to use linear approx lookahead? */
333                         set tokrefs;            /* if ith element of alt is tokref then i is member */
334                         set rulerefs;           /* if ith element of alt is rule ref then i is member */
335                         struct _ListNode *exceptions; /* list of exceptions groups for rule */
336                         struct _ListNode *el_labels;  /* list of element labels for rule */
337 #ifdef __cplusplus
338                 };
339 #else
340                 } Junction;
341 #endif
342
343 typedef struct { Node *left, *right; } Graph;
344