home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pccts1.zip / H / ERR.H < prev    next >
C/C++ Source or Header  |  1993-09-01  |  13KB  |  527 lines

  1. /*
  2.  * err.h
  3.  *
  4.  * Standard error handling mechanism
  5.  *
  6.  * SOFTWARE RIGHTS
  7.  *
  8.  * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
  9.  * Set (PCCTS) -- PCCTS is in the public domain.  An individual or
  10.  * company may do whatever they wish with source code distributed with
  11.  * PCCTS or the code generated by PCCTS, including the incorporation of
  12.  * PCCTS, or its output, into commerical software.
  13.  * 
  14.  * We encourage users to develop software with PCCTS.  However, we do ask
  15.  * that credit is given to us for developing PCCTS.  By "credit",
  16.  * we mean that if you incorporate our source code into one of your
  17.  * programs (commercial product, research project, or otherwise) that you
  18.  * acknowledge this fact somewhere in the documentation, research report,
  19.  * etc...  If you like PCCTS and have developed a nice tool with the
  20.  * output, please mention that you developed it using PCCTS.  In
  21.  * addition, we ask that this header remain intact in our source code.
  22.  * As long as these guidelines are kept, we expect to continue enhancing
  23.  * this system and expect to make other tools available as they are
  24.  * completed.
  25.  *
  26.  * Has grown to hold all kinds of stuff (err.h is increasingly misnamed)
  27.  *
  28.  * Terence Parr: 1989-1993
  29.  * Purdue University Electrical Engineering
  30.  * ANTLR Version 1.10
  31.  */
  32.  
  33. #ifndef ZZERR_H
  34. #define ZZERR_H
  35.  
  36. #include <string.h>
  37. #ifdef __STDC__
  38. #include <stdarg.h>
  39. #else
  40. #include <varargs.h>
  41. #endif
  42.  
  43. /* Define usable bits per unsigned int word (used for set stuff) */
  44. #ifdef PC
  45. #define WORDSIZE 16
  46. #define LogWordSize    4
  47. #else
  48. #define    WORDSIZE 32
  49. #define LogWordSize 5
  50. #endif
  51.  
  52. #define    MODWORD(x) ((x) & (WORDSIZE-1))        /* x % WORDSIZE */
  53. #define    DIVWORD(x) ((x) >> LogWordSize)        /* x / WORDSIZE */
  54.  
  55. /* This is not put into the global pccts_parser structure because it is
  56.  * hidden and does not need to be saved during a "save state" operation
  57.  */
  58. /* maximum of 32 bits/unsigned int and must be 8 bits/byte */
  59. static SetWordType bitmask[] = {
  60.     0x00000001, 0x00000002, 0x00000004, 0x00000008,
  61.     0x00000010, 0x00000020, 0x00000040, 0x00000080
  62. };
  63.  
  64. void
  65. #ifdef __STDC__
  66. zzresynch(SetWordType *wd,SetWordType mask)
  67. #else
  68. zzresynch(wd,mask)
  69. SetWordType *wd, mask;
  70. #endif
  71. {
  72.     static int consumed = 1;
  73.  
  74.     /* if you enter here without having consumed a token from last resynch
  75.      * force a token consumption.
  76.      */
  77.     if ( !consumed ) {zzCONSUME; return;}
  78.  
  79.     /* if current token is in resynch set, we've got what we wanted */
  80.     if ( wd[LA(1)]&mask || LA(1) == zzEOF_TOKEN ) {consumed=0; return;}
  81.     
  82.     /* scan until we find something in the resynch set */
  83.     while ( !(wd[LA(1)]&mask) && LA(1) != zzEOF_TOKEN ) {zzCONSUME;}
  84.     consumed=1;
  85. }
  86.  
  87. /* input looks like:
  88.  *        zzFAIL(k, e1, e2, ...,&zzMissSet,&zzMissText,&zzBadTok,&zzBadText)
  89.  * where the zzMiss stuff is set here to the token that did not match
  90.  * (and which set wasn't it a member of).
  91.  */
  92. void
  93. #ifdef __STDC__
  94. zzFAIL(int k, ...)
  95. #else
  96. zzFAIL(va_alist)
  97. va_dcl
  98. #endif
  99. {
  100. #ifdef LL_K
  101.     static char text[LL_K*ZZLEXBUFSIZE+1];
  102.     SetWordType *f[LL_K];
  103. #else
  104.     static char text[ZZLEXBUFSIZE+1];
  105.     SetWordType *f[1];
  106. #endif
  107.     SetWordType **miss_set;
  108.     char **miss_text;
  109.     unsigned *bad_tok;
  110.     char **bad_text;
  111.     unsigned *err_k;
  112.     int i;
  113.     va_list ap;
  114. #ifndef __STDC__
  115.     int k;
  116. #endif
  117. #ifdef __STDC__
  118.     va_start(ap, k);
  119. #else
  120.     va_start(ap);
  121.     k = va_arg(ap, int);    /* how many lookahead sets? */
  122. #endif
  123.     text[0] = '\0';
  124.     for (i=1; i<=k; i++)    /* collect all lookahead sets */
  125.     {
  126.         f[i-1] = va_arg(ap, SetWordType *);
  127.     }
  128.     for (i=1; i<=k; i++)    /* look for offending token */
  129.     {
  130.         if ( i>1 ) strcat(text, " ");
  131.         strcat(text, LATEXT(i));
  132.         if ( !zzset_el(LA(i), f[i-1]) ) break;
  133.     }
  134.     miss_set = va_arg(ap, SetWordType **);
  135.     miss_text = va_arg(ap, char **);
  136.     bad_tok = va_arg(ap, unsigned *);
  137.     bad_text = va_arg(ap, char **);
  138.     err_k = va_arg(ap, unsigned *);
  139.     if ( i>k )
  140.     {
  141.         /* bad; lookahead is permutation that cannot be matched,
  142.          * but, the ith token of lookahead is valid at the ith position
  143.          * (The old LL sub 1 (k) versus LL(k) parsing technique)
  144.          */
  145.         *miss_set = NULL;
  146.         *miss_text = zzlextext;
  147.         *bad_tok = LA(1);
  148.         *bad_text = LATEXT(1);
  149.         *err_k = k;
  150.         return;
  151.     }
  152. /*    fprintf(stderr, "%s not in %dth set\n", zztokens[LA(i)], i);*/
  153.     *miss_set = f[i-1];
  154.     *miss_text = text;
  155.     *bad_tok = LA(i);
  156.     *bad_text = LATEXT(i);
  157.     if ( i==1 ) *err_k = 1;
  158.     else *err_k = k;
  159. }
  160.  
  161. void
  162. #ifdef __STDC__
  163. zzsave_antlr_state(zzantlr_state *buf)
  164. #else
  165. zzsave_antlr_state(buf)
  166. zzantlr_state *buf;
  167. #endif
  168. {
  169. #ifdef LL_K
  170.     int i;
  171. #endif
  172.  
  173. #ifdef ZZCAN_GUESS
  174.     buf->guess_start = zzguess_start;
  175.     buf->guessing = zzguessing;
  176. #endif
  177.     buf->asp = zzasp;
  178. #ifdef ZZINF_LOOK
  179.     buf->inf_labase = zzinf_labase;
  180.     buf->inf_last = zzinf_last;
  181. #endif
  182. #ifdef DEMAND_LOOK
  183.     buf->dirty = zzdirty;
  184. #endif
  185. #ifdef LL_K
  186.     for (i=0; i<LL_K; i++) buf->tokenLA[i] = zztokenLA[i];
  187.     for (i=0; i<LL_K; i++) strcpy(buf->textLA[i], zztextLA[i]);
  188.     buf->lap = zzlap;
  189.     buf->labase = zzlabase;
  190. #else
  191.     buf->token = zztoken;
  192. #endif
  193. }
  194.  
  195. void
  196. #ifdef __STDC__
  197. zzrestore_antlr_state(zzantlr_state *buf)
  198. #else
  199. zzrestore_antlr_state(buf)
  200. zzantlr_state *buf;
  201. #endif
  202. {
  203. #ifdef LL_K
  204.     int i;
  205. #endif
  206.  
  207. #ifdef ZZCAN_GUESS
  208.     zzguess_start = buf->guess_start;
  209.     zzguessing = buf->guessing;
  210. #endif
  211.     zzasp = buf->asp;
  212. #ifdef ZZINF_LOOK
  213.     zzinf_labase = buf->inf_labase;
  214.     zzinf_last = buf->inf_last;
  215. #endif
  216. #ifdef DEMAND_LOOK
  217.     zzdirty = buf->dirty;
  218. #endif
  219. #ifdef LL_K
  220.     for (i=0; i<LL_K; i++) zztokenLA[i] = buf->tokenLA[i];
  221.     for (i=0; i<LL_K; i++) strcpy(zztextLA[i], buf->textLA[i]);
  222.     zzlap = buf->lap;
  223.     zzlabase = buf->labase;
  224. #else
  225.     zztoken = buf->token;
  226. #endif
  227. }
  228.  
  229. #ifndef USER_ZZSYN
  230. /* standard error reporting function */
  231. void
  232. #ifdef __STDC__
  233. zzsyn(char *text, unsigned tok, char *egroup, SetWordType *eset, unsigned etok, int k, char *bad_text)
  234. #else
  235. zzsyn(text, tok, egroup, eset, etok, k, bad_text)
  236. char *text, *egroup, *bad_text;
  237. unsigned tok;
  238. unsigned etok;
  239. int k;
  240. SetWordType *eset;
  241. #endif
  242. {
  243.     
  244.     fprintf(stderr, "line %d: syntax error at \"%s\"", zzline, (tok==zzEOF_TOKEN)?"EOF":text);
  245.     if ( !etok && !eset ) {fprintf(stderr, "\n"); return;}
  246.     if ( k==1 ) fprintf(stderr, " missing");
  247.     else
  248.     {
  249.         fprintf(stderr, "; \"%s\" not", bad_text);
  250.         if ( zzset_deg(eset)>1 ) fprintf(stderr, " in");
  251.     }
  252.     if ( zzset_deg(eset)>0 ) zzedecode(eset);
  253.     else fprintf(stderr, " %s", zztokens[etok]);
  254.     if ( strlen(egroup) > 0 ) fprintf(stderr, " in %s", egroup);
  255.     fprintf(stderr, "\n");
  256. }
  257. #endif
  258.  
  259. /* is b an element of set p? */
  260. int
  261. #ifdef __STDC__
  262. zzset_el(unsigned b, SetWordType *p)
  263. #else
  264. zzset_el(b,p)
  265. unsigned b;
  266. SetWordType *p;
  267. #endif
  268. {
  269.     return( p[DIVWORD(b)] & bitmask[MODWORD(b)] );
  270. }
  271.  
  272. int
  273. #ifdef __STDC__
  274. zzset_deg(SetWordType *a)
  275. #else
  276. zzset_deg(a)
  277. SetWordType *a;
  278. #endif
  279. {
  280.     /* Fast compute degree of a set... the number
  281.        of elements present in the set.  Assumes
  282.        that all word bits are used in the set
  283.     */
  284.     register SetWordType *p = a;
  285.     register SetWordType *endp = &(a[zzSET_SIZE]);
  286.     register int degree = 0;
  287.  
  288.     if ( a == NULL ) return 0;
  289.     while ( p < endp )
  290.     {
  291.         register SetWordType t = *p;
  292.         register SetWordType *b = &(bitmask[0]);
  293.         do {
  294.             if (t & *b) ++degree;
  295.         } while (++b < &(bitmask[WORDSIZE]));
  296.         p++;
  297.     }
  298.  
  299.     return(degree);
  300. }
  301.  
  302. void
  303. #ifdef __STDC__
  304. zzedecode(SetWordType *a)
  305. #else
  306. zzedecode(a)
  307. SetWordType *a;
  308. #endif
  309. {
  310.     register SetWordType *p = a;
  311.     register SetWordType *endp = &(p[zzSET_SIZE]);
  312.     register SetWordType e = 0;
  313.  
  314.     if ( zzset_deg(a)>1 ) fprintf(stderr, " {");
  315.     do {
  316.         register SetWordType t = *p;
  317.         register SetWordType *b = &(bitmask[0]);
  318.         do {
  319.             if ( t & *b ) fprintf(stderr, " %s", zztokens[e]);
  320.             e++;
  321.         } while (++b < &(bitmask[sizeof(SetWordType)*8]));
  322.     } while (++p < endp);
  323.     if ( zzset_deg(a)>1 ) fprintf(stderr, " }");
  324. }
  325.  
  326. #ifndef ZZUSE_MACROS
  327.  
  328. #ifdef DEMAND_LOOK
  329.  
  330. #ifdef LL_K
  331. int
  332. #ifdef __STDC__
  333. _zzmatch(int _t, char **zzBadText, char **zzMissText,
  334.         unsigned *zzMissTok, unsigned *zzBadTok,
  335.         SetWordType **zzMissSet)
  336. #else
  337. _zzmatch(_t, zzBadText, zzMissText, zzMissTok, zzBadTok, zzMissSet)
  338. int _t;
  339. char **zzBadText;
  340. char **zzMissText;
  341. unsigned *zzMissTok, *zzBadTok;
  342. SetWordType **zzMissSet;
  343. #endif
  344. {
  345.     if ( zzdirty==LL_K ) {
  346.         zzCONSUME;
  347.     }
  348.     if ( LA(1)!=_t ) {        
  349.         *zzBadText = *zzMissText=LATEXT(1);    
  350.         *zzMissTok= _t; *zzBadTok=LA(1); 
  351.         *zzMissSet=NULL;                
  352.         return 0;
  353.     }
  354.     zzMakeAttr                        
  355.     zzdirty++;                        
  356.     zzlabase++;                        
  357.     return 1;
  358. }
  359.  
  360. #else
  361.  
  362. int
  363. #ifdef __STDC__
  364. _zzmatch(int _t, char **zzBadText, char **zzMissText,
  365.          unsigned *zzMissTok, unsigned *zzBadTok, SetWordType **zzMissSet)
  366. #else
  367. _zzmatch(_t, zzBadText, zzMissText, zzMissTok, zzBadTok, zzMissSet)
  368. int _t;
  369. char **zzBadText;
  370. char **zzMissText;
  371. unsigned *zzMissTok, *zzBadTok;
  372. SetWordType **zzMissSet;
  373. #endif
  374. {                                
  375.     if ( zzdirty ) {zzCONSUME;}        
  376.     if ( LA(1)!=_t ) {
  377.         *zzBadText = *zzMissText=LATEXT(1);    
  378.         *zzMissTok= _t; *zzBadTok=LA(1); 
  379.         *zzMissSet=NULL;                
  380.         return 0;
  381.     }                                
  382.     zzdirty = 1;                    
  383.     zzMakeAttr                        
  384.     return 1;
  385. }
  386. #endif /*LL_K*/
  387.  
  388. #else
  389.  
  390. int
  391. #ifdef __STDC__
  392. _zzmatch(int _t, char **zzBadText, char **zzMissText,
  393.         unsigned *zzMissTok, unsigned *zzBadTok,
  394.         SetWordType **zzMissSet)
  395. #else
  396. _zzmatch(_t, zzBadText, zzMissText, zzMissTok, zzBadTok, zzMissSet)
  397. int _t;
  398. char **zzBadText;
  399. char **zzMissText;
  400. unsigned *zzMissTok, *zzBadTok;
  401. SetWordType **zzMissSet;
  402. #endif
  403. {
  404.     if ( LA(1)!=_t ) {                
  405.         *zzBadText = *zzMissText=LATEXT(1);    
  406.         *zzMissTok= _t; *zzBadTok=LA(1); 
  407.         *zzMissSet=NULL;                
  408.         return 0;
  409.     }
  410.     zzMakeAttr
  411.     return 1;
  412. }
  413. #endif /*DEMAND_LOOK*/
  414.  
  415. #ifdef ZZINF_LOOK
  416. void
  417. #ifdef __STDC__
  418. _inf_zzgettok(void)
  419. #else
  420. _inf_zzgettok()
  421. #endif
  422. {
  423.     if ( zzinf_labase >= zzinf_last )                    
  424.         {NLA = DEFAULT_EOF_TOKEN; strcpy(NLATEXT, "");}    
  425.     else {                                            
  426.         NLA = zzinf_tokens[zzinf_labase];                
  427.         strcpy(NLATEXT, zzinf_text[zzinf_labase]);        
  428.         zzinf_labase++;                                 
  429.     }                                                
  430. }
  431. #endif
  432.  
  433. #endif /* !def ZZUSE_MACROS */
  434.  
  435. #ifdef ZZINF_LOOK
  436. /* allocate default size text,token arrays;
  437.  * then, read all of the input reallocing the arrays as needed.
  438.  * Once the number of total tokens is known, the LATEXT(i) array (zzinf_text)
  439.  * is allocated and it's pointers are set to the tokens in zzinf_text_buffer.
  440.  */
  441. void
  442. #ifdef __STDC__
  443. zzfill_inf_look(void)
  444. #else
  445. zzfill_inf_look()
  446. #endif
  447. {
  448.     int tok;
  449.     int zzinf_token_buffer_size = ZZINF_DEF_TOKEN_BUFFER_SIZE;
  450.     int zzinf_text_buffer_size = ZZINF_DEF_TEXT_BUFFER_SIZE;
  451.     int zzinf_text_buffer_index = 0;
  452.     int zzinf_lap = 0;
  453.  
  454.     /* allocate text/token buffers */
  455.     zzinf_text_buffer = (char *) malloc(zzinf_text_buffer_size);
  456.     if ( zzinf_text_buffer == NULL )
  457.     {
  458.         fprintf(stderr, "cannot allocate lookahead text buffer (%d bytes)\n", 
  459.         zzinf_text_buffer_size);
  460.         exit(-1);                                        
  461.     }
  462.     zzinf_tokens = (int *) calloc(zzinf_token_buffer_size,sizeof(int)); 
  463.     if ( zzinf_tokens == NULL )
  464.     {
  465.         fprintf(stderr,    "cannot allocate token buffer (%d tokens)\n", 
  466.                 zzinf_token_buffer_size);
  467.         exit(-1);                                        
  468.     }
  469.  
  470.     /* get tokens, copying text to text buffer */
  471.     zzinf_text_buffer_index = 0;
  472.     do {
  473.         zzgettok();
  474.         while ( zzinf_lap>=zzinf_token_buffer_size )
  475.         {
  476.             zzinf_token_buffer_size += ZZINF_BUFFER_TOKEN_CHUNK_SIZE; 
  477.             zzinf_tokens = (int *) realloc(zzinf_tokens,
  478.                                                  zzinf_token_buffer_size*sizeof(int));
  479.             if ( zzinf_tokens == NULL )
  480.             {
  481.                 fprintf(stderr, "cannot allocate lookahead token buffer (%d tokens)\n", 
  482.                         zzinf_token_buffer_size);
  483.                 exit(-1);
  484.             }
  485.         }
  486.         while ( (zzinf_text_buffer_index+strlen(NLATEXT)+1) >= zzinf_text_buffer_size )
  487.         {
  488.             zzinf_text_buffer_size += ZZINF_BUFFER_TEXT_CHUNK_SIZE; 
  489.             zzinf_text_buffer = (char *) realloc(zzinf_text_buffer,
  490.                                                  zzinf_text_buffer_size);
  491.             if ( zzinf_text_buffer == NULL )
  492.             {
  493.                 fprintf(stderr,    "cannot allocate lookahead text buffer (%d bytes)\n", 
  494.                         zzinf_text_buffer_size);
  495.                 exit(-1);
  496.             }
  497.         }
  498.         /* record token and text of input symbol */
  499.         tok = zzinf_tokens[zzinf_lap] = NLA;
  500.         strcpy(&zzinf_text_buffer[zzinf_text_buffer_index], NLATEXT);
  501.         zzinf_text_buffer_index += strlen(NLATEXT)+1;            
  502.         zzinf_lap++;
  503.     } while (tok!=DEFAULT_EOF_TOKEN);
  504.     zzinf_labase = 0;
  505.     zzinf_last = zzinf_lap-1;
  506.  
  507.     /* allocate ptrs to text of ith token */
  508.     zzinf_text = (char **) calloc(zzinf_last+1,sizeof(char *));
  509.     if ( zzinf_text == NULL )
  510.     {
  511.         fprintf(stderr,    "cannot allocate lookahead text buffer (%d)\n", 
  512.                 zzinf_text_buffer_size); 
  513.         exit(-1);                                            
  514.     }                                                        
  515.     zzinf_text_buffer_index = 0;
  516.     zzinf_lap = 0;
  517.     /* set ptrs so that zzinf_text[i] is the text of the ith token found on input */
  518.     while (zzinf_lap<=zzinf_last)
  519.     {
  520.         zzinf_text[zzinf_lap++] = &zzinf_text_buffer[zzinf_text_buffer_index]; 
  521.         zzinf_text_buffer_index += strlen(&zzinf_text_buffer[zzinf_text_buffer_index])+1; 
  522.     }
  523. }
  524. #endif
  525.  
  526. #endif /* ERR_H */
  527.