home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / LEX.ZIP / YYLEX.C < prev   
Encoding:
C/C++ Source or Header  |  1980-01-01  |  5.1 KB  |  167 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.  */
  25.  
  26. /*
  27.  * yylex for lex tables
  28.  */
  29.  
  30. #include <lex.h>
  31.  
  32. #define ERROR   256     /* yacc's value */
  33.  
  34. tst__b(c, tab)
  35. register int    c;
  36. char            tab[];
  37. {
  38.         return(tab[(c >> 3) & 037] & (1 << (c & 07)) );
  39. }
  40.  
  41. struct  lextab  *_tabp = 0;
  42.  
  43. extern char     *llsave[];      /* Right-context buffer                 */
  44. char    llbuf[100];             /* work buffer                          */
  45. char    *llp1   = &llbuf[0];    /* pointer to next avail. in token      */
  46. char    *llp2   = &llbuf[0];    /* pointer to end of lookahead          */
  47. char    *llend  = &llbuf[0];    /* pointer to end of token              */
  48. char    *llebuf = &llbuf[sizeof llbuf];
  49. int     lleof;
  50. int     yylval  = 0;
  51. int     yyline  = 0;
  52.  
  53. yylex()
  54. {
  55.         register int c, st;
  56.         int final, l, llk, i;
  57.  
  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.  
  117.         register c;
  118.         register struct lextab *lp;
  119.         register char *cp;
  120.  
  121.         lp = _tabp;
  122.         cp = lp->llign;                         /* Ignore class         */
  123.         for (;;) {
  124.                 /*
  125.                  * Get the next character from the save buffer (if possible)
  126.                  * If the save buffer's empty, then return EOF or the next
  127.                  * input character.  Ignore the character if it's in the
  128.                  * ignore class.
  129.                  */
  130.                 c = (llp1 < llp2) ? *llp1 & 0377 : (lleof) ? EOF : lexgetc();
  131.                 if (c >= 0) {                   /* Got a character?     */
  132.                         if (cp && tst__b(c, cp))
  133.                                 continue;       /* Ignore it            */
  134.                         if (llp1 >= llebuf) {   /* No, is there room?   */
  135.                                 lexerror("Token buffer overflow");
  136.                                 exit(1);
  137.                         }
  138.                         *llp1++ = c;            /* Store in token buff  */
  139.                 } else
  140.                         lleof = 1;              /* Set EOF signal       */
  141.                 return(c);
  142.         }
  143. }
  144.  
  145. llset()
  146. /*
  147.  * Return TRUE if EOF and nothing was moved in the look-ahead buffer
  148.  */
  149. {
  150.         register char *lp1, *lp2;
  151.  
  152.         for (lp1 = llbuf, lp2 = llend; lp2 < llp2;)
  153.                 *lp1++ = *lp2++;
  154.         llend = llp1 = llbuf;
  155.         llp2 = lp1;
  156.         return(lleof && lp1 == llbuf);
  157. }
  158.  
  159. /*
  160.  * Re-initialize yylex() so that it can be re-used on
  161.  * another file.
  162.  */
  163. llinit()
  164.    {
  165.    llp1 = llp2 = llend = llbuf;
  166.    llebuf = llbuf + sizeof(llbuf);
  167.    lleof = yylval = yyline = 0;
  168.    }
  169.