home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_progs / fileutil / scan.lha / src / yylex.c < prev   
Encoding:
C/C++ Source or Header  |  1992-05-21  |  5.0 KB  |  168 lines

  1. /*
  2.  * Bob Denny 28-Aug-82  Remove reference to stdio.h
  3.  * Remove code to default lexin, change to call to
  4.  * llstin(), generated by lex depending upon setting
  5.  * of "-s" switch.  Eliminates hardwired dependency
  6.  * on standard I/O library.  Moved declaration of
  7.  * lexin to lexgetc().
  8.  *
  9.  * Bob Denny 31-Aug-82  Add call to lexswitch() in
  10.  * the generated file, to switch to the table whose
  11.  * name was given in the "-t" switch (or to "lextab"
  12.  * if "-t" wasn't given).  Removed hardwired setting
  13.  * of _tabp --> "lextab" here. Now handled automagically.
  14.  *
  15.  * Bob Denny 21-Oct-82  Add llinit() function to re-initialize
  16.  * yylex(), making it serially reusable.
  17.  *
  18.  * Initialize _tabp to NULL so lexswitch() to real table happens
  19.  * only once.
  20.  *
  21.  * Bob Denny 15-Apr-83 Move NBPW to LEX.H and make it 32 on VAX native,
  22.  *                     else 16.
  23.  * Scott Guthery 20-Nov-83      Adapt for IBM PC & DeSmet C
  24.  * William Lee 28-Dec-86 Adapt for Commodore-Amiga 1000 and Lattice C
  25.  */
  26.  
  27. /*
  28.  * yylex for lex tables
  29.  */
  30.  
  31. #include "lex.h"
  32.  
  33. #define ERROR   256     /* yacc's value */
  34.  
  35. tst__b(c, tab)
  36. register int    c;
  37. char            tab[];
  38. {
  39.         return(tab[(c >> 3) & 037] & (1 << (c & 07)) );
  40. }
  41.  
  42. struct  lextab  *_tabp = 0;
  43.  
  44. extern char     *llsave[];      /* Right-context buffer                 */
  45. char    llbuf[100];             /* work buffer                          */
  46. char    *llp1   = &llbuf[0];    /* pointer to next avail. in token      */
  47. char    *llp2   = &llbuf[0];    /* pointer to end of lookahead          */
  48. char    *llend  = &llbuf[0];    /* pointer to end of token              */
  49. char    *llebuf = &llbuf[sizeof llbuf];
  50. int     lleof;
  51. int     yylval  = 0;
  52. int     yyline  = 0;
  53.  
  54. yylex()
  55. {
  56.         register int c, st;
  57.         int final, l, llk, i;
  58.         register struct lextab *lp;
  59.         char *cp;
  60.  
  61.         /*
  62.          * Call llstin() to default lexin to stdin
  63.          * and assign _tabp to "real" table.
  64.          */
  65.         llstin();                       /* Initialize yylex() variables */
  66.  
  67. loop:
  68.         llk = 0;
  69.         if (llset())
  70.                 return(0);              /* Prevent EOF loop     */
  71.         st = 0;
  72.         final = -1;
  73.         lp = _tabp;
  74.  
  75.         do {
  76.                 if (lp->lllook && (l = lp->lllook[st])) {
  77.                         for (c=0; c<NBPW; c++)
  78.                                 if (l&(1<<c))
  79.                                         llsave[c] = llp1;
  80.                         llk++;
  81.                 }
  82.                 if ((i = lp->llfinal[st]) != -1) {
  83.                         final = i;
  84.                         llend = llp1;
  85.                 }
  86.                 if ((c = llinp()) < 0)
  87.                         break;
  88.                 if ((cp = lp->llbrk) && llk==0 && tst__b(c, cp)) {
  89.                         llp1--;
  90.                         break;
  91.                 }
  92.         } while ((st = (*lp->llmove)(lp, c, st)) != -1);
  93.  
  94.  
  95.         if (llp2 < llp1)
  96.                 llp2 = llp1;
  97.         if (final == -1) {
  98.                 llend = llp1;
  99.                 if (st == 0 && c < 0)
  100.                         return(0);
  101.                 if ((cp = lp->llill) && tst__b(c, cp)) {
  102.                         lexerror("Illegal character: %c (%03o)", c, c);
  103.                         goto loop;
  104.                 }
  105.                 return(ERROR);
  106.         }
  107.         if (c = (final >> 11) & 037)
  108.                 llend = llsave[c-1];
  109.         if ((c = (*lp->llactr)(final&03777)) >= 0)
  110.                 return(c);
  111.         goto loop;
  112. }
  113.  
  114. llinp()
  115. {
  116.         register c;
  117.         register struct lextab *lp;
  118.         register char *cp;
  119.  
  120.         lp = _tabp;
  121.         cp = lp->llign;                         /* Ignore class         */
  122.         for (;;) {
  123.                 /*
  124.                  * Get the next character from the save buffer (if possible)
  125.                  * If the save buffer's empty, then return EOF or the next
  126.                  * input character.  Ignore the character if it's in the
  127.                  * ignore class.
  128.                  */
  129.                 c = (llp1 < llp2) ? *llp1 & 0377 : (lleof) ? EOF : lexgetc();
  130.                 if (c >= 0) {                   /* Got a character?     */
  131.                         if (cp && tst__b(c, cp))
  132.                                 continue;       /* Ignore it            */
  133.                         if (llp1 >= llebuf) {   /* No, is there room?   */
  134.                                 lexerror("Token buffer overflow");
  135.                                 exit(1);
  136.                         }
  137.                         *llp1++ = c;            /* Store in token buff  */
  138.                 } else
  139.                         lleof = 1;              /* Set EOF signal       */
  140.                 return(c);
  141.         }
  142. }
  143.  
  144. llset()
  145. /*
  146.  * Return TRUE if EOF and nothing was moved in the look-ahead buffer
  147.  */
  148. {
  149.         register char *lp1, *lp2;
  150.  
  151.         for (lp1 = llbuf, lp2 = llend; lp2 < llp2;)
  152.                 *lp1++ = *lp2++;
  153.         llend = llp1 = llbuf;
  154.         llp2 = lp1;
  155.         return(lleof && lp1 == llbuf);
  156. }
  157.  
  158. /*
  159.  * Re-initialize yylex() so that it can be re-used on
  160.  * another file.
  161.  */
  162. llinit()
  163.    {
  164.    llp1 = llp2 = llend = llbuf;
  165.    llebuf = llbuf + sizeof(llbuf);
  166.    lleof = yylval = yyline = 0;
  167.    }
  168.