Home | History | Annotate | Download | only in h
      1 /* dlgauto.h automaton
      2  *
      3  * SOFTWARE RIGHTS
      4  *
      5  * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
      6  * Set (PCCTS) -- PCCTS is in the public domain.  An individual or
      7  * company may do whatever they wish with source code distributed with
      8  * PCCTS or the code generated by PCCTS, including the incorporation of
      9  * PCCTS, or its output, into commerical software.
     10  *
     11  * We encourage users to develop software with PCCTS.  However, we do ask
     12  * that credit is given to us for developing PCCTS.  By "credit",
     13  * we mean that if you incorporate our source code into one of your
     14  * programs (commercial product, research project, or otherwise) that you
     15  * acknowledge this fact somewhere in the documentation, research report,
     16  * etc...  If you like PCCTS and have developed a nice tool with the
     17  * output, please mention that you developed it using PCCTS.  In
     18  * addition, we ask that this header remain intact in our source code.
     19  * As long as these guidelines are kept, we expect to continue enhancing
     20  * this system and expect to make other tools available as they are
     21  * completed.
     22  *
     23  * ANTLR 1.33
     24  * Will Cohen and Terence Parr
     25  * Parr Research Corporation
     26  * with Purdue University and AHPCRC, University of Minnesota
     27  * 1989-1998
     28  */
     29 
     30 #ifndef ZZDEFAUTO_H
     31 #define ZZDEFAUTO_H
     32 
     33 /*  10-Apr-97 133MR1  Uses __USE_PROTOS show should #include pcctscfg.h */
     34 
     35 #include "pcctscfg.h"
     36 
     37 zzchar_t  *zzlextext;  /* text of most recently matched token */
     38 zzchar_t  *zzbegexpr;  /* beginning of last reg expr recogn. */
     39 zzchar_t  *zzendexpr;  /* beginning of last reg expr recogn. */
     40 int  zzbufsize = 0;  /* number of characters in zzlextext */          /* MR7 */
     41 int  zzbegcol = 0;  /* column that first character of token is in*/
     42 int  zzendcol = 0;  /* column that last character of token is in */
     43 int  zzline = 1;  /* line current token is on */
     44 int  zzreal_line=1;  /* line of 1st portion of token that is not skipped */
     45 int  zzchar;    /* character to determine next state */
     46 int  zzbufovf;  /* indicates that buffer too small for text */
     47 int  zzcharfull = 0;
     48 static zzchar_t  *zznextpos;/* points to next available position in zzlextext*/
     49 static int   zzclass;
     50 
     51 #ifdef __USE_PROTOS
     52 void  zzerrstd(const char *);
     53 void  (*zzerr)(const char *)=zzerrstd;/* pointer to error reporting function */
     54 extern int  zzerr_in(void);
     55 static int  (*zzfunc_in)(void) = zzerr_in;  /* MR20 */
     56 #else
     57 void  zzerrstd();
     58 void  (*zzerr)()=zzerrstd;  /* pointer to error reporting function */
     59 extern int  zzerr_in();
     60 static int  (*zzfunc_in)() = zzerr_in;      /* MR20 */
     61 #endif
     62 
     63 static FILE  *zzstream_in=0;
     64 static zzchar_t  *zzstr_in=0;
     65 
     66 #ifdef USER_ZZMODE_STACK
     67 int             zzauto = 0;
     68 #else
     69 static int     zzauto = 0;
     70 #endif
     71 static int  zzadd_erase;
     72 static char   zzebuf[70];
     73 
     74 #ifdef ZZCOL
     75 #define ZZINC (++zzendcol)
     76 #else
     77 #define ZZINC
     78 #endif
     79 
     80 
     81 #define ZZGETC_STREAM {zzchar = getc(zzstream_in); zzclass = ZZSHIFT(zzchar);}
     82 #define ZZGETC_FUNC {zzchar = (*zzfunc_in)(); zzclass = ZZSHIFT(zzchar);}
     83 #define ZZGETC_STR {       \
     84   if (*zzstr_in){        \
     85     zzchar = *zzstr_in;    \
     86     ++zzstr_in;        \
     87   }else{            \
     88     zzchar = EOF;      \
     89   }              \
     90   zzclass = ZZSHIFT(zzchar);  \
     91 }
     92 
     93 #define ZZNEWSTATE  (newstate = dfa[state][zzclass])
     94 
     95 #ifndef ZZCOPY
     96 #define ZZCOPY  \
     97   /* Truncate matching buffer to size (not an error) */  \
     98   if (zznextpos < lastpos){        \
     99     *(zznextpos++) = zzchar;      \
    100   }else{              \
    101     zzbufovf = 1;          \
    102   }
    103 #endif
    104 
    105 void
    106 #ifdef __USE_PROTOS
    107 zzrdstream( FILE *f )
    108 #else
    109 zzrdstream( f )
    110 FILE *f;
    111 #endif
    112 {
    113   /* make sure that it is really set to something, otherwise just
    114      leave it be.
    115   */
    116   if (f){
    117     /* make sure that there is always someplace to get input
    118        before closing zzstream_in
    119     */
    120 #if 0
    121     if (zzstream_in && zzstream_in!=stdin) fclose( zzstream_in );
    122 #endif
    123     zzline = 1;
    124     zzstream_in = f;
    125     zzfunc_in = NULL;
    126     zzstr_in = 0;
    127     zzcharfull = 0;
    128   }
    129 }
    130 
    131 void
    132 #ifdef __USE_PROTOS
    133 zzrdfunc( int (*f)(void) )
    134 #else
    135 zzrdfunc( f )
    136 int (*f)();
    137 #endif
    138 {
    139   /* make sure that it is really set to something, otherwise just
    140      leave it be.
    141   */
    142   if (f){
    143     /* make sure that there is always someplace to get input
    144        before closing zzstream_in
    145     */
    146 #if 0
    147     if (zzstream_in && zzstream_in!=stdin) fclose( zzstream_in );
    148 #endif
    149     zzline = 1;
    150     zzstream_in = NULL;
    151     zzfunc_in = f;
    152     zzstr_in = 0;
    153     zzcharfull = 0;
    154   }
    155 }
    156 
    157 
    158 void
    159 #ifdef __USE_PROTOS
    160 zzrdstr( zzchar_t *s )
    161 #else
    162 zzrdstr( s )
    163 zzchar_t *s;
    164 #endif
    165 {
    166   /* make sure that it is really set to something, otherwise just
    167      leave it be.
    168   */
    169   if (s){
    170     /* make sure that there is always someplace to get input
    171        before closing zzstream_in
    172     */
    173 #if 0
    174     if (zzstream_in && zzstream_in!=stdin) fclose( zzstream_in );
    175 #endif
    176     zzline = 1;
    177     zzstream_in = NULL;
    178     zzfunc_in = 0;
    179     zzstr_in = s;
    180     zzcharfull = 0;
    181   }
    182 }
    183 
    184 
    185 #ifdef __USE_PROTOS
    186 void zzclose_stream(void)
    187 #else
    188 void zzclose_stream()
    189 #endif
    190 {
    191 #if 0
    192   fclose( zzstream_in );
    193   zzstream_in = NULL;
    194   zzfunc_in = NULL;
    195 #endif
    196 }
    197 
    198 /* saves dlg state, but not what feeds dlg (such as file position) */
    199 void
    200 #ifdef __USE_PROTOS
    201 zzsave_dlg_state(struct zzdlg_state *state)
    202 #else
    203 zzsave_dlg_state(state)
    204 struct zzdlg_state *state;
    205 #endif
    206 {
    207   state->stream = zzstream_in;
    208   state->func_ptr = zzfunc_in;
    209   state->str = zzstr_in;
    210   state->auto_num = zzauto;
    211   state->add_erase = zzadd_erase;
    212   state->lookc = zzchar;
    213   state->char_full = zzcharfull;
    214   state->begcol = zzbegcol;
    215   state->endcol = zzendcol;
    216   state->line = zzline;
    217   state->lextext = zzlextext;
    218   state->begexpr = zzbegexpr;
    219   state->endexpr = zzendexpr;
    220   state->bufsize = zzbufsize;
    221   state->bufovf = zzbufovf;
    222   state->nextpos = zznextpos;
    223   state->class_num = zzclass;
    224 }
    225 
    226 void
    227 #ifdef __USE_PROTOS
    228 zzrestore_dlg_state(struct zzdlg_state *state)
    229 #else
    230 zzrestore_dlg_state(state)
    231 struct zzdlg_state *state;
    232 #endif
    233 {
    234   zzstream_in = state->stream;
    235   zzfunc_in = state->func_ptr;
    236   zzstr_in = state->str;
    237   zzauto = state->auto_num;
    238   zzadd_erase = state->add_erase;
    239   zzchar = state->lookc;
    240   zzcharfull = state->char_full;
    241   zzbegcol = state->begcol;
    242   zzendcol = state->endcol;
    243   zzline = state->line;
    244   zzlextext = state->lextext;
    245   zzbegexpr = state->begexpr;
    246   zzendexpr = state->endexpr;
    247   zzbufsize = state->bufsize;
    248   zzbufovf = state->bufovf;
    249   zznextpos = state->nextpos;
    250   zzclass = state->class_num;
    251 }
    252 
    253 void
    254 #ifdef __USE_PROTOS
    255 zzmode( int m )
    256 #else
    257 zzmode( m )
    258 int m;
    259 #endif
    260 {
    261   /* points to base of dfa table */
    262   if (m<MAX_MODE){
    263     zzauto = m;
    264     /* have to redo class since using different compression */
    265     zzclass = ZZSHIFT(zzchar);
    266   }else{
    267     sprintf(zzebuf,"Invalid automaton mode = %d ",m);
    268     zzerr(zzebuf);
    269   }
    270 }
    271 
    272 /* erase what is currently in the buffer, and get a new reg. expr */
    273 
    274 #ifdef __USE_PROTOS
    275 void zzskip(void)
    276 #else
    277 void zzskip()
    278 #endif
    279 {
    280   zzadd_erase = 1;
    281 }
    282 
    283 /* don't erase what is in the zzlextext buffer, add on to it */
    284 #ifdef __USE_PROTOS
    285 void zzmore()
    286 #else
    287 void zzmore()
    288 #endif
    289 {
    290   zzadd_erase = 2;
    291 }
    292 
    293 /* substitute c for the reg. expr last matched and is in the buffer */
    294 #ifdef __USE_PROTOS
    295 void
    296 zzreplchar(zzchar_t c)
    297 #else
    298 void
    299 zzreplchar(c)
    300 zzchar_t c;
    301 #endif
    302 {
    303   /* can't allow overwriting null at end of string */
    304   if (zzbegexpr < &zzlextext[zzbufsize-1]){
    305     *zzbegexpr = c;
    306     *(zzbegexpr+1) = '\0';
    307   }
    308   zzendexpr = zzbegexpr;
    309   zznextpos = zzbegexpr + 1;
    310 }
    311 
    312 /* replace the string s for the reg. expr last matched and in the buffer */
    313 void
    314 #ifdef __USE_PROTOS
    315 zzreplstr(register zzchar_t *s)
    316 #else
    317 zzreplstr(s)
    318 register zzchar_t *s;
    319 #endif
    320 {
    321   register zzchar_t *l= &zzlextext[zzbufsize -1];
    322 
    323   zznextpos = zzbegexpr;
    324   if (s){
    325      while ((zznextpos <= l) && (*(zznextpos++) = *(s++))!=0){
    326       /* empty */
    327     }
    328     /* correct for NULL at end of string */
    329     zznextpos--;
    330   }
    331   if ((zznextpos <= l) && (*(--s) == 0)){
    332     zzbufovf = 0;
    333   }else{
    334     zzbufovf = 1;
    335   }
    336   *(zznextpos) = '\0';
    337   zzendexpr = zznextpos - 1;
    338 }
    339 
    340 #ifdef __USE_PROTOS
    341 void zzgettok(void)
    342 #else
    343 void zzgettok()
    344 #endif
    345 {
    346   register int state, newstate;
    347   /* last space reserved for the null char */
    348   register zzchar_t *lastpos;
    349 
    350 skip:
    351   zzreal_line = zzline;
    352   zzbufovf = 0;
    353   lastpos = &zzlextext[zzbufsize-1];
    354   zznextpos = zzlextext;
    355   zzbegcol = zzendcol+1;
    356 more:
    357   zzbegexpr = zznextpos;
    358 #ifdef ZZINTERACTIVE
    359   /* interactive version of automaton */
    360   /* if there is something in zzchar, process it */
    361   state = newstate = dfa_base[zzauto];
    362   if (zzcharfull){
    363     ZZINC;
    364     ZZCOPY;
    365     ZZNEWSTATE;
    366   }
    367   if (zzstr_in)
    368     while (zzalternatives[newstate]){
    369       state = newstate;
    370       ZZGETC_STR;
    371       ZZINC;
    372       ZZCOPY;
    373       ZZNEWSTATE;
    374     }
    375   else if (zzstream_in)
    376     while (zzalternatives[newstate]){
    377       state = newstate;
    378       ZZGETC_STREAM;
    379       ZZINC;
    380       ZZCOPY;
    381       ZZNEWSTATE;
    382     }
    383   else if (zzfunc_in)
    384     while (zzalternatives[newstate]){
    385       state = newstate;
    386       ZZGETC_FUNC;
    387       ZZINC;
    388       ZZCOPY;
    389       ZZNEWSTATE;
    390     }
    391   /* figure out if last character really part of token */
    392   if ((state != dfa_base[zzauto]) && (newstate == DfaStates)){
    393     zzcharfull = 1;
    394     --zznextpos;
    395   }else{
    396     zzcharfull = 0;
    397     state = newstate;
    398   }
    399   *(zznextpos) = '\0';
    400   /* Able to transition out of start state to some non err state?*/
    401   if ( state == dfa_base[zzauto] ){
    402     /* make sure doesn't get stuck */
    403     zzadvance();
    404   }
    405 #else
    406   /* non-interactive version of automaton */
    407   if (!zzcharfull)
    408     zzadvance();
    409   else
    410     ZZINC;
    411   state = dfa_base[zzauto];
    412   if (zzstr_in)
    413     while (ZZNEWSTATE != DfaStates){
    414       state = newstate;
    415       ZZCOPY;
    416       ZZGETC_STR;
    417       ZZINC;
    418     }
    419   else if (zzstream_in)
    420     while (ZZNEWSTATE != DfaStates){
    421       state = newstate;
    422       ZZCOPY;
    423       ZZGETC_STREAM;
    424       ZZINC;
    425     }
    426   else if (zzfunc_in)
    427     while (ZZNEWSTATE != DfaStates){
    428       state = newstate;
    429       ZZCOPY;
    430       ZZGETC_FUNC;
    431       ZZINC;
    432     }
    433   zzcharfull = 1;
    434   if ( state == dfa_base[zzauto] ){
    435     if (zznextpos < lastpos){
    436       *(zznextpos++) = zzchar;
    437     }else{
    438       zzbufovf = 1;
    439     }
    440     *zznextpos = '\0';
    441     /* make sure doesn't get stuck */
    442     zzadvance();
    443   }else{
    444     *zznextpos = '\0';
    445   }
    446 #endif
    447 #ifdef ZZCOL
    448   zzendcol -= zzcharfull;
    449 #endif
    450   zzendexpr = zznextpos -1;
    451   zzadd_erase = 0;
    452   (*actions[accepts[state]])();
    453   switch (zzadd_erase) {
    454     case 1: goto skip;
    455     case 2: goto more;
    456   }
    457 }
    458 
    459 #ifdef __USE_PROTOS
    460 void zzadvance(void)
    461 #else
    462 void zzadvance()
    463 #endif
    464 {
    465   if (zzstream_in) { ZZGETC_STREAM; zzcharfull = 1; ZZINC;}
    466   if (zzfunc_in) { ZZGETC_FUNC; zzcharfull = 1; ZZINC;}
    467   if (zzstr_in) { ZZGETC_STR; zzcharfull = 1; ZZINC;}
    468   if (!(zzstream_in || zzfunc_in || zzstr_in)){
    469     zzerr_in();
    470   }
    471 }
    472 
    473 void
    474 #ifdef __USE_PROTOS
    475 zzerrstd(const char *s)
    476 #else
    477 zzerrstd(s)
    478 char *s;
    479 #endif
    480 {
    481         zzLexErrCount++;                /* MR11 */
    482         fprintf(stderr,
    483                 "%s near line %d (text was '%s')\n",
    484                 ((s == NULL) ? "Lexical error" : s),
    485                 zzline,zzlextext);
    486 }
    487 
    488 #ifdef __USE_PROTOS
    489 int zzerr_in(void)
    490 #else
    491 int zzerr_in()
    492 #endif
    493 {
    494   fprintf(stderr,"No input stream, function, or string\n");
    495   /* return eof to get out gracefully */
    496   return EOF;
    497 }
    498 
    499 #endif
    500