home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pccts.zip / pccts / antlr / syn.h < prev    next >
C/C++ Source or Header  |  1994-03-31  |  9KB  |  279 lines

  1. /*
  2.  * syn.h
  3.  *
  4.  * $Id: syn.h,v 1.6 1994/03/25 19:40:05 parrt Exp parrt $
  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.20
  30.  * Terence Parr
  31.  * Purdue University
  32.  * With AHPCRC, University of Minnesota
  33.  * 1989-1994
  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. /* note that 'right' is used by the tree node allocator as a ptr for linked list */
  65. typedef struct _tree {
  66.             struct _tree *down, *right;
  67.             int token;
  68.             union {
  69.                 int rk;    /* if token==EpToken, => how many more tokens req'd */
  70.                 struct _tree *tref;    /* if token==TREE_REF */
  71.                 set sref;            /* if token==SET */
  72.             } v;
  73. #ifdef TREE_DEBUG
  74.             int in_use;
  75. #endif
  76.         } Tree;
  77.  
  78. /* a predicate is defined to be a predicate action and a token tree with
  79.  * context info (if used); later, this struct may include the
  80.  * "hoisting distance" when we hoist past tokens.
  81.  *
  82.  * A tree is used to indicate && vs ||
  83.  *
  84.  *    p
  85.  *    |
  86.  *    q--r
  87.  *
  88.  * indicates p && (q||r).
  89.  */
  90. typedef struct _Predicate {
  91.     struct _Predicate *down, *right;    /* these have to be first */
  92.     char *expr;
  93.     Tree *tcontext;    /* used if lookahead depth of > one is needed (tree) */
  94.     set scontext[2];/* used if lookahead depth of one is needed (set) */
  95.                     /* scontext[0] is not used; only needed so genExprSets()
  96.                        routine works (it expects an array)
  97.                      */
  98.     set completion;    /* which lookahead depths are required to complete tcontext? */
  99. } Predicate;
  100.  
  101. #define TokenString(_i)            ((TokenInd!=NULL)?TokenStr[TokenInd[_i]]:TokenStr[_i])
  102. #define ExprString(_i)            ((TokenInd!=NULL)?ExprStr[TokenInd[_i]]:ExprStr[_i])
  103.  
  104.  
  105.                 /* M e s s a g e  P a s s i n g  T o  N o d e s */
  106.  
  107. /*
  108.  * assumes a 'Junction *r' exists.  This macro calls a function with
  109.  * the pointer to the node to operate on and a pointer to the rule
  110.  * in which it is enclosed.
  111.  */
  112. #define TRANS(p)    {if ( (p)==NULL ) fatal("TRANS: NULL object");        \
  113.                     if ( (p)->ntype == nJunction ) (*(fpJTrans[((Junction *)(p))->jtype]))( p );\
  114.                     else (*(fpTrans[(p)->ntype]))( p );}
  115.  
  116. #define PRINT(p)    {if ( (p)==NULL ) fatal("PRINT: NULL object");\
  117.                     (*(fpPrint[(p)->ntype]))( p );}
  118.  
  119. #define REACH(p,k,rk,a) {if ( (p)==NULL ) fatal("REACH: NULL object");\
  120.                     (a) = (*(fpReach[(p)->ntype]))( p, k, rk );}
  121.  
  122. #define TRAV(p,k,rk,a) {if ( (p)==NULL ) fatal("TRAV: NULL object");\
  123.                     (a) = (*(fpTraverse[(p)->ntype]))( p, k, rk );}
  124.  
  125. /* All syntax diagram nodes derive from Node -- superclass
  126.  */
  127. #ifdef __cplusplus
  128. class Node {
  129. public:
  130.             NodeType ntype;
  131.         };
  132. #else
  133. typedef struct _node {
  134.             NodeType ntype;
  135.         } Node;
  136. #endif
  137.  
  138. #ifdef __cplusplus
  139. class ActionNode : public Node {
  140. public:
  141. #else
  142. typedef struct _anode {
  143.             NodeType ntype;
  144. #endif
  145.             Node *next;
  146.             char *action;
  147.             int file;            /* index in FileStr (name of file with action) */
  148.             int line;            /* line number that action occurs on */
  149.             int is_predicate;    /* true if action is a <<...>>? predicate action */
  150.             int done;            /* don't dump if action dumped (used for predicates) */
  151.             int init_action;    /* is this the 1st action of 1st prod of block? */
  152.             char *pred_fail;    /* what to do/print when predicate fails */
  153. #ifdef __cplusplus
  154.         };
  155. #else
  156.         } ActionNode;
  157. #endif
  158.  
  159. #ifdef __cplusplus
  160. class TokNode : public Node {
  161. public:
  162. #else
  163. typedef struct _toknode {
  164.             NodeType ntype;
  165. #endif
  166.             Node *next;
  167.             char *rname;        /* name of rule it's in */
  168.             int file;            /* index in FileStr (name of file with rule) */
  169.             int line;            /* line number that token occurs on */
  170.             int token;
  171.             int astnode;        /* leaf/root/excluded (used to build AST's) */
  172.             unsigned char label;/* token label or expression ? */
  173.             unsigned char remapped;
  174.                                 /* used if token id's are forced to certain positions;
  175.                                  * a function walks the tree reassigning token numbers */
  176.             unsigned char upper_range;
  177.                                 /* used only if Token is of type T1..T2; in this case,
  178.                                  * use token..upper_range as the range; else
  179.                                  * upper_range must be 0 */
  180.             unsigned char wild_card;
  181.                                 /* indicates that the token is the "." wild-card;
  182.                                  * field token is ignored if wild_card is set
  183.                                  */
  184.             unsigned int elnum; /* element number within the alternative */
  185.             struct _TCnode *tclass;        /* token class if tokclass ref */
  186.             set tset;            /* set of tokens represented by meta token */
  187.             unsigned char complement;    /* complement the set? */
  188. #ifdef __cplusplus
  189.         };
  190. #else
  191.         } TokNode;
  192. #endif
  193.  
  194. #ifdef __cplusplus
  195. class RuleRefNode : public Node {
  196. public:
  197. #else
  198. typedef struct _rrnode {
  199.             NodeType ntype;
  200. #endif
  201.             Node *next;
  202.             char *rname;        /* name of rule it's in */
  203.             int file;            /* index in FileStr (name of file with rule)
  204.                                    it's in */
  205.             int line;            /* line number that rule ref occurs on */
  206.             char *text;            /* reference to which rule */
  207.             char *parms;        /* point to parameters of rule invocation
  208.                                    (if present) */
  209.             char *assign;        /* point to left-hand-side of assignment
  210.                                    (if any) */
  211.             int linked;            /* Has a FoLink already been established? */
  212.             int astnode;        /* excluded? (used to build AST's) */
  213.             unsigned int elnum; /* element number within the alternative */
  214. #ifdef __cplusplus
  215.         };
  216. #else
  217.         } RuleRefNode;
  218. #endif
  219.  
  220. #ifdef __cplusplus
  221. class Junction : public Node {
  222. public:
  223. #else
  224. typedef struct _junct {
  225.             NodeType ntype;
  226. #endif
  227.             char ignore;        /* used by FIRST computation to ignore
  228.                                    empty alt added for the (...)+ blks */
  229.             char visited;        /* used by recursive routines to avoid
  230.                                    infinite recursion */
  231.             char pvisited;        /* used by print routines to avoid
  232.                                    infinite recursion */
  233.             char fvisited;        /* used by FoLink() to avoid
  234.                                    infinite recursion */
  235.             char *lock;            /* used by REACH to track infinite recursion */
  236.             char *pred_lock;    /* used by find_predicates to track infinite recursion */
  237.             int altnum;            /* used in subblocks. altnum==0 means not an
  238.                                    alt of subrule */
  239.             int jtype;            /* annotation for code-gen/FIRST/FOLLOW.
  240.                                    Junction type */
  241. #ifdef __cplusplus
  242.             Junction *end;        /* pointer to node with EndBlk in it
  243.                                    if blk == a block type */
  244. #else
  245.             struct _junct *end;    /* pointer to node with EndBlk in it
  246.                                    if blk == a block type */
  247. #endif
  248.             Node *p1, *p2;
  249.             char *rname;        /* name of rule junction is in */
  250.             int file;            /* index in FileStr (name of file with rule)
  251.                                    if blk == RuleBlk */
  252.             int line;            /* line number that rule occurs on */
  253.             int halt;            /* never move past a junction with halt==TRUE */
  254.             char *pdecl;        /* point to declaration of parameters on rule
  255.                                    (if present) */
  256.             char *parm;            /* point to parameter of block invocation
  257.                                    (if present) */
  258.             int predparm;        /* indicates that the 'parm' is a predicate
  259.                                  * to be used in the while loop generated
  260.                                  * for blocks */
  261.             char *ret;            /* point to return type of rule (if present) */
  262.             char *erraction;    /* point to error action (if present) */
  263.             int blockid;        /* if start of block, then this is a unique ID */
  264.             set *fset;            /* used for code generation */
  265.             Tree *ftree;        /* used for code generation */
  266.             Predicate *predicate;/* predicate that can be used to disambiguate */
  267.             char guess;            /* true if (...)? block */
  268.             char approx;        /* limit block to use linear approx lookahead? */
  269.             set tokrefs;        /* if ith element of alt is tokref then i is member */
  270.             set rulerefs;        /* if ith element of alt is rule ref then i is member */
  271. #ifdef __cplusplus
  272.         };
  273. #else
  274.         } Junction;
  275. #endif
  276.  
  277. typedef struct { Node *left, *right; } Graph;
  278.  
  279.