File indexing completed on 2025-01-26 04:43:59

0001 /* dlgauto.h automaton
0002  *
0003  * SOFTWARE RIGHTS
0004  *
0005  * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
0006  * Set (PCCTS) -- PCCTS is in the public domain.  An individual or
0007  * company may do whatever they wish with source code distributed with
0008  * PCCTS or the code generated by PCCTS, including the incorporation of
0009  * PCCTS, or its output, into commercial software.
0010  *
0011  * We encourage users to develop software with PCCTS.  However, we do ask
0012  * that credit is given to us for developing PCCTS.  By "credit",
0013  * we mean that if you incorporate our source code into one of your
0014  * programs (commercial product, research project, or otherwise) that you
0015  * acknowledge this fact somewhere in the documentation, research report,
0016  * etc...  If you like PCCTS and have developed a nice tool with the
0017  * output, please mention that you developed it using PCCTS.  In
0018  * addition, we ask that this header remain intact in our source code.
0019  * As long as these guidelines are kept, we expect to continue enhancing
0020  * this system and expect to make other tools available as they are
0021  * completed.
0022  *
0023  * ANTLR 1.33
0024  * Will Cohen and Terence Parr
0025  * Parr Research Corporation
0026  * with Purdue University and AHPCRC, University of Minnesota
0027  * 1989-1995
0028  */
0029 
0030 #ifndef DLGAUTO_H
0031 #define DLGAUTO_H
0032 
0033 zzchar_t    *zzlextext; /* text of most recently matched token */
0034 zzchar_t    *zzbegexpr; /* beginning of last reg expr recogn. */
0035 zzchar_t    *zzendexpr; /* beginning of last reg expr recogn. */
0036 int zzbufsize;  /* number of characters in zzlextext */
0037 int zzbegcol = 0;   /* column that first character of token is in*/
0038 int zzendcol = 0;   /* column that last character of token is in */
0039 int zzline = 1; /* line current token is on */
0040 int zzreal_line=1;  /* line of 1st portion of token that is not skipped */
0041 int zzchar;     /* character to determine next state */
0042 int zzbufovf;   /* indicates that buffer too small for text */
0043 int zzcharfull = 0;
0044 static zzchar_t *zznextpos;/* points to next available position in zzlextext*/
0045 static int  zzclass;
0046 
0047 void    zzerrstd(const char *);
0048 void    (*zzerr)(const char *)=zzerrstd;/* pointer to error reporting function */
0049 extern int  zzerr_in(void);
0050 
0051 static FILE *zzstream_in=0;
0052 static int  (*zzfunc_in)() = zzerr_in;
0053 static zzchar_t *zzstr_in=0;
0054 
0055 #ifdef USER_ZZMODE_STACK
0056 int               zzauto = 0;
0057 #else
0058 static int     zzauto = 0;
0059 #endif
0060 static int  zzadd_erase;
0061 static char     zzebuf[70];
0062 
0063 #ifdef ZZCOL
0064 #define ZZINC (++zzendcol)
0065 #else
0066 #define ZZINC
0067 #endif
0068 
0069 
0070 #define ZZGETC_STREAM {zzchar = getc(zzstream_in); zzclass = ZZSHIFT(zzchar);}
0071 #define ZZGETC_FUNC {zzchar = (*zzfunc_in)(); zzclass = ZZSHIFT(zzchar);}
0072 #define ZZGETC_STR {            \
0073     if (*zzstr_in){             \
0074         zzchar = *zzstr_in;     \
0075         ++zzstr_in;             \
0076     }else{                      \
0077         zzchar = EOF;           \
0078     }                           \
0079     zzclass = ZZSHIFT(zzchar);  \
0080 }
0081 
0082 #define ZZNEWSTATE  (newstate = dfa[state][zzclass])
0083 
0084 #ifndef ZZCOPY
0085 #define ZZCOPY  \
0086     /* Truncate matching buffer to size (not an error) */   \
0087     if (zznextpos < lastpos){               \
0088         *(zznextpos++) = zzchar;            \
0089     }else{                          \
0090         zzbufovf = 1;                   \
0091     }
0092 #endif
0093 
0094 void
0095 zzrdstream( FILE *f )
0096 {
0097     /* make sure that it is really set to something, otherwise just
0098        leave it be.
0099     */
0100     if (f){
0101         /* make sure that there is always someplace to get input
0102            before closing zzstream_in
0103         */
0104         zzline = 1;
0105         zzstream_in = f;
0106         zzfunc_in = NULL;
0107         zzstr_in = 0;
0108         zzcharfull = 0;
0109     }
0110 }
0111 
0112 void
0113 zzrdfunc( int (*f)() )
0114 {
0115     /* make sure that it is really set to something, otherwise just
0116        leave it be.
0117     */
0118     if (f){
0119         /* make sure that there is always someplace to get input
0120            before closing zzstream_in
0121         */
0122         zzline = 1;
0123         zzstream_in = NULL;
0124         zzfunc_in = f;
0125         zzstr_in = 0;
0126         zzcharfull = 0;
0127     }
0128 }
0129 
0130 
0131 void
0132 zzrdstr( zzchar_t *s )
0133 {
0134     /* make sure that it is really set to something, otherwise just
0135        leave it be.
0136     */
0137     if (s){
0138         /* make sure that there is always someplace to get input
0139            before closing zzstream_in
0140         */
0141         zzline = 1;
0142         zzstream_in = NULL;
0143         zzfunc_in = 0;
0144         zzstr_in = s;
0145         zzcharfull = 0;
0146     }
0147 }
0148 
0149 
0150 void
0151 zzclose_stream()
0152 {
0153 }
0154 
0155 /* saves dlg state, but not what feeds dlg (such as file position) */
0156 void
0157 zzsave_dlg_state(struct zzdlg_state *state)
0158 {
0159     state->stream = zzstream_in;
0160     state->func_ptr = zzfunc_in;
0161     state->str = zzstr_in;
0162     state->auto_num = zzauto;
0163     state->add_erase = zzadd_erase;
0164     state->lookc = zzchar;
0165     state->char_full = zzcharfull;
0166     state->begcol = zzbegcol;
0167     state->endcol = zzendcol;
0168     state->line = zzline;
0169     state->lextext = zzlextext;
0170     state->begexpr = zzbegexpr;
0171     state->endexpr = zzendexpr;
0172     state->bufsize = zzbufsize;
0173     state->bufovf = zzbufovf;
0174     state->nextpos = zznextpos;
0175     state->class_num = zzclass;
0176 }
0177 
0178 void
0179 zzrestore_dlg_state(struct zzdlg_state *state)
0180 {
0181     zzstream_in = state->stream;
0182     zzfunc_in = state->func_ptr;
0183     zzstr_in = state->str;
0184     zzauto = state->auto_num;
0185     zzadd_erase = state->add_erase;
0186     zzchar = state->lookc;
0187     zzcharfull = state->char_full;
0188     zzbegcol = state->begcol;
0189     zzendcol = state->endcol;
0190     zzline = state->line;
0191     zzlextext = state->lextext;
0192     zzbegexpr = state->begexpr;
0193     zzendexpr = state->endexpr;
0194     zzbufsize = state->bufsize;
0195     zzbufovf = state->bufovf;
0196     zznextpos = state->nextpos;
0197     zzclass = state->class_num;
0198 }
0199 
0200 void
0201 zzmode( int m )
0202 {
0203     /* points to base of dfa table */
0204     if (m<MAX_MODE){
0205         zzauto = m;
0206         /* have to redo class since using different compression */
0207         zzclass = ZZSHIFT(zzchar);
0208     }else{
0209         sprintf(zzebuf,"Invalid automaton mode = %d ",m);
0210         zzerr(zzebuf);
0211     }
0212 }
0213 
0214 /* erase what is currently in the buffer, and get a new reg. expr */
0215 void
0216 zzskip()
0217 {
0218     zzadd_erase = 1;
0219 }
0220 
0221 /* don't erase what is in the zzlextext buffer, add on to it */
0222 void
0223 zzmore()
0224 {
0225     zzadd_erase = 2;
0226 }
0227 
0228 /* substitute c for the reg. expr last matched and is in the buffer */
0229 void
0230 zzreplchar(zzchar_t c)
0231 {
0232     /* can't allow overwriting null at end of string */
0233     if (zzbegexpr < &zzlextext[zzbufsize-1]){
0234         *zzbegexpr = c;
0235         *(zzbegexpr+1) = '\0';
0236     }
0237     zzendexpr = zzbegexpr;
0238     zznextpos = zzbegexpr + 1;
0239 }
0240 
0241 /* replace the string s for the reg. expr last matched and in the buffer */
0242 void
0243 zzreplstr(register zzchar_t *s)
0244 {
0245     register zzchar_t *l= &zzlextext[zzbufsize -1];
0246 
0247     zznextpos = zzbegexpr;
0248     if (s){
0249         while ((zznextpos <= l) && (*(zznextpos++) = *(s++))!=0){
0250             /* empty */
0251         }
0252         /* correct for NULL at end of string */
0253         zznextpos--;
0254     }
0255     if ((zznextpos <= l) && (*(--s) == 0)){
0256         zzbufovf = 0;
0257     }else{
0258         zzbufovf = 1;
0259     }
0260     *(zznextpos) = '\0';
0261     zzendexpr = zznextpos - 1;
0262 }
0263 
0264 void
0265 zzgettok()
0266 {
0267     register int state, newstate;
0268     /* last space reserved for the null char */
0269     zzchar_t *lastpos;           /* GPW 1997/09/05 (removed 'register' */
0270 
0271 skip:
0272     zzreal_line = zzline;
0273     zzbufovf = 0;
0274     lastpos = &zzlextext[zzbufsize-1];
0275     zznextpos = zzlextext;
0276     zzbegcol = zzendcol+1;
0277 more:
0278     zzbegexpr = zznextpos;
0279 #ifdef ZZINTERACTIVE
0280     /* interactive version of automaton */
0281     /* if there is something in zzchar, process it */
0282     state = newstate = dfa_base[zzauto];
0283     if (zzcharfull){
0284         ZZINC;
0285         ZZCOPY;
0286         ZZNEWSTATE;
0287     }
0288     if (zzstr_in)
0289         while (zzalternatives[newstate]){
0290             state = newstate;
0291             ZZGETC_STR;
0292             ZZINC;
0293             ZZCOPY;
0294             ZZNEWSTATE;
0295         }
0296     else if (zzstream_in)
0297         while (zzalternatives[newstate]){
0298             state = newstate;
0299             ZZGETC_STREAM;
0300             ZZINC;
0301             ZZCOPY;
0302             ZZNEWSTATE;
0303         }
0304     else if (zzfunc_in)
0305         while (zzalternatives[newstate]){
0306             state = newstate;
0307             ZZGETC_FUNC;
0308             ZZINC;
0309             ZZCOPY;
0310             ZZNEWSTATE;
0311         }
0312     /* figure out if last character really part of token */
0313     if ((state != dfa_base[zzauto]) && (newstate == DfaStates)){
0314         zzcharfull = 1;
0315         --zznextpos;
0316     }else{
0317         zzcharfull = 0;
0318         state = newstate;
0319     }
0320     *(zznextpos) = '\0';
0321     /* Able to transition out of start state to some non err state?*/
0322     if ( state == dfa_base[zzauto] ){
0323         /* make sure doesn't get stuck */
0324         zzadvance();
0325     }
0326 #else
0327     /* non-interactive version of automaton */
0328     if (!zzcharfull)
0329         zzadvance();
0330     else
0331         ZZINC;
0332     state = dfa_base[zzauto];
0333     if (zzstr_in)
0334         while (ZZNEWSTATE != DfaStates){
0335             state = newstate;
0336             ZZCOPY;
0337             ZZGETC_STR;
0338             ZZINC;
0339         }
0340     else if (zzstream_in)
0341         while (ZZNEWSTATE != DfaStates){
0342             state = newstate;
0343             ZZCOPY;
0344             ZZGETC_STREAM;
0345             ZZINC;
0346         }
0347     else if (zzfunc_in)
0348         while (ZZNEWSTATE != DfaStates){
0349             state = newstate;
0350             ZZCOPY;
0351             ZZGETC_FUNC;
0352             ZZINC;
0353         }
0354     zzcharfull = 1;
0355     if ( state == dfa_base[zzauto] ){
0356         if (zznextpos < lastpos){
0357             *(zznextpos++) = zzchar;
0358         }else{
0359             zzbufovf = 1;
0360         }
0361         *zznextpos = '\0';
0362         /* make sure doesn't get stuck */
0363         zzadvance();
0364     }else{
0365         *zznextpos = '\0';
0366     }
0367 #endif
0368 #ifdef ZZCOL
0369     zzendcol -= zzcharfull;
0370 #endif
0371     zzendexpr = zznextpos -1;
0372     zzadd_erase = 0;
0373     (*actions[accepts[state]])();
0374     switch (zzadd_erase) {
0375         case 1: goto skip;
0376         case 2: goto more;
0377     }
0378 }
0379 
0380 void
0381 zzadvance()
0382 {
0383     if (zzstream_in) { ZZGETC_STREAM; zzcharfull = 1; ZZINC;}
0384     if (zzfunc_in) { ZZGETC_FUNC; zzcharfull = 1; ZZINC;}
0385     if (zzstr_in) { ZZGETC_STR; zzcharfull = 1; ZZINC;}
0386     if (!(zzstream_in || zzfunc_in || zzstr_in)){
0387         zzerr_in();
0388     }
0389 }
0390 
0391 void
0392 zzerrstd(const char *s)
0393 {
0394         fprintf(stderr,
0395                 "%s near line %d (text was '%s')\n",
0396                 ((s == NULL) ? "Lexical error" : s),
0397                 zzline,zzlextext);
0398 }
0399 
0400 int
0401 zzerr_in()
0402 {
0403     fprintf(stderr,"No input stream, function, or string\n");
0404     /* return eof to get out gracefully */
0405     return EOF;
0406 }
0407 
0408 #endif