home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / OS2_LEX.ZIP / YYLEX.C < prev   
C/C++ Source or Header  |  1989-12-20  |  5KB  |  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. char    *llsave[NBPW];          /* Right-context buffer                 */
  44. char    llbuf[512];             /* 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.         register struct lextab *lp;
  58.         char *cp;
  59.  
  60.         /*
  61.          * Call llstin() to default lexin to stdin
  62.          * and assign _tabp to "real" table.
  63.          */
  64.         llstin();                       /* Initialize yylex() variables */
  65.  
  66. loop:
  67.         llk = 0;
  68.         if (llset())
  69.                 return(0);              /* Prevent EOF loop     */
  70.         st = 0;
  71.         final = -1;
  72.         lp = _tabp;
  73.  
  74.         do {
  75.                 if (lp->lllook && (l = lp->lllook[st])) {
  76.                         for (c=0; c<NBPW; c++)
  77.                                 if (l&(1<<c))
  78.                                         llsave[c] = llp1;
  79.                         llk++;
  80.                 }
  81.                 if ((i = lp->llfinal[st]) != -1) {
  82.                         final = i;
  83.                         llend = llp1;
  84.                 }
  85.                 if ((c = llinp()) < 0)
  86.                         break;
  87.                 if ((cp = lp->llbrk) && llk==0 && tst__b(c, cp)) {
  88.                         llp1--;
  89.                         break;
  90.                 }
  91.         } while ((st = (*lp->llmove)(lp, c, st)) != -1);
  92.  
  93.  
  94.         if (llp2 < llp1)
  95.                 llp2 = llp1;
  96.         if (final == -1) {
  97.                 llend = llp1;
  98.                 if (st == 0 && c < 0)
  99.                         return(0);
  100.                 if ((cp = lp->llill) && tst__b(c, cp)) {
  101.                         lexerror("Illegal character: %c (%03o)", c, c);
  102.                         goto loop;
  103.                 }
  104.                 return(ERROR);
  105.         }
  106.         if (c = (final >> 11) & 037)
  107.                 llend = llsave[c-1];
  108.         if ((c = (*lp->llactr)(final&03777)) >= 0)
  109.                 return(c);
  110.         goto loop;
  111. }
  112.  
  113. llinp()
  114. {
  115.         register c;
  116.         register struct lextab *lp;
  117.         register char *cp;
  118.  
  119.         lp = _tabp;
  120.         cp = lp->llign;                         /* Ignore class         */
  121.         for (;;) {
  122.                 /*
  123.                  * Get the next character from the save buffer (if possible)
  124.                  * If the save buffer's empty, then return EOF or the next
  125.                  * input character.  Ignore the character if it's in the
  126.                  * ignore class.
  127.                  */
  128.                 c = (llp1 < llp2) ? *llp1 & 0377 : (lleof) ? EOF : lexgetc();
  129.                 if (c >= 0) {                   /* Got a character?     */
  130.                         if (cp && tst__b(c, cp))
  131.                                 continue;       /* Ignore it            */
  132.                         if (llp1 >= llebuf) {   /* No, is there room?   */
  133.                                 lexerror("Token buffer overflow");
  134.                                 exit(1);
  135.                         }
  136.                         *llp1++ = c;            /* Store in token buff  */
  137.                 } else
  138.                         lleof = 1;              /* Set EOF signal       */
  139.                 return(c);
  140.         }
  141. }
  142.  
  143. llset()
  144. /*
  145.  * Return TRUE if EOF and nothing was moved in the look-ahead buffer
  146.  */
  147. {
  148.         register char *lp1, *lp2;
  149.  
  150.         for (lp1 = llbuf, lp2 = llend; lp2 < llp2;)
  151.                 *lp1++ = *lp2++;
  152.         llend = llp1 = llbuf;
  153.         llp2 = lp1;
  154.         return(lleof && lp1 == llbuf);
  155. }
  156.  
  157. /*
  158.  * Re-initialize yylex() so that it can be re-used on
  159.  * another file.
  160.  */
  161. llinit()
  162.    {
  163.    llp1 = llp2 = llend = llbuf;
  164.    llebuf = llbuf + sizeof(llbuf);
  165.    lleof = yylval = yyline = 0;
  166.    }
  167.