home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume8 / phoon / lexstring.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-02-26  |  3.7 KB  |  195 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3.  
  4. #define YYLERR yysvec
  5. #define YYTYPE int
  6. #define YYLMAX 256
  7.  
  8. struct yysvf { 
  9.     struct yywork *yystoff;
  10.     struct yysvf *yyother;
  11.     int *yystops;
  12. };
  13.  
  14. struct yywork { 
  15.     YYTYPE    verify;
  16.     YYTYPE    advance; 
  17. }; 
  18.  
  19. extern int yyvstop[];
  20. extern struct yywork yycrank[];
  21. extern struct yysvf yysvec[];
  22. extern struct yywork *yytop;
  23. extern char yymatch[];
  24. extern char yyextra[];
  25.  
  26. #ifdef LEXDEBUG
  27. static int debug = 0;
  28. #endif LEXDEBUG
  29.  
  30. lex_string( strptr, start_cond)
  31.     char    **strptr;
  32.     int    start_cond;
  33. {
  34.     register struct yysvf *state, **lsp;
  35.     register struct yywork *tran;
  36.     register int ch;
  37.     register char    *cp = *strptr;
  38.     register int    *found;
  39.     struct    yysvf *yylstate[YYLMAX];
  40.  
  41.     /* start off machines */
  42.     lsp = yylstate;
  43.     state = yysvec+1+start_cond;
  44.     for (;;){
  45. # ifdef LEXDEBUG
  46.         if(debug)
  47.             fprintf(stderr,"state %d\n",state-yysvec-1);
  48. # endif
  49.         tran = state->yystoff;
  50.         if(tran == yycrank)
  51.             /* may not be any transitions */
  52.             if (state->yyother == 0 ||
  53.                 state->yyother->yystoff == yycrank)
  54.                 break;
  55.  
  56.         ch = *cp++;
  57. #ifdef ONECASE
  58.         if (isupper(ch) )
  59.             ch = tolower(ch);
  60. #endif ONECASE
  61. tryagain:
  62. # ifdef LEXDEBUG
  63.         if(debug){
  64.             fprintf(stderr,"char ");
  65.             allprint(ch);
  66.             putchar('\n');
  67.         }
  68. # endif
  69.         if ( tran > yycrank){
  70.             tran += ch;
  71.             if (tran <= yytop && tran->verify+yysvec == state){
  72.                 if ((state = tran->advance+yysvec) == YYLERR){
  73.                     /* error transitions */
  74.                     --cp;
  75.                     break;
  76.                 }
  77.                 *lsp++ = state;
  78.                 goto contin;
  79.             }
  80.  
  81.         } else if(tran < yycrank) {
  82.             /* r < yycrank */
  83.             tran = yycrank+(yycrank-tran) + ch;
  84. # ifdef LEXDEBUG
  85.             if (debug)
  86.                 fprintf(stderr,"compressed state\n");
  87. # endif
  88.             if(tran <= yytop && tran->verify+yysvec == state){
  89.                 if ((state = tran->advance+yysvec) == YYLERR)
  90.                     /* error transitions */
  91.                     break;
  92.  
  93.                 *lsp++ = state;
  94.                 goto contin;
  95.             }
  96.             tran += (yymatch[ch] - ch);
  97. # ifdef LEXDEBUG
  98.             if(debug){
  99.                 fprintf(stderr,"try fall back character ");
  100.                 allprint(yymatch[ch]);
  101.                 putchar('\n');
  102.             }
  103. # endif
  104.             if(tran <= yytop && tran->verify+yysvec == state){
  105.                 if(tran->advance+yysvec == YYLERR)
  106.                     /* error transition */
  107.                     break;
  108.  
  109.                 *lsp++ = state = tran->advance+yysvec;
  110.                 goto contin;
  111.             }
  112.         }
  113.         if ((state = state->yyother) &&
  114.             (tran = state->yystoff) != yycrank){
  115. # ifdef LEXDEBUG
  116.             if(debug)
  117.                 fprintf(stderr,"fall back to state %d\n",
  118.                     state-yysvec-1);
  119. # endif
  120.             goto tryagain;
  121.         } else
  122.             break;
  123.  
  124. contin:
  125. # ifdef LEXDEBUG
  126.         if(debug){
  127.             fprintf(stderr,"state %d char ",state-yysvec-1);
  128.             allprint(ch);
  129.             putchar('\n');
  130.         }
  131. # endif
  132.         ;
  133.     }
  134. # ifdef LEXDEBUG
  135.     if(debug){
  136.         fprintf(stderr,"stopped at %d with ",*(lsp-1)-yysvec-1);
  137.         allprint(ch);
  138.         putchar('\n');
  139.     }
  140. # endif
  141.     while (lsp-- > yylstate){
  142.         if (*lsp != 0 && (found= (*lsp)->yystops) && *found > 0){
  143.             if(yyextra[*found]){
  144.                 /* must backup */
  145.                 ch = -*found;
  146.                 do {
  147.                     while (*found && *found++ != ch)
  148.                         ;
  149.                  } while (lsp > yylstate &&
  150.                       (found = (*--lsp)->yystops));
  151.             }
  152. # ifdef LEXDEBUG
  153.             if(debug){
  154.                 fprintf(stderr,"\nmatch ");
  155.                 for ( cp = *strptr;
  156.                       cp <= ((*strptr)+(lsp-yylstate));
  157.                       cp++)
  158.                     allprint( *cp );
  159.                 fprintf(stderr," action %d\n",*found);
  160.             }
  161. # endif
  162.             *strptr += (lsp - yylstate + 1);
  163.             return(*found);
  164.         }
  165.     }
  166.     /* the string didn't match anything - if we're looking at
  167.      * eos, just return 0.  Otherwise, bump the string pointer
  168.      * and return -1.
  169.      */
  170. # ifdef LEXDEBUG
  171.     if(debug)
  172.         fprintf(stderr,"\nno match\n");
  173. #endif LEXDEBUG
  174.     if ( **strptr ) {
  175.         (*strptr)++;
  176.         return (-1);
  177.     }
  178.     return (0);
  179. }
  180.  
  181. #ifdef LEXDEBUG
  182. allprint(c)
  183.     char c;
  184. {
  185.     if ( c < 32 ) {
  186.         putc( '^', stderr );
  187.         c += 32;
  188.     } else if ( c == 127 ) {
  189.         putc( '^', stderr );
  190.         c = '?';
  191.     }
  192.     putc( c, stderr );
  193. }
  194. #endif LEXDEBUG
  195.