4 * Standard error handling mechanism
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.
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
26 * Has grown to hold all kinds of stuff (err.h is increasingly misnamed)
30 * Parr Research Corporation
31 * with Purdue University and AHPCRC, University of Minnesota
48 /* Define usable bits per unsigned int word (used for set stuff) */
50 #define BSETWORDSIZE 16
51 #define BSETLOGWORDSIZE 4
53 #define BSETWORDSIZE 32
54 #define BSETLOGWORDSIZE 5
58 #define BSETWORDSIZE 8
59 #define BSETLOGWORDSIZE 3 /* SetWordType is 8bits */
61 #define BSETMODWORD(x) ((x) & (BSETWORDSIZE-1)) /* x % BSETWORDSIZE */
62 #define BSETDIVWORD(x) ((x) >> BSETLOGWORDSIZE) /* x / BSETWORDSIZE */
64 /* This is not put into the global pccts_parser structure because it is
65 * hidden and does not need to be saved during a "save state" operation
67 /* maximum of 32 bits/unsigned int and must be 8 bits/byte */
68 static SetWordType bitmask[] = {
69 0x00000001, 0x00000002, 0x00000004, 0x00000008,
70 0x00000010, 0x00000020, 0x00000040, 0x00000080
75 zzresynch(SetWordType *wd,SetWordType mask)
78 SetWordType *wd, mask;
81 static int consumed = 1;
83 /* if you enter here without having consumed a token from last resynch
84 * force a token consumption.
86 if ( !consumed ) {zzCONSUME; return;}
88 /* if current token is in resynch set, we've got what we wanted */
89 if ( wd[LA(1)]&mask || LA(1) == zzEOF_TOKEN ) {consumed=0; return;}
91 /* scan until we find something in the resynch set */
92 while ( !(wd[LA(1)]&mask) && LA(1) != zzEOF_TOKEN ) {zzCONSUME;}
98 zzconsumeUntil(SetWordType *st)
104 while ( !zzset_el(LA(1), st) ) { zzCONSUME; }
109 zzconsumeUntilToken(int t)
111 zzconsumeUntilToken(t)
115 while ( LA(1)!=t ) { zzCONSUME; }
119 * zzFAIL(k, e1, e2, ...,&zzMissSet,&zzMissText,&zzBadTok,&zzBadText)
120 * where the zzMiss stuff is set here to the token that did not match
121 * (and which set wasn't it a member of).
132 static char text[LL_K*ZZLEXBUFSIZE+1];
133 SetWordType *f[LL_K];
135 static char text[ZZLEXBUFSIZE+1];
138 SetWordType **miss_set;
152 k = va_arg(ap, int); /* how many lookahead sets? */
155 for (i=1; i<=k; i++) /* collect all lookahead sets */
157 f[i-1] = va_arg(ap, SetWordType *);
159 for (i=1; i<=k; i++) /* look for offending token */
161 if ( i>1 ) strcat(text, " ");
162 strcat(text, LATEXT(i));
163 if ( !zzset_el((unsigned)LA(i), f[i-1]) ) break;
165 miss_set = va_arg(ap, SetWordType **);
166 miss_text = va_arg(ap, char **);
167 bad_tok = va_arg(ap, int *);
168 bad_text = va_arg(ap, char **);
169 err_k = va_arg(ap, int *);
172 /* bad; lookahead is permutation that cannot be matched,
173 * but, the ith token of lookahead is valid at the ith position
174 * (The old LL sub 1 (k) versus LL(k) parsing technique)
177 *miss_text = zzlextext;
179 *bad_text = LATEXT(1);
183 /* fprintf(stderr, "%s not in %dth set\n", zztokens[LA(i)], i);*/
187 *bad_text = LATEXT(i);
188 if ( i==1 ) *err_k = 1;
194 zzsave_antlr_state(zzantlr_state *buf)
196 zzsave_antlr_state(buf)
205 buf->guess_start = zzguess_start;
206 buf->guessing = zzguessing;
210 buf->ast_sp = zzast_sp;
213 buf->inf_labase = zzinf_labase;
214 buf->inf_last = zzinf_last;
217 buf->dirty = zzdirty;
220 for (i=0; i<LL_K; i++) buf->tokenLA[i] = zztokenLA[i];
221 for (i=0; i<LL_K; i++) strcpy(buf->textLA[i], zztextLA[i]);
223 buf->labase = zzlabase;
225 buf->token = zztoken;
226 strcpy(buf->text, zzlextext);
232 zzrestore_antlr_state(zzantlr_state *buf)
234 zzrestore_antlr_state(buf)
243 zzguess_start = buf->guess_start;
244 zzguessing = buf->guessing;
248 zzast_sp = buf->ast_sp;
251 zzinf_labase = buf->inf_labase;
252 zzinf_last = buf->inf_last;
255 zzdirty = buf->dirty;
258 for (i=0; i<LL_K; i++) zztokenLA[i] = buf->tokenLA[i];
259 for (i=0; i<LL_K; i++) strcpy(zztextLA[i], buf->textLA[i]);
261 zzlabase = buf->labase;
263 zztoken = buf->token;
264 strcpy(zzlextext, buf->text);
270 zzedecode(SetWordType *a)
276 register SetWordType *p = a;
277 register SetWordType *endp = &(p[zzSET_SIZE]);
278 register unsigned e = 0;
280 if ( zzset_deg(a)>1 ) fprintf(stderr, " {");
282 register SetWordType t = *p;
283 register SetWordType *b = &(bitmask[0]);
285 if ( t & *b ) fprintf(stderr, " %s", zztokens[e]);
287 } while (++b < &(bitmask[sizeof(SetWordType)*8]));
288 } while (++p < endp);
289 if ( zzset_deg(a)>1 ) fprintf(stderr, " }");
293 /* standard error reporting function */
296 zzsyn(char *text, int tok, char *egroup, SetWordType *eset, int etok, int k, char *bad_text)
298 zzsyn(text, tok, egroup, eset, etok, k, bad_text)
299 char *text, *egroup, *bad_text;
307 fprintf(stderr, "line %d: syntax error at \"%s\"", zzline, (tok==zzEOF_TOKEN)?"EOF":bad_text);
308 if ( !etok && !eset ) {fprintf(stderr, "\n"); return;}
309 if ( k==1 ) fprintf(stderr, " missing");
312 fprintf(stderr, "; \"%s\" not", bad_text);
313 if ( zzset_deg(eset)>1 ) fprintf(stderr, " in");
315 if ( zzset_deg(eset)>0 ) zzedecode(eset);
316 else fprintf(stderr, " %s", zztokens[etok]);
317 if ( strlen(egroup) > 0 ) fprintf(stderr, " in %s", egroup);
318 fprintf(stderr, "\n");
322 /* is b an element of set p? */
325 zzset_el(unsigned b, SetWordType *p)
332 return( p[BSETDIVWORD(b)] & bitmask[BSETMODWORD(b)] );
337 zzset_deg(SetWordType *a)
343 /* Fast compute degree of a set... the number
344 of elements present in the set. Assumes
345 that all word bits are used in the set
347 register SetWordType *p = a;
348 register SetWordType *endp = &(a[zzSET_SIZE]);
349 register int degree = 0;
351 if ( a == NULL ) return 0;
354 register SetWordType t = *p;
355 register SetWordType *b = &(bitmask[0]);
357 if (t & *b) ++degree;
358 } while (++b < &(bitmask[sizeof(SetWordType)*8]));
370 _zzmatch(int _t, char **zzBadText, char **zzMissText,
371 int *zzMissTok, int *zzBadTok,
372 SetWordType **zzMissSet)
374 _zzmatch(_t, zzBadText, zzMissText, zzMissTok, zzBadTok, zzMissSet)
378 int *zzMissTok, *zzBadTok;
379 SetWordType **zzMissSet;
382 if ( zzdirty==LL_K ) {
386 *zzBadText = *zzMissText=LATEXT(1);
387 *zzMissTok= _t; *zzBadTok=LA(1);
399 _zzmatch_wsig(int _t)
405 if ( zzdirty==LL_K ) {
421 _zzmatch(int _t, char **zzBadText, char **zzMissText,
422 int *zzMissTok, int *zzBadTok, SetWordType **zzMissSet)
424 _zzmatch(_t, zzBadText, zzMissText, zzMissTok, zzBadTok, zzMissSet)
428 int *zzMissTok, *zzBadTok;
429 SetWordType **zzMissSet;
432 if ( zzdirty ) {zzCONSUME;}
434 *zzBadText = *zzMissText=LATEXT(1);
435 *zzMissTok= _t; *zzBadTok=LA(1);
446 _zzmatch_wsig(int _t)
452 if ( zzdirty ) {zzCONSUME;}
467 _zzmatch(int _t, char **zzBadText, char **zzMissText,
468 int *zzMissTok, int *zzBadTok,
469 SetWordType **zzMissSet)
471 _zzmatch(_t, zzBadText, zzMissText, zzMissTok, zzBadTok, zzMissSet)
475 int *zzMissTok, *zzBadTok;
476 SetWordType **zzMissSet;
480 *zzBadText = *zzMissText=LATEXT(1);
481 *zzMissTok= _t; *zzBadTok=LA(1);
491 _zzmatch_wsig(int _t)
497 if ( LA(1)!=_t ) return 0;
502 #endif /*DEMAND_LOOK*/
512 if ( zzinf_labase >= zzinf_last )
513 {NLA = zzEOF_TOKEN; strcpy(NLATEXT, "");}
515 NLA = zzinf_tokens[zzinf_labase];
516 zzline = zzinf_line[zzinf_labase]; /* wrong in 1.21 */
517 strcpy(NLATEXT, zzinf_text[zzinf_labase]);
524 /* allocate default size text,token and line arrays;
525 * then, read all of the input reallocing the arrays as needed.
526 * Once the number of total tokens is known, the LATEXT(i) array (zzinf_text)
527 * is allocated and it's pointers are set to the tokens in zzinf_text_buffer.
531 zzfill_inf_look(void)
537 int zzinf_token_buffer_size = ZZINF_DEF_TOKEN_BUFFER_SIZE;
538 int zzinf_text_buffer_size = ZZINF_DEF_TEXT_BUFFER_SIZE;
539 int zzinf_text_buffer_index = 0;
542 /* allocate text/token buffers */
543 zzinf_text_buffer = (char *) malloc(zzinf_text_buffer_size);
544 if ( zzinf_text_buffer == NULL )
546 fprintf(stderr, "cannot allocate lookahead text buffer (%d bytes)\n",
547 zzinf_text_buffer_size);
548 exit(PCCTS_EXIT_FAILURE);
550 zzinf_tokens = (int *) calloc(zzinf_token_buffer_size,sizeof(int));
551 if ( zzinf_tokens == NULL )
553 fprintf(stderr, "cannot allocate token buffer (%d tokens)\n",
554 zzinf_token_buffer_size);
555 exit(PCCTS_EXIT_FAILURE);
557 zzinf_line = (int *) calloc(zzinf_token_buffer_size,sizeof(int));
558 if ( zzinf_line == NULL )
560 fprintf(stderr, "cannot allocate line buffer (%d ints)\n",
561 zzinf_token_buffer_size);
562 exit(PCCTS_EXIT_FAILURE);
565 /* get tokens, copying text to text buffer */
566 zzinf_text_buffer_index = 0;
570 while ( zzinf_lap>=zzinf_token_buffer_size )
572 zzinf_token_buffer_size += ZZINF_BUFFER_TOKEN_CHUNK_SIZE;
573 zzinf_tokens = (int *) realloc(zzinf_tokens,
574 zzinf_token_buffer_size*sizeof(int));
575 if ( zzinf_tokens == NULL )
577 fprintf(stderr, "cannot allocate lookahead token buffer (%d tokens)\n",
578 zzinf_token_buffer_size);
579 exit(PCCTS_EXIT_FAILURE);
581 zzinf_line = (int *) realloc(zzinf_line,
582 zzinf_token_buffer_size*sizeof(int));
583 if ( zzinf_line == NULL )
585 fprintf(stderr, "cannot allocate lookahead line buffer (%d ints)\n",
586 zzinf_token_buffer_size);
587 exit(PCCTS_EXIT_FAILURE);
591 while ( (zzinf_text_buffer_index+strlen(NLATEXT)+1) >= zzinf_text_buffer_size )
593 zzinf_text_buffer_size += ZZINF_BUFFER_TEXT_CHUNK_SIZE;
594 zzinf_text_buffer = (char *) realloc(zzinf_text_buffer,
595 zzinf_text_buffer_size);
596 if ( zzinf_text_buffer == NULL )
598 fprintf(stderr, "cannot allocate lookahead text buffer (%d bytes)\n",
599 zzinf_text_buffer_size);
600 exit(PCCTS_EXIT_FAILURE);
603 /* record token and text and line of input symbol */
604 tok = zzinf_tokens[zzinf_lap] = NLA;
605 strcpy(&zzinf_text_buffer[zzinf_text_buffer_index], NLATEXT);
606 zzinf_text_buffer_index += strlen(NLATEXT)+1;
607 zzinf_line[zzinf_lap] = line;
609 } while (tok!=zzEOF_TOKEN);
611 zzinf_last = zzinf_lap-1;
613 /* allocate ptrs to text of ith token */
614 zzinf_text = (char **) calloc(zzinf_last+1,sizeof(char *));
615 if ( zzinf_text == NULL )
617 fprintf(stderr, "cannot allocate lookahead text buffer (%d)\n",
618 zzinf_text_buffer_size);
619 exit(PCCTS_EXIT_FAILURE);
621 zzinf_text_buffer_index = 0;
623 /* set ptrs so that zzinf_text[i] is the text of the ith token found on input */
624 while (zzinf_lap<=zzinf_last)
626 zzinf_text[zzinf_lap++] = &zzinf_text_buffer[zzinf_text_buffer_index];
627 zzinf_text_buffer_index += strlen(&zzinf_text_buffer[zzinf_text_buffer_index])+1;
634 _zzsetmatch(SetWordType *e, char **zzBadText, char **zzMissText,
635 int *zzMissTok, int *zzBadTok,
636 SetWordType **zzMissSet)
638 _zzsetmatch(e, zzBadText, zzMissText, zzMissTok, zzBadTok, zzMissSet)
642 int *zzMissTok, *zzBadTok;
643 SetWordType **zzMissSet;
648 if ( zzdirty==LL_K ) {zzCONSUME;}
650 if ( zzdirty ) {zzCONSUME;}
653 if ( !zzset_el((unsigned)LA(1), e) ) {
654 *zzBadText = LATEXT(1); *zzMissText=NULL;
655 *zzMissTok= 0; *zzBadTok=LA(1);
672 _zzmatch_wdfltsig(int tokenWanted, SetWordType *whatFollows)
674 _zzmatch_wdfltsig(tokenWanted, whatFollows)
676 SetWordType *whatFollows;
681 if ( zzdirty==LL_K ) {
685 if ( zzdirty ) {zzCONSUME;}
689 if ( LA(1)!=tokenWanted )
692 "line %d: syntax error at \"%s\" missing %s\n",
694 (LA(1)==zzEOF_TOKEN)?"<eof>":LATEXT(1),
695 zztokens[tokenWanted]);
696 zzconsumeUntil( whatFollows );
709 /* zzCONSUME; consume if not demand lookahead */
717 _zzsetmatch_wdfltsig(SetWordType *tokensWanted,
719 SetWordType *whatFollows)
721 _zzsetmatch_wdfltsig(tokensWanted, tokenTypeOfSet, whatFollows)
722 SetWordType *tokensWanted;
724 SetWordType *whatFollows;
729 if ( zzdirty==LL_K ) {zzCONSUME;}
731 if ( zzdirty ) {zzCONSUME;}
734 if ( !zzset_el((unsigned)LA(1), tokensWanted) )
737 "line %d: syntax error at \"%s\" missing %s\n",
739 (LA(1)==zzEOF_TOKEN)?"<eof>":LATEXT(1),
740 zztokens[tokenTypeOfSet]);
741 zzconsumeUntil( whatFollows );
754 /* zzCONSUME; consume if not demand lookahead */
762 _zzsetmatch_wsig(SetWordType *e)
770 if ( zzdirty==LL_K ) {zzCONSUME;}
772 if ( zzdirty ) {zzCONSUME;}
775 if ( !zzset_el((unsigned)LA(1), e) ) return 0;
787 #ifdef USER_ZZMODE_STACK
788 static int zzmstk[ZZMAXSTK] = { -1 };
789 static int zzmdep = 0;
790 static char zzmbuf[70];
800 if(zzmdep == ZZMAXSTK - 1) {
801 sprintf(zzmbuf, "Mode stack overflow ");
804 zzmstk[zzmdep++] = zzauto;
817 { sprintf(zzmbuf, "Mode stack underflow ");
822 zzmode(zzmstk[zzmdep]);
828 zzsave_mode_stack( int modeStack[], int *modeLevel )
830 zzsave_mode_stack( modeStack, modeLevel )
836 memcpy(modeStack, zzmstk, sizeof(zzmstk));
845 zzrestore_mode_stack( int modeStack[], int *modeLevel )
847 zzrestore_mode_stack( modeStack, modeLevel )
854 memcpy(zzmstk, modeStack, sizeof(zzmstk));
859 #endif /* USER_ZZMODE_STACK */