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-1998
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 */
75static SetWordType bitmask[] = {
76  0x00000001, 0x00000002, 0x00000004, 0x00000008,
77  0x00000010, 0x00000020, 0x00000040, 0x00000080
78};
79
80#ifdef zzTRACE_RULES
81int  zzTraceOptionValueDefault=1;
82int  zzTraceOptionValue=1;
83int  zzTraceGuessOptionValue=1;
84char *zzTraceCurrentRuleName=NULL;
85int  zzTraceDepth=0;
86#endif
87
88int  zzGuessSeq=0;          /* MR10 */
89int  zzSyntaxErrCount=0;    /* MR11 */
90int  zzLexErrCount=0;       /* MR11 */
91
92void
93#ifdef __USE_PROTOS
94zzresynch(SetWordType *wd,SetWordType mask)
95#else
96zzresynch(wd,mask)
97SetWordType *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
120void
121#ifdef __USE_PROTOS
122zzconsumeUntil(SetWordType *st)
123#else
124zzconsumeUntil(st)
125SetWordType *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
138void
139#ifdef __USE_PROTOS
140zzconsumeUntilToken(int t)
141#else
142zzconsumeUntilToken(t)
143int 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
157void zzFAIL(int k, ...)
158#else
159void zzFAIL(va_alist)
160va_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
226void zzTraceGuessDone(zzantlr_state *state)
227#else
228void 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
263void
264#ifdef __USE_PROTOS
265zzsave_antlr_state(zzantlr_state *buf)
266#else
267zzsave_antlr_state(buf)
268zzantlr_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
319void
320#ifdef __USE_PROTOS
321zzrestore_antlr_state(zzantlr_state *buf)
322#else
323zzrestore_antlr_state(buf)
324zzantlr_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
392void
393#ifdef __USE_PROTOS
394zzedecode(SetWordType *a)
395#else
396zzedecode(a)
397SetWordType *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 */
418void
419#ifdef __USE_PROTOS
420zzsyn(char *text, int tok, char *egroup, SetWordType *eset, int etok, int k, char *bad_text)
421#else
422zzsyn(text, tok, egroup, eset, etok, k, bad_text)
423char *text, *egroup, *bad_text;
424int tok;
425int etok;
426int k;
427SetWordType *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? */
448int
449#ifdef __USE_PROTOS
450zzset_el(unsigned b, SetWordType *p)
451#else
452zzset_el(b,p)
453unsigned b;
454SetWordType *p;
455#endif
456{
457  return( p[BSETDIVWORD(b)] & bitmask[BSETMODWORD(b)] );
458}
459
460int
461#ifdef __USE_PROTOS
462zzset_deg(SetWordType *a)
463#else
464zzset_deg(a)
465SetWordType *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
493int
494#ifdef __USE_PROTOS
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)
500int _t;
501char **zzBadText;
502char **zzMissText;
503int *zzMissTok, *zzBadTok;
504SetWordType **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
522int
523#ifdef __USE_PROTOS
524_zzmatch_wsig(int _t)
525#else
526_zzmatch_wsig(_t)
527int _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
544int
545#ifdef __USE_PROTOS
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)
550int _t;
551char **zzBadText;
552char **zzMissText;
553int *zzMissTok, *zzBadTok;
554SetWordType **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
569int
570#ifdef __USE_PROTOS
571_zzmatch_wsig(int _t)
572#else
573_zzmatch_wsig(_t)
574int _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
590int
591#ifdef __USE_PROTOS
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)
597int _t;
598char **zzBadText;
599char **zzMissText;
600int *zzMissTok, *zzBadTok;
601SetWordType **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
614int
615#ifdef __USE_PROTOS
616_zzmatch_wsig(int _t)
617#else
618_zzmatch_wsig(_t)
619int _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
630void
631#ifdef __USE_PROTOS
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 */
654void
655#ifdef __USE_PROTOS
656zzfill_inf_look(void)
657#else
658zzfill_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
757int
758#ifdef __USE_PROTOS
759_zzsetmatch(SetWordType *e, char **zzBadText, char **zzMissText,
760      int *zzMissTok, int *zzBadTok,
761      SetWordType **zzMissSet)
762#else
763_zzsetmatch(e, zzBadText, zzMissText, zzMissTok, zzBadTok, zzMissSet)
764SetWordType *e;
765char **zzBadText;
766char **zzMissText;
767int *zzMissTok, *zzBadTok;
768SetWordType **zzMissSet;
769#endif
770{
771#ifdef DEMAND_LOOK
772#ifdef LL_K
773  if ( zzdirty==LL_K ) {zzCONSUME;}
774#else
775  if ( zzdirty ) {zzCONSUME;}
776#endif
777#endif
778  if ( !zzset_el((unsigned)LA(1), e) ) {
779    *zzBadText = LATEXT(1); *zzMissText=NULL;
780    *zzMissTok= 0; *zzBadTok=LA(1);
781    *zzMissSet=e;
782    return 0;
783  }
784  zzMakeAttr           /* MR14 Ger Hobbelt (hobbelt@axa.nl) */
785#ifdef DEMAND_LOOK
786#ifdef LL_K
787  zzdirty++;
788    zzlabase++;          /* MR14 Ger Hobbelt (hobbelt@axa.nl) */
789#else
790  zzdirty = 1;
791#endif
792#endif
793  return 1;
794}
795
796int
797#ifdef __USE_PROTOS
798_zzmatch_wdfltsig(int tokenWanted, SetWordType *whatFollows)
799#else
800_zzmatch_wdfltsig(tokenWanted, whatFollows)
801int tokenWanted;
802SetWordType *whatFollows;
803#endif
804{
805#ifdef DEMAND_LOOK
806#ifdef LL_K
807  if ( zzdirty==LL_K ) {
808      zzCONSUME;
809  }
810#else
811  if ( zzdirty ) {zzCONSUME;}
812#endif
813#endif
814
815  if ( LA(1)!=tokenWanted )
816  {
817        zzSyntaxErrCount++;     /* MR11 */
818    fprintf(stderr,
819        "line %d: syntax error at \"%s\" missing %s\n",
820        zzline,
821        (LA(1)==zzEOF_TOKEN)?"<eof>":(char *)LATEXT(1),
822        zztokens[tokenWanted]);
823    zzconsumeUntil( whatFollows );
824    return 0;
825  }
826  else {
827    zzMakeAttr
828#ifdef DEMAND_LOOK
829#ifdef LL_K
830    zzdirty++;
831    zzlabase++;
832#else
833    zzdirty = 1;
834#endif
835#else
836/*    zzCONSUME;     consume if not demand lookahead */
837#endif
838    return 1;
839  }
840}
841
842int
843#ifdef __USE_PROTOS
844_zzsetmatch_wdfltsig(SetWordType *tokensWanted,
845           int tokenTypeOfSet,
846           SetWordType *whatFollows)
847#else
848_zzsetmatch_wdfltsig(tokensWanted, tokenTypeOfSet, whatFollows)
849SetWordType *tokensWanted;
850int tokenTypeOfSet;
851SetWordType *whatFollows;
852#endif
853{
854#ifdef DEMAND_LOOK
855#ifdef LL_K
856  if ( zzdirty==LL_K ) {zzCONSUME;}
857#else
858  if ( zzdirty ) {zzCONSUME;}
859#endif
860#endif
861  if ( !zzset_el((unsigned)LA(1), tokensWanted) )
862  {
863        zzSyntaxErrCount++;     /* MR11 */
864    fprintf(stderr,
865        "line %d: syntax error at \"%s\" missing %s\n",
866        zzline,
867        (LA(1)==zzEOF_TOKEN)?"<eof>":(char *)LATEXT(1),
868        zztokens[tokenTypeOfSet]);
869    zzconsumeUntil( whatFollows );
870    return 0;
871  }
872  else {
873    zzMakeAttr
874#ifdef DEMAND_LOOK
875#ifdef LL_K
876    zzdirty++;
877    zzlabase++;
878#else
879    zzdirty = 1;
880#endif
881#else
882/*    zzCONSUME;    consume if not demand lookahead */
883#endif
884    return 1;
885  }
886}
887
888int
889#ifdef __USE_PROTOS
890_zzsetmatch_wsig(SetWordType *e)
891#else
892_zzsetmatch_wsig(e)
893SetWordType *e;
894#endif
895{
896#ifdef DEMAND_LOOK
897#ifdef LL_K
898  if ( zzdirty==LL_K ) {zzCONSUME;}
899#else
900  if ( zzdirty ) {zzCONSUME;}
901#endif
902#endif
903  if ( !zzset_el((unsigned)LA(1), e) ) return 0;
904  zzMakeAttr           /* MR14 Ger Hobbelt (hobbelt@axa.nl) */
905#ifdef DEMAND_LOOK
906#ifdef LL_K
907  zzdirty++;
908    zzlabase++;          /* MR14 Ger Hobbelt (hobbelt@axa.nl) */
909#else
910  zzdirty = 1;
911#endif
912#endif
913  return 1;
914}
915
916#ifdef USER_ZZMODE_STACK
917static int  zzmstk[ZZMAXSTK] = { -1 };
918static int  zzmdep = 0;
919static char zzmbuf[70];
920
921void
922#ifdef __USE_PROTOS
923zzmpush( int m )
924#else
925zzmpush( m )
926int m;
927#endif
928{
929   if(zzmdep == ZZMAXSTK - 1) {
930     sprintf(zzmbuf, "Mode stack overflow ");
931     zzerr(zzmbuf);
932   } else {
933     zzmstk[zzmdep++] = zzauto;
934     zzmode(m);
935   }
936}
937
938void
939#ifdef __USE_PROTOS
940zzmpop( void )
941#else
942zzmpop( )
943#endif
944{
945   if(zzmdep == 0)
946   {  sprintf(zzmbuf, "Mode stack underflow ");
947      zzerr(zzmbuf);
948   }
949   else
950   {  zzmdep--;
951      zzmode(zzmstk[zzmdep]);
952   }
953}
954
955void
956#ifdef __USE_PROTOS
957zzsave_mode_stack( int modeStack[], int *modeLevel )
958#else
959zzsave_mode_stack( modeStack, modeLevel )
960int modeStack[];
961int *modeLevel;
962#endif
963{
964  int i;
965  memcpy(modeStack, zzmstk, sizeof(zzmstk));
966  *modeLevel = zzmdep;
967  zzmdep = 0;
968
969  return;
970}
971
972void
973#ifdef __USE_PROTOS
974zzrestore_mode_stack( int modeStack[], int *modeLevel )
975#else
976zzrestore_mode_stack( modeStack, modeLevel )
977int modeStack[];
978int *modeLevel;
979#endif
980{
981  int i;
982
983  memcpy(zzmstk, modeStack, sizeof(zzmstk));
984  zzmdep = *modeLevel;
985
986  return;
987}
988#endif /* USER_ZZMODE_STACK */
989
990#ifdef __USE_PROTOS
991void zzTraceReset(void)
992#else
993void zzTraceReset()
994#endif
995{
996#ifdef zzTRACE_RULES
997  zzTraceOptionValue=zzTraceOptionValueDefault;
998  zzTraceGuessOptionValue=1;
999  zzTraceCurrentRuleName=NULL;
1000  zzTraceDepth=0;
1001#endif
1002}
1003
1004#ifdef __USE_PROTOS
1005void zzTraceGuessFail(void)
1006#else
1007void zzTraceGuessFail()
1008#endif
1009{
1010
1011#ifdef zzTRACE_RULES
1012#ifdef ZZCAN_GUESS
1013
1014  int   doIt=0;
1015
1016  if (zzTraceOptionValue <= 0) {
1017    doIt=0;
1018  } else if (zzguessing && zzTraceGuessOptionValue <= 0) {
1019    doIt=0;
1020  } else {
1021    doIt=1;
1022  };
1023
1024  if (doIt) {
1025    fprintf(stderr,"guess failed\n");
1026  };
1027#endif
1028#endif
1029}
1030
1031/* zzTraceOption:
1032     zero value turns off trace
1033*/
1034
1035#ifdef __USE_PROTOS
1036void zzTraceIn(char * rule)
1037#else
1038void zzTraceIn(rule)
1039  char  *rule;
1040#endif
1041{
1042#ifdef zzTRACE_RULES
1043
1044  int           doIt=0;
1045
1046  zzTraceDepth++;
1047  zzTraceCurrentRuleName=rule;
1048
1049  if (zzTraceOptionValue <= 0) {
1050    doIt=0;
1051#ifdef ZZCAN_GUESS
1052  } else if (zzguessing && zzTraceGuessOptionValue <= 0) {
1053    doIt=0;
1054#endif
1055  } else {
1056    doIt=1;
1057  };
1058
1059  if (doIt) {
1060    fprintf(stderr,"enter rule %s {\"%s\"} depth %d",
1061            rule,
1062            LA(1)==1 ? "@" : (char *) LATEXT(1),    /* MR19 */
1063            zzTraceDepth);
1064#ifdef ZZCAN_GUESS
1065    if (zzguessing) fprintf(stderr," guessing");
1066#endif
1067    fprintf(stderr,"\n");
1068  };
1069#endif
1070  return;
1071}
1072
1073#ifdef __USE_PROTOS
1074void zzTraceOut(char * rule)
1075#else
1076void zzTraceOut(rule)
1077  char  *rule;
1078#endif
1079{
1080#ifdef zzTRACE_RULES
1081  int       doIt=0;
1082
1083  zzTraceDepth--;
1084
1085  if (zzTraceOptionValue <= 0) {
1086    doIt=0;
1087#ifdef ZZCAN_GUESS
1088  } else if (zzguessing && zzTraceGuessOptionValue <= 0) {
1089    doIt=0;
1090#endif
1091  } else {
1092    doIt=1;
1093  };
1094
1095  if (doIt) {
1096    fprintf(stderr,"exit rule %s {\"%s\"} depth %d",
1097            rule,
1098            LA(1)==1 ? "@" : (char *) LATEXT(1), /* MR19 */
1099            zzTraceDepth+1);
1100#ifdef ZZCAN_GUESS
1101    if (zzguessing) fprintf(stderr," guessing");
1102#endif
1103    fprintf(stderr,"\n");
1104  };
1105#endif
1106}
1107
1108#ifdef __USE_PROTOS
1109int zzTraceOption(int delta)
1110#else
1111int zzTraceOption(delta)
1112  int   delta;
1113#endif
1114{
1115#ifdef zzTRACE_RULES
1116    int     prevValue=zzTraceOptionValue;
1117
1118    zzTraceOptionValue=zzTraceOptionValue+delta;
1119
1120    if (zzTraceCurrentRuleName != NULL) {
1121      if (prevValue <= 0 && zzTraceOptionValue > 0) {
1122        fprintf(stderr,"trace enabled in rule %s depth %d\n",
1123                                            zzTraceCurrentRuleName,zzTraceDepth);
1124      };
1125      if (prevValue > 0 && zzTraceOptionValue <= 0) {
1126        fprintf(stderr,"trace disabled in rule %s depth %d\n",
1127                                            zzTraceCurrentRuleName,zzTraceDepth);
1128      };
1129    };
1130    return  prevValue;
1131#else
1132    return 0;
1133#endif
1134}
1135
1136#ifdef __USE_PROTOS
1137int zzTraceGuessOption(int delta)
1138#else
1139int zzTraceGuessOption(delta)
1140  int   delta;
1141#endif
1142{
1143#ifdef zzTRACE_RULES
1144#ifdef ZZCAN_GUESS
1145    int     prevValue=zzTraceGuessOptionValue;
1146
1147    zzTraceGuessOptionValue=zzTraceGuessOptionValue+delta;
1148
1149    if (zzTraceCurrentRuleName != NULL) {
1150      if (prevValue <= 0 && zzTraceGuessOptionValue > 0) {
1151        fprintf(stderr,"guess trace enabled in rule %s depth %d\n",
1152                                                zzTraceCurrentRuleName,zzTraceDepth);
1153      };
1154      if (prevValue > 0 && zzTraceGuessOptionValue <= 0) {
1155        fprintf(stderr,"guess trace disabled in rule %s depth %d\n",
1156                                                zzTraceCurrentRuleName,zzTraceDepth);
1157      };
1158    };
1159    return prevValue;
1160#else
1161    return 0;
1162#endif
1163#else
1164    return 0;
1165#endif
1166}
1167
1168#endif /* ERR_H */
1169