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  * ANTLR 1.33
29  * Terence Parr
30  * Parr Research Corporation
31  * with Purdue University and AHPCRC, University of Minnesota
32  * 1989-2000
33  */
34 
35 #ifndef ERR_H
36 #define ERR_H
37 
38 #include "pcctscfg.h"
39 #include <stdlib.h>
40 #include <assert.h>
41 
42 /*									      */
43 /*  7-Apr-97  133MR1							      */
44 /*		Proper choice of STDC and cplusplus pre-processor symbols (?) */
45 /*									      */
46 #include "pccts_string.h"
47 
48 #ifdef PCCTS_USE_STDARG
49 #include "pccts_stdarg.h"
50 #else
51 #include <varargs.h>
52 #endif
53 
54 #ifdef DUM
55 /* Define usable bits per unsigned int word (used for set stuff) */
56 #ifdef PC
57 #define BSETWORDSIZE 16
58 #define BSETLOGWORDSIZE	4
59 #else
60 #define	BSETWORDSIZE 32
61 #define BSETLOGWORDSIZE 5
62 #endif
63 #endif
64 
65 #define	BSETWORDSIZE 8
66 #define BSETLOGWORDSIZE 3		/* SetWordType is 8bits */
67 
68 #define	BSETMODWORD(x) ((x) & (BSETWORDSIZE-1))		/* x % BSETWORDSIZE */
69 #define	BSETDIVWORD(x) ((x) >> BSETLOGWORDSIZE)		/* x / BSETWORDSIZE */
70 
71 /* This is not put into the global pccts_parser structure because it is
72  * hidden and does not need to be saved during a "save state" operation
73  */
74 /* maximum of 32 bits/unsigned int and must be 8 bits/byte */
75 static SetWordType bitmask[] = {
76 	0x00000001, 0x00000002, 0x00000004, 0x00000008,
77 	0x00000010, 0x00000020, 0x00000040, 0x00000080
78 };
79 
80 #ifdef zzTRACE_RULES
81 int  zzTraceOptionValueDefault=1;
82 int  zzTraceOptionValue=1;
83 int  zzTraceGuessOptionValue=1;
84 char *zzTraceCurrentRuleName=NULL;
85 int  zzTraceDepth=0;
86 #endif
87 
88 int  zzGuessSeq=0;          /* MR10 */
89 int  zzSyntaxErrCount=0;    /* MR11 */
90 int  zzLexErrCount=0;       /* MR11 */
91 
92 void
93 #ifdef __USE_PROTOS
zzresynch(SetWordType * wd,SetWordType mask)94 zzresynch(SetWordType *wd,SetWordType mask)
95 #else
96 zzresynch(wd,mask)
97 SetWordType *wd, mask;
98 #endif
99 {
100 	static int consumed = 1;
101 
102 	/* if you enter here without having consumed a token from last resynch
103 	 * force a token consumption.
104 	 */
105 	if ( !consumed ) {zzCONSUME; consumed=1; return;}   /* MR10 */
106 
107 	/* if current token is in resynch set, we've got what we wanted */
108 	if ( wd[LA(1)]&mask || LA(1) == zzEOF_TOKEN ) {consumed=0; return;}
109 
110 	/* scan until we find something in the resynch set */
111 	while ( !(wd[LA(1)]&mask) && LA(1) != zzEOF_TOKEN ) {zzCONSUME;}
112 	consumed=1;
113 }
114 
115 /*                                                                          */
116 /*  7-Apr-97 133MR1 for C++ and MR7 for C                                   */
117 /*   	     Change suggested by Eli Sternheim (eli@interhdl.com)           */
118 /*                                                                          */
119 
120 void
121 #ifdef __USE_PROTOS
zzconsumeUntil(SetWordType * st)122 zzconsumeUntil(SetWordType *st)
123 #else
124 zzconsumeUntil(st)
125 SetWordType *st;
126 #endif
127 {
128     int     tmp;                                                     /* MR7 */
129 	while ( !zzset_el( (tmp=LA(1)), st) && tmp!=1 /* Eof */) {       /* MR7 */
130                                                       zzCONSUME; }   /* MR7 */
131 }
132 
133 /*                                                                          */
134 /*  7-Apr-97 133MR1 for C++ and MR7 for C                                   */
135 /*   	     Change suggested by Eli Sternheim (eli@interhdl.com)           */
136 /*                                                                          */
137 
138 void
139 #ifdef __USE_PROTOS
zzconsumeUntilToken(int t)140 zzconsumeUntilToken(int t)
141 #else
142 zzconsumeUntilToken(t)
143 int t;
144 #endif
145 {
146     int     tmp;                                                     /* MR7 */
147 	while ( (tmp=LA(1)) !=t && tmp!=1 /* Eof */) { zzCONSUME; }      /* MR7 */
148 }
149 
150 /* input looks like:
151  *		zzFAIL(k, e1, e2, ...,&zzMissSet,&zzMissText,&zzBadTok,&zzBadText)
152  * where the zzMiss stuff is set here to the token that did not match
153  * (and which set wasn't it a member of).
154  */
155 
156 #ifdef PCCTS_USE_STDARG
zzFAIL(int k,...)157 void zzFAIL(int k, ...)
158 #else
159 void zzFAIL(va_alist)
160 va_dcl
161 #endif
162 {
163 #ifdef LL_K
164 	static char text[LL_K*ZZLEXBUFSIZE+1];
165 	SetWordType *f[LL_K];
166 #else
167 	static char text[ZZLEXBUFSIZE+1];
168 	SetWordType *f[1];
169 #endif
170 	SetWordType **miss_set;
171 	char **miss_text;
172 	int *bad_tok;
173 	char **bad_text;
174 	int *err_k;
175 	int i;
176 	va_list ap;
177 #ifndef PCCTS_USE_STDARG			/* MR20 */
178 	int k;
179 #endif
180 #ifdef PCCTS_USE_STDARG         /* MR20 */
181 	va_start(ap, k);
182 #else
183 	va_start(ap);
184 	k = va_arg(ap, int);	/* how many lookahead sets? */
185 #endif
186     assert(k <= sizeof(f)/sizeof(f[0]));    /* MR20 G. Hobbelt */
187 	text[0] = '\0';
188 	for (i=1; i<=k; i++)	/* collect all lookahead sets */
189 	{
190 		f[i-1] = va_arg(ap, SetWordType *);
191 	}
192 	for (i=1; i<=k; i++)	/* look for offending token */
193 	{
194 		if ( i>1 ) strcat(text, " ");
195 		strcat(text, LATEXT(i));
196 		if ( !zzset_el((unsigned)LA(i), f[i-1]) ) break;
197 	}
198 	miss_set = va_arg(ap, SetWordType **);
199 	miss_text = va_arg(ap, char **);
200 	bad_tok = va_arg(ap, int *);
201 	bad_text = va_arg(ap, char **);
202 	err_k = va_arg(ap, int *);
203 	if ( i>k )
204 	{
205 		/* bad; lookahead is permutation that cannot be matched,
206 		 * but, the ith token of lookahead is valid at the ith position
207 		 * (The old LL sub 1 (k) versus LL(k) parsing technique)
208 		 */
209 		*miss_set = NULL;
210 		*miss_text = zzlextext;
211 		*bad_tok = LA(1);
212 		*bad_text = LATEXT(1);
213 		*err_k = k;
214 		return;
215 	}
216 /*	fprintf(stderr, "%s not in %dth set\n", zztokens[LA(i)], i);*/
217 	*miss_set = f[i-1];
218 	*miss_text = text;
219 	*bad_tok = LA(i);
220 	*bad_text = LATEXT(i);
221 	if ( i==1 ) *err_k = 1;
222 	else *err_k = k;
223 }
224 
225 #ifdef __USE_PROTOS
zzTraceGuessDone(zzantlr_state * state)226 void zzTraceGuessDone(zzantlr_state *state)
227 #else
228 void zzTraceGuessDone(state)
229   zzantlr_state     *state;
230 #endif
231 {
232 #ifdef zzTRACE_RULES
233 #ifdef ZZCAN_GUESS
234 
235   int   doIt=0;
236 
237   if (zzTraceCurrentRuleName == NULL) return;
238 
239   if (zzTraceOptionValue <= 0) {
240     doIt=0;
241   } else if (zzTraceGuessOptionValue <= 0) {
242     doIt=0;
243   } else {
244     doIt=1;
245   };
246 
247   if (doIt) {
248     fprintf(stderr,"guess done - returning to rule %s {\"%s\"} at depth %d",
249         state->traceCurrentRuleName,
250         LATEXT(1),
251         state->traceDepth);
252     if (state->guessing != 0) {
253       fprintf(stderr," (guess mode continues - an enclosing guess is still active)");
254     } else {
255       fprintf(stderr," (guess mode ends)");
256     };
257     fprintf(stderr,"\n");
258   };
259 #endif
260 #endif
261 }
262 
263 void
264 #ifdef __USE_PROTOS
zzsave_antlr_state(zzantlr_state * buf)265 zzsave_antlr_state(zzantlr_state *buf)
266 #else
267 zzsave_antlr_state(buf)
268 zzantlr_state *buf;
269 #endif
270 {
271 #ifdef LL_K
272 	int     i;
273 #endif
274 
275 #ifdef ZZCAN_GUESS
276 	buf->guess_start = zzguess_start;
277 	buf->guessing = zzguessing;
278 #endif
279 	buf->asp = zzasp;
280 #ifdef GENAST
281 	buf->ast_sp = zzast_sp;
282 #endif
283 #ifdef ZZINF_LOOK
284 	buf->inf_labase = zzinf_labase;
285 	buf->inf_last = zzinf_last;
286 
287 /* MR6 	Gunnar Rxnning (gunnar@candleweb.no)                                */
288 /* MR6	  Additional state needs to be saved/restored                       */
289 
290   	buf->inf_tokens = zzinf_tokens;                                  /* MR6 */
291 	buf->inf_text = zzinf_text;                                      /* MR6 */
292 	buf->inf_text_buffer = zzinf_text_buffer;                        /* MR6 */
293 	buf->inf_line = zzinf_line;			                             /* MR6 */
294 
295 #endif
296 #ifdef DEMAND_LOOK
297 	buf->dirty = zzdirty;
298 #endif
299 #ifdef LL_K
300 	for (i=0; i<LL_K; i++) buf->tokenLA[i] = zztokenLA[i];
301 	for (i=0; i<LL_K; i++) strcpy(buf->textLA[i], zztextLA[i]);
302 	buf->lap = zzlap;
303 	buf->labase = zzlabase;
304 #else
305 	buf->token = zztoken;
306 	strcpy(buf->text, zzlextext);
307 #endif
308 #ifdef zzTRACE_RULES
309 
310     /* MR10 */
311 
312     buf->traceOptionValue=zzTraceOptionValue;
313     buf->traceGuessOptionValue=zzTraceGuessOptionValue;
314     buf->traceCurrentRuleName=zzTraceCurrentRuleName;
315     buf->traceDepth=zzTraceDepth;
316 #endif
317 }
318 
319 void
320 #ifdef __USE_PROTOS
zzrestore_antlr_state(zzantlr_state * buf)321 zzrestore_antlr_state(zzantlr_state *buf)
322 #else
323 zzrestore_antlr_state(buf)
324 zzantlr_state *buf;
325 #endif
326 {
327 
328 #ifdef zzTRACE_RULES
329     int     prevTraceOptionValue;
330 #endif
331 
332 #ifdef LL_K
333 	int     i;
334 #endif
335 
336 #ifdef ZZCAN_GUESS
337 	zzguess_start = buf->guess_start;
338 	zzguessing = buf->guessing;
339 #endif
340 	zzasp = buf->asp;
341 #ifdef GENAST
342 	zzast_sp = buf->ast_sp;
343 #endif
344 #ifdef ZZINF_LOOK
345 	zzinf_labase = buf->inf_labase;
346 	zzinf_last = buf->inf_last;
347 
348 /* MR6 	Gunnar Rxnning (gunnar@candleweb.no)                                */
349 /* MR6	  Additional state needs to be saved/restored                       */
350 
351 	zzinf_tokens = buf->inf_tokens;                                  /* MR6 */
352 	zzinf_text = buf->inf_text;                                      /* MR6 */
353 	zzinf_text_buffer = buf->inf_text_buffer;                        /* MR6 */
354 	zzinf_line = buf->inf_line;			                             /* MR6 */
355 #endif
356 #ifdef DEMAND_LOOK
357 	zzdirty = buf->dirty;
358 #endif
359 #ifdef LL_K
360 	for (i=0; i<LL_K; i++) zztokenLA[i] = buf->tokenLA[i];
361 	for (i=0; i<LL_K; i++) strcpy(zztextLA[i], buf->textLA[i]);
362 	zzlap = buf->lap;
363 	zzlabase = buf->labase;
364 #else
365 	zztoken = buf->token;
366 	strcpy(zzlextext, buf->text);
367 #endif
368 #ifdef zzTRACE_RULES
369 
370     prevTraceOptionValue=zzTraceOptionValue;
371     zzTraceOptionValue=buf->traceOptionValue;
372     if ( (prevTraceOptionValue > 0) !=
373              (zzTraceOptionValue > 0)) {
374       if (zzTraceOptionValue > 0) {
375         fprintf(stderr,"trace enable restored in rule %s depth %d\n",
376                         zzTraceCurrentRuleName,zzTraceDepth);
377       };
378       if (zzTraceOptionValue <= 0) {
379         fprintf(stderr,"trace disable restored in rule %s depth %d\n",
380                         zzTraceCurrentRuleName,zzTraceDepth);
381       };
382     };
383 
384     zzTraceOptionValue=buf->traceOptionValue;            /* MR10 */
385     zzTraceGuessOptionValue=buf->traceGuessOptionValue;  /* MR10 */
386     zzTraceCurrentRuleName=buf->traceCurrentRuleName;    /* MR10 */
387     zzTraceDepth=buf->traceDepth;                        /* MR10 */
388     zzTraceGuessDone(buf);                               /* MR10 */
389 #endif
390 }
391 
392 void
393 #ifdef __USE_PROTOS
zzedecode(SetWordType * a)394 zzedecode(SetWordType *a)
395 #else
396 zzedecode(a)
397 SetWordType *a;
398 #endif
399 {
400 	register SetWordType *p = a;
401 	register SetWordType *endp = &(p[zzSET_SIZE]);
402 	register unsigned e = 0;
403 
404 	if ( zzset_deg(a)>1 ) fprintf(stderr, " {");
405 	do {
406 		register SetWordType t = *p;
407 		register SetWordType *b = &(bitmask[0]);
408 		do {
409 			if ( t & *b ) fprintf(stderr, " %s", zztokens[e]);
410 			e++;
411 		} while (++b < &(bitmask[sizeof(SetWordType)*8]));
412 	} while (++p < endp);
413 	if ( zzset_deg(a)>1 ) fprintf(stderr, " }");
414 }
415 
416 #ifndef USER_ZZSYN
417 /* standard error reporting function */
418 void
419 #ifdef __USE_PROTOS
zzsyn(char * text,int tok,char * egroup,SetWordType * eset,int etok,int k,char * bad_text)420 zzsyn(char *text, int tok, char *egroup, SetWordType *eset, int etok, int k, char *bad_text)
421 #else
422 zzsyn(text, tok, egroup, eset, etok, k, bad_text)
423 char *text, *egroup, *bad_text;
424 int tok;
425 int etok;
426 int k;
427 SetWordType *eset;
428 #endif
429 {
430 
431     zzSyntaxErrCount++;                             /* MR11 */
432 	fprintf(stderr, "line %d: syntax error at \"%s\"", zzline, (tok==zzEOF_TOKEN)?"EOF":bad_text);
433 	if ( !etok && !eset ) {fprintf(stderr, "\n"); return;}
434 	if ( k==1 ) fprintf(stderr, " missing");
435 	else
436 	{
437 		fprintf(stderr, "; \"%s\" not", bad_text);
438 		if ( zzset_deg(eset)>1 ) fprintf(stderr, " in");
439 	}
440 	if ( zzset_deg(eset)>0 ) zzedecode(eset);
441 	else fprintf(stderr, " %s", zztokens[etok]);
442 	if ( strlen(egroup) > 0 ) fprintf(stderr, " in %s", egroup);
443 	fprintf(stderr, "\n");
444 }
445 #endif
446 
447 /* is b an element of set p? */
448 int
449 #ifdef __USE_PROTOS
zzset_el(unsigned b,SetWordType * p)450 zzset_el(unsigned b, SetWordType *p)
451 #else
452 zzset_el(b,p)
453 unsigned b;
454 SetWordType *p;
455 #endif
456 {
457 	return( p[BSETDIVWORD(b)] & bitmask[BSETMODWORD(b)] );
458 }
459 
460 int
461 #ifdef __USE_PROTOS
zzset_deg(SetWordType * a)462 zzset_deg(SetWordType *a)
463 #else
464 zzset_deg(a)
465 SetWordType *a;
466 #endif
467 {
468 	/* Fast compute degree of a set... the number
469 	   of elements present in the set.  Assumes
470 	   that all word bits are used in the set
471 	*/
472 	register SetWordType *p = a;
473 	register SetWordType *endp = &(a[zzSET_SIZE]);
474 	register int degree = 0;
475 
476 	if ( a == NULL ) return 0;
477 	while ( p < endp )
478 	{
479 		register SetWordType t = *p;
480 		register SetWordType *b = &(bitmask[0]);
481 		do {
482 			if (t & *b) ++degree;
483 		} while (++b < &(bitmask[sizeof(SetWordType)*8]));
484 		p++;
485 	}
486 
487 	return(degree);
488 }
489 
490 #ifdef DEMAND_LOOK
491 
492 #ifdef LL_K
493 int
494 #ifdef __USE_PROTOS
_zzmatch(int _t,char ** zzBadText,char ** zzMissText,int * zzMissTok,int * zzBadTok,SetWordType ** zzMissSet)495 _zzmatch(int _t, char **zzBadText, char **zzMissText,
496 		int *zzMissTok, int *zzBadTok,
497 		SetWordType **zzMissSet)
498 #else
499 _zzmatch(_t, zzBadText, zzMissText, zzMissTok, zzBadTok, zzMissSet)
500 int _t;
501 char **zzBadText;
502 char **zzMissText;
503 int *zzMissTok, *zzBadTok;
504 SetWordType **zzMissSet;
505 #endif
506 {
507 	if ( zzdirty==LL_K ) {
508 		zzCONSUME;
509 	}
510 	if ( LA(1)!=_t ) {
511 		*zzBadText = *zzMissText=LATEXT(1);
512 		*zzMissTok= _t; *zzBadTok=LA(1);
513 		*zzMissSet=NULL;
514 		return 0;
515 	}
516 	zzMakeAttr
517 	zzdirty++;
518 	zzlabase++;
519 	return 1;
520 }
521 
522 int
523 #ifdef __USE_PROTOS
_zzmatch_wsig(int _t)524 _zzmatch_wsig(int _t)
525 #else
526 _zzmatch_wsig(_t)
527 int _t;
528 #endif
529 {
530 	if ( zzdirty==LL_K ) {
531 		zzCONSUME;
532 	}
533 	if ( LA(1)!=_t ) {
534 		return 0;
535 	}
536 	zzMakeAttr
537 	zzdirty++;
538 	zzlabase++;
539 	return 1;
540 }
541 
542 #else
543 
544 int
545 #ifdef __USE_PROTOS
_zzmatch(int _t,char ** zzBadText,char ** zzMissText,int * zzMissTok,int * zzBadTok,SetWordType ** zzMissSet)546 _zzmatch(int _t, char **zzBadText, char **zzMissText,
547 		 int *zzMissTok, int *zzBadTok, SetWordType **zzMissSet)
548 #else
549 _zzmatch(_t, zzBadText, zzMissText, zzMissTok, zzBadTok, zzMissSet)
550 int _t;
551 char **zzBadText;
552 char **zzMissText;
553 int *zzMissTok, *zzBadTok;
554 SetWordType **zzMissSet;
555 #endif
556 {
557 	if ( zzdirty ) {zzCONSUME;}
558 	if ( LA(1)!=_t ) {
559 		*zzBadText = *zzMissText=LATEXT(1);
560 		*zzMissTok= _t; *zzBadTok=LA(1);
561 		*zzMissSet=NULL;
562 		return 0;
563 	}
564 	zzdirty = 1;
565 	zzMakeAttr
566 	return 1;
567 }
568 
569 int
570 #ifdef __USE_PROTOS
_zzmatch_wsig(int _t)571 _zzmatch_wsig(int _t)
572 #else
573 _zzmatch_wsig(_t)
574 int _t;
575 #endif
576 {
577 	if ( zzdirty ) {zzCONSUME;}
578 	if ( LA(1)!=_t ) {
579 		return 0;
580 	}
581 	zzdirty = 1;
582 	zzMakeAttr
583 	return 1;
584 }
585 
586 #endif /*LL_K*/
587 
588 #else
589 
590 int
591 #ifdef __USE_PROTOS
_zzmatch(int _t,char ** zzBadText,char ** zzMissText,int * zzMissTok,int * zzBadTok,SetWordType ** zzMissSet)592 _zzmatch(int _t, char **zzBadText, char **zzMissText,
593 		int *zzMissTok, int *zzBadTok,
594 		SetWordType **zzMissSet)
595 #else
596 _zzmatch(_t, zzBadText, zzMissText, zzMissTok, zzBadTok, zzMissSet)
597 int _t;
598 char **zzBadText;
599 char **zzMissText;
600 int *zzMissTok, *zzBadTok;
601 SetWordType **zzMissSet;
602 #endif
603 {
604 	if ( LA(1)!=_t ) {
605 		*zzBadText = *zzMissText=LATEXT(1);
606 		*zzMissTok= _t; *zzBadTok=LA(1);
607 		*zzMissSet=NULL;
608 		return 0;
609 	}
610 	zzMakeAttr
611 	return 1;
612 }
613 
614 int
615 #ifdef __USE_PROTOS
_zzmatch_wsig(int _t)616 _zzmatch_wsig(int _t)
617 #else
618 _zzmatch_wsig(_t)
619 int _t;
620 #endif
621 {
622 	if ( LA(1)!=_t ) return 0;
623 	zzMakeAttr
624 	return 1;
625 }
626 
627 #endif /*DEMAND_LOOK*/
628 
629 #ifdef ZZINF_LOOK
630 void
631 #ifdef __USE_PROTOS
_inf_zzgettok(void)632 _inf_zzgettok(void)
633 #else
634 _inf_zzgettok()
635 #endif
636 {
637 	if ( zzinf_labase >= zzinf_last )
638 		{NLA = zzEOF_TOKEN; strcpy(NLATEXT, "");}
639 	else {
640 		NLA = zzinf_tokens[zzinf_labase];
641 		zzline = zzinf_line[zzinf_labase];	/* wrong in 1.21 */
642 		strcpy(NLATEXT, zzinf_text[zzinf_labase]);
643 		zzinf_labase++;
644 	}
645 }
646 #endif
647 
648 #ifdef ZZINF_LOOK
649 /* allocate default size text,token and line arrays;
650  * then, read all of the input reallocing the arrays as needed.
651  * Once the number of total tokens is known, the LATEXT(i) array (zzinf_text)
652  * is allocated and it's pointers are set to the tokens in zzinf_text_buffer.
653  */
654 void
655 #ifdef __USE_PROTOS
zzfill_inf_look(void)656 zzfill_inf_look(void)
657 #else
658 zzfill_inf_look()
659 #endif
660 {
661 	int tok, line;
662 	int zzinf_token_buffer_size = ZZINF_DEF_TOKEN_BUFFER_SIZE;
663 	int zzinf_text_buffer_size = ZZINF_DEF_TEXT_BUFFER_SIZE;
664 	int zzinf_text_buffer_index = 0;
665 	int zzinf_lap = 0;
666 
667 	/* allocate text/token buffers */
668 	zzinf_text_buffer = (char *) malloc(zzinf_text_buffer_size);
669 	if ( zzinf_text_buffer == NULL )
670 	{
671 		fprintf(stderr, "cannot allocate lookahead text buffer (%d bytes)\n",
672 		zzinf_text_buffer_size);
673 		exit(PCCTS_EXIT_FAILURE);
674 	}
675 	zzinf_tokens = (int *) calloc(zzinf_token_buffer_size,sizeof(int));
676 	if ( zzinf_tokens == NULL )
677 	{
678 		fprintf(stderr,	"cannot allocate token buffer (%d tokens)\n",
679 				zzinf_token_buffer_size);
680 		exit(PCCTS_EXIT_FAILURE);
681 	}
682     zzinf_line = (int *) calloc(zzinf_token_buffer_size,sizeof(int));
683     if ( zzinf_line == NULL )
684     {
685         fprintf(stderr, "cannot allocate line buffer (%d ints)\n",
686                 zzinf_token_buffer_size);
687         exit(PCCTS_EXIT_FAILURE);
688 	}
689 
690 	/* get tokens, copying text to text buffer */
691 	zzinf_text_buffer_index = 0;
692 	do {
693 		zzgettok();
694 		line = zzreal_line;
695 		while ( zzinf_lap>=zzinf_token_buffer_size )
696 		{
697 			zzinf_token_buffer_size += ZZINF_BUFFER_TOKEN_CHUNK_SIZE;
698 			zzinf_tokens = (int *) realloc(zzinf_tokens,
699 												 zzinf_token_buffer_size*sizeof(int));
700 			if ( zzinf_tokens == NULL )
701 			{
702 				fprintf(stderr, "cannot allocate lookahead token buffer (%d tokens)\n",
703 						zzinf_token_buffer_size);
704 				exit(PCCTS_EXIT_FAILURE);
705 			}
706             zzinf_line = (int *) realloc(zzinf_line,
707                                          zzinf_token_buffer_size*sizeof(int));
708             if ( zzinf_line == NULL )
709             {
710                 fprintf(stderr, "cannot allocate lookahead line buffer (%d ints)\n",
711                         zzinf_token_buffer_size);
712                 exit(PCCTS_EXIT_FAILURE);
713 			}
714 
715 		}
716 		while ( (zzinf_text_buffer_index+strlen(NLATEXT)+1) >= zzinf_text_buffer_size )
717 		{
718 			zzinf_text_buffer_size += ZZINF_BUFFER_TEXT_CHUNK_SIZE;
719 			zzinf_text_buffer = (char *) realloc(zzinf_text_buffer,
720 												 zzinf_text_buffer_size);
721 			if ( zzinf_text_buffer == NULL )
722 			{
723 				fprintf(stderr,	"cannot allocate lookahead text buffer (%d bytes)\n",
724 						zzinf_text_buffer_size);
725 				exit(PCCTS_EXIT_FAILURE);
726 			}
727 		}
728 		/* record token and text and line of input symbol */
729 		tok = zzinf_tokens[zzinf_lap] = NLA;
730 		strcpy(&zzinf_text_buffer[zzinf_text_buffer_index], NLATEXT);
731 		zzinf_text_buffer_index += strlen(NLATEXT)+1;
732         zzinf_line[zzinf_lap] = line;
733 		zzinf_lap++;
734 	} while (tok!=zzEOF_TOKEN);
735 	zzinf_labase = 0;
736 	zzinf_last = zzinf_lap-1;
737 
738 	/* allocate ptrs to text of ith token */
739 	zzinf_text = (char **) calloc(zzinf_last+1,sizeof(char *));
740 	if ( zzinf_text == NULL )
741 	{
742 		fprintf(stderr,	"cannot allocate lookahead text buffer (%d)\n",
743 				zzinf_text_buffer_size);
744 		exit(PCCTS_EXIT_FAILURE);
745 	}
746 	zzinf_text_buffer_index = 0;
747 	zzinf_lap = 0;
748 	/* set ptrs so that zzinf_text[i] is the text of the ith token found on input */
749 	while (zzinf_lap<=zzinf_last)
750 	{
751 	    zzinf_text[zzinf_lap++] = &zzinf_text_buffer[zzinf_text_buffer_index];
752 		zzinf_text_buffer_index += strlen(&zzinf_text_buffer[zzinf_text_buffer_index])+1;
753 	}
754 }
755 #endif
756 
757 int
758 #ifdef __USE_PROTOS
_zzsetmatch(SetWordType * e,char ** zzBadText,char ** zzMissText,int * zzMissTok,int * zzBadTok,SetWordType ** zzMissSet,SetWordType * zzTokclassErrset)759 _zzsetmatch(SetWordType *e, char **zzBadText, char **zzMissText,
760 			int *zzMissTok, int *zzBadTok,
761 			SetWordType **zzMissSet,
762 			SetWordType *zzTokclassErrset /* MR23 */)
763 #else
764 _zzsetmatch(e, zzBadText, zzMissText, zzMissTok, zzBadTok, zzMissSet, zzTokclassErrset /* MR23 */)
765 SetWordType *e;
766 char **zzBadText;
767 char **zzMissText;
768 int *zzMissTok, *zzBadTok;
769 SetWordType **zzMissSet;
770 SetWordType *zzTokclassErrset;
771 #endif
772 {
773 #ifdef DEMAND_LOOK
774 #ifdef LL_K
775 	if ( zzdirty==LL_K ) {zzCONSUME;}
776 #else
777 	if ( zzdirty ) {zzCONSUME;}
778 #endif
779 #endif
780 	if ( !zzset_el((unsigned)LA(1), e) ) {
781 		*zzBadText = LATEXT(1); *zzMissText=NULL;
782 		*zzMissTok= 0; *zzBadTok=LA(1);
783 		*zzMissSet=zzTokclassErrset; /* MR23 */
784 		return 0;
785 	}
786 	zzMakeAttr           /* MR14 Ger Hobbelt (hobbelt@axa.nl) */
787 #ifdef DEMAND_LOOK
788 #ifdef LL_K
789 	zzdirty++;
790     zzlabase++;          /* MR14 Ger Hobbelt (hobbelt@axa.nl) */
791 #else
792 	zzdirty = 1;
793 #endif
794 #endif
795 	return 1;
796 }
797 
798 int
799 #ifdef __USE_PROTOS
_zzmatch_wdfltsig(int tokenWanted,SetWordType * whatFollows)800 _zzmatch_wdfltsig(int tokenWanted, SetWordType *whatFollows)
801 #else
802 _zzmatch_wdfltsig(tokenWanted, whatFollows)
803 int tokenWanted;
804 SetWordType *whatFollows;
805 #endif
806 {
807 #ifdef DEMAND_LOOK
808 #ifdef LL_K
809 	if ( zzdirty==LL_K ) {
810 			zzCONSUME;
811 	}
812 #else
813 	if ( zzdirty ) {zzCONSUME;}
814 #endif
815 #endif
816 
817 	if ( LA(1)!=tokenWanted )
818 	{
819         zzSyntaxErrCount++;     /* MR11 */
820 		fprintf(stderr,
821 				"line %d: syntax error at \"%s\" missing %s\n",
822 				zzline,
823 				(LA(1)==zzEOF_TOKEN)?"<eof>":(char *)LATEXT(1),
824 				zztokens[tokenWanted]);
825 		zzconsumeUntil( whatFollows );
826 		return 0;
827 	}
828 	else {
829 		zzMakeAttr
830 #ifdef DEMAND_LOOK
831 #ifdef LL_K
832 		zzdirty++;
833 		zzlabase++;
834 #else
835 		zzdirty = 1;
836 #endif
837 #else
838 /*		zzCONSUME;		 consume if not demand lookahead */
839 #endif
840 		return 1;
841 	}
842 }
843 
844 int
845 #ifdef __USE_PROTOS
_zzsetmatch_wdfltsig(SetWordType * tokensWanted,int tokenTypeOfSet,SetWordType * whatFollows)846 _zzsetmatch_wdfltsig(SetWordType *tokensWanted,
847 					 int tokenTypeOfSet,
848 					 SetWordType *whatFollows)
849 #else
850 _zzsetmatch_wdfltsig(tokensWanted, tokenTypeOfSet, whatFollows)
851 SetWordType *tokensWanted;
852 int tokenTypeOfSet;
853 SetWordType *whatFollows;
854 #endif
855 {
856 #ifdef DEMAND_LOOK
857 #ifdef LL_K
858 	if ( zzdirty==LL_K ) {zzCONSUME;}
859 #else
860 	if ( zzdirty ) {zzCONSUME;}
861 #endif
862 #endif
863 	if ( !zzset_el((unsigned)LA(1), tokensWanted) )
864 	{
865         zzSyntaxErrCount++;     /* MR11 */
866 		fprintf(stderr,
867 				"line %d: syntax error at \"%s\" missing %s\n",
868 				zzline,
869 				(LA(1)==zzEOF_TOKEN)?"<eof>":(char *)LATEXT(1),
870 				zztokens[tokenTypeOfSet]);
871 		zzconsumeUntil( whatFollows );
872 		return 0;
873 	}
874 	else {
875 		zzMakeAttr
876 #ifdef DEMAND_LOOK
877 #ifdef LL_K
878 		zzdirty++;
879 		zzlabase++;
880 #else
881 		zzdirty = 1;
882 #endif
883 #else
884 /*		zzCONSUME;		consume if not demand lookahead */
885 #endif
886 		return 1;
887 	}
888 }
889 
890 int
891 #ifdef __USE_PROTOS
_zzsetmatch_wsig(SetWordType * e)892 _zzsetmatch_wsig(SetWordType *e)
893 #else
894 _zzsetmatch_wsig(e)
895 SetWordType *e;
896 #endif
897 {
898 #ifdef DEMAND_LOOK
899 #ifdef LL_K
900 	if ( zzdirty==LL_K ) {zzCONSUME;}
901 #else
902 	if ( zzdirty ) {zzCONSUME;}
903 #endif
904 #endif
905 	if ( !zzset_el((unsigned)LA(1), e) ) return 0;
906 	zzMakeAttr           /* MR14 Ger Hobbelt (hobbelt@axa.nl) */
907 #ifdef DEMAND_LOOK
908 #ifdef LL_K
909 	zzdirty++;
910     zzlabase++;          /* MR14 Ger Hobbelt (hobbelt@axa.nl) */
911 #else
912 	zzdirty = 1;
913 #endif
914 #endif
915 	return 1;
916 }
917 
918 #ifdef USER_ZZMODE_STACK
919 static int  zzmstk[ZZMAXSTK] = { -1 };
920 static int  zzmdep = 0;
921 static char zzmbuf[70];
922 
923 void
924 #ifdef __USE_PROTOS
zzmpush(int m)925 zzmpush( int m )
926 #else
927 zzmpush( m )
928 int m;
929 #endif
930 {
931    if(zzmdep == ZZMAXSTK - 1) {
932      sprintf(zzmbuf, "Mode stack overflow ");
933      zzerr(zzmbuf);
934    } else {
935      zzmstk[zzmdep++] = zzauto;
936      zzmode(m);
937    }
938 }
939 
940 void
941 #ifdef __USE_PROTOS
zzmpop(void)942 zzmpop( void )
943 #else
944 zzmpop( )
945 #endif
946 {
947    if(zzmdep == 0)
948    {  sprintf(zzmbuf, "Mode stack underflow ");
949       zzerr(zzmbuf);
950    }
951    else
952    {  zzmdep--;
953       zzmode(zzmstk[zzmdep]);
954    }
955 }
956 
957 void
958 #ifdef __USE_PROTOS
zzsave_mode_stack(int modeStack[],int * modeLevel)959 zzsave_mode_stack( int modeStack[], int *modeLevel )
960 #else
961 zzsave_mode_stack( modeStack, modeLevel )
962 int modeStack[];
963 int *modeLevel;
964 #endif
965 {
966   int i;
967   memcpy(modeStack, zzmstk, sizeof(zzmstk));
968   *modeLevel = zzmdep;
969   zzmdep = 0;
970 
971   return;
972 }
973 
974 void
975 #ifdef __USE_PROTOS
zzrestore_mode_stack(int modeStack[],int * modeLevel)976 zzrestore_mode_stack( int modeStack[], int *modeLevel )
977 #else
978 zzrestore_mode_stack( modeStack, modeLevel )
979 int modeStack[];
980 int *modeLevel;
981 #endif
982 {
983   int i;
984 
985   memcpy(zzmstk, modeStack, sizeof(zzmstk));
986   zzmdep = *modeLevel;
987 
988   return;
989 }
990 #endif /* USER_ZZMODE_STACK */
991 
992 #ifdef __USE_PROTOS
zzTraceReset(void)993 void zzTraceReset(void)
994 #else
995 void zzTraceReset()
996 #endif
997 {
998 #ifdef zzTRACE_RULES
999   zzTraceOptionValue=zzTraceOptionValueDefault;
1000   zzTraceGuessOptionValue=1;
1001   zzTraceCurrentRuleName=NULL;
1002   zzTraceDepth=0;
1003 #endif
1004 }
1005 
1006 #ifdef __USE_PROTOS
zzTraceGuessFail(void)1007 void zzTraceGuessFail(void)
1008 #else
1009 void zzTraceGuessFail()
1010 #endif
1011 {
1012 
1013 #ifdef zzTRACE_RULES
1014 #ifdef ZZCAN_GUESS
1015 
1016   int   doIt=0;
1017 
1018   if (zzTraceOptionValue <= 0) {
1019     doIt=0;
1020   } else if (zzguessing && zzTraceGuessOptionValue <= 0) {
1021     doIt=0;
1022   } else {
1023     doIt=1;
1024   };
1025 
1026   if (doIt) {
1027     fprintf(stderr,"guess failed\n");
1028   };
1029 #endif
1030 #endif
1031 }
1032 
1033 /* zzTraceOption:
1034      zero value turns off trace
1035 */
1036 
1037 #ifdef __USE_PROTOS
zzTraceIn(char * rule)1038 void zzTraceIn(char * rule)
1039 #else
1040 void zzTraceIn(rule)
1041   char  *rule;
1042 #endif
1043 {
1044 #ifdef zzTRACE_RULES
1045 
1046   int           doIt=0;
1047 
1048   zzTraceDepth++;
1049   zzTraceCurrentRuleName=rule;
1050 
1051   if (zzTraceOptionValue <= 0) {
1052     doIt=0;
1053 #ifdef ZZCAN_GUESS
1054   } else if (zzguessing && zzTraceGuessOptionValue <= 0) {
1055     doIt=0;
1056 #endif
1057   } else {
1058     doIt=1;
1059   };
1060 
1061   if (doIt) {
1062     fprintf(stderr,"enter rule %s {\"%s\"} depth %d",
1063             rule,
1064             LA(1)==1 ? "@" : (char *) LATEXT(1),    /* MR19 */
1065             zzTraceDepth);
1066 #ifdef ZZCAN_GUESS
1067     if (zzguessing) fprintf(stderr," guessing");
1068 #endif
1069     fprintf(stderr,"\n");
1070   };
1071 #endif
1072   return;
1073 }
1074 
1075 #ifdef __USE_PROTOS
zzTraceOut(char * rule)1076 void zzTraceOut(char * rule)
1077 #else
1078 void zzTraceOut(rule)
1079   char  *rule;
1080 #endif
1081 {
1082 #ifdef zzTRACE_RULES
1083   int       doIt=0;
1084 
1085   zzTraceDepth--;
1086 
1087   if (zzTraceOptionValue <= 0) {
1088     doIt=0;
1089 #ifdef ZZCAN_GUESS
1090   } else if (zzguessing && zzTraceGuessOptionValue <= 0) {
1091     doIt=0;
1092 #endif
1093   } else {
1094     doIt=1;
1095   };
1096 
1097   if (doIt) {
1098     fprintf(stderr,"exit rule %s {\"%s\"} depth %d",
1099             rule,
1100             LA(1)==1 ? "@" : (char *) LATEXT(1), /* MR19 */
1101             zzTraceDepth+1);
1102 #ifdef ZZCAN_GUESS
1103     if (zzguessing) fprintf(stderr," guessing");
1104 #endif
1105     fprintf(stderr,"\n");
1106   };
1107 #endif
1108 }
1109 
1110 #ifdef __USE_PROTOS
zzTraceOption(int delta)1111 int zzTraceOption(int delta)
1112 #else
1113 int zzTraceOption(delta)
1114   int   delta;
1115 #endif
1116 {
1117 #ifdef zzTRACE_RULES
1118     int     prevValue=zzTraceOptionValue;
1119 
1120     zzTraceOptionValue=zzTraceOptionValue+delta;
1121 
1122     if (zzTraceCurrentRuleName != NULL) {
1123       if (prevValue <= 0 && zzTraceOptionValue > 0) {
1124         fprintf(stderr,"trace enabled in rule %s depth %d\n",
1125                                             zzTraceCurrentRuleName,zzTraceDepth);
1126       };
1127       if (prevValue > 0 && zzTraceOptionValue <= 0) {
1128         fprintf(stderr,"trace disabled in rule %s depth %d\n",
1129                                             zzTraceCurrentRuleName,zzTraceDepth);
1130       };
1131     };
1132     return  prevValue;
1133 #else
1134     return 0;
1135 #endif
1136 }
1137 
1138 #ifdef __USE_PROTOS
zzTraceGuessOption(int delta)1139 int zzTraceGuessOption(int delta)
1140 #else
1141 int zzTraceGuessOption(delta)
1142   int   delta;
1143 #endif
1144 {
1145 #ifdef zzTRACE_RULES
1146 #ifdef ZZCAN_GUESS
1147     int     prevValue=zzTraceGuessOptionValue;
1148 
1149     zzTraceGuessOptionValue=zzTraceGuessOptionValue+delta;
1150 
1151     if (zzTraceCurrentRuleName != NULL) {
1152       if (prevValue <= 0 && zzTraceGuessOptionValue > 0) {
1153         fprintf(stderr,"guess trace enabled in rule %s depth %d\n",
1154                                                 zzTraceCurrentRuleName,zzTraceDepth);
1155       };
1156       if (prevValue > 0 && zzTraceGuessOptionValue <= 0) {
1157         fprintf(stderr,"guess trace disabled in rule %s depth %d\n",
1158                                                 zzTraceCurrentRuleName,zzTraceDepth);
1159       };
1160     };
1161     return prevValue;
1162 #else
1163     return 0;
1164 #endif
1165 #else
1166     return 0;
1167 #endif
1168 }
1169 
1170 #endif /* ERR_H */
1171