home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume11 / watcher / part01 / yylex.c < prev   
C/C++ Source or Header  |  1987-09-27  |  2KB  |  92 lines

  1. /*
  2.    yylex for watcher: this is a simple routine looking for numbers,
  3.    special characters and strings.  The special chars are stored in
  4.    'words' and represent tokens by themselves.   In y.tab.h are the
  5.    values to return for the various tokens which are not listed in
  6.    'words'.
  7.  
  8.    Kenneth Ingham
  9.  
  10.    Copyright (C) 1987 The University of New Mexico
  11. */
  12.  
  13. #include "defs.h"
  14.  
  15. char words[] = "\".*|;:%@$-{}";
  16.  
  17. yylex()
  18. {
  19.     extern int yylval, intval, ointval;
  20.     extern char *strval, ostrval[], pipeline[];
  21.     extern FILE *cf;
  22.     int c, value, i;
  23.     static char str[MAX_STR];
  24.  
  25.     while (isspace(c = getc(cf)))
  26.         ;
  27.  
  28.     if (c == EOF)
  29.         return EOF;
  30.  
  31.     if (c == '(') { /* aha, pipeline */
  32.         c = getc(cf);
  33.         for (i=0; c != EOF && c != ')'; i++) {
  34.             str[i] = c;
  35.             c = getc(cf);
  36.         }
  37.         str[i] = '\0';
  38.         if (c == EOF) {
  39.             fprintf(stderr, "Missing ')' to end pipeline.\n");
  40.             return EOF;
  41.         }
  42.         (void) strcpy(pipeline, str);
  43.         return PIPELINE;
  44.     }
  45.  
  46.     if (c == '#') { /* comment to end of line */
  47.         while (c != '\n' && c != EOF)
  48.             c = getc(cf);
  49.         if (c == EOF)
  50.             return EOF;
  51.         return yylex();
  52.     }
  53.  
  54.     if (index(words, c) != 0) {
  55.         yylval = c;
  56.         return c;
  57.     }
  58.  
  59.     if (isdigit(c)) { /* a number */
  60.         value = c - '0';
  61.         while (isdigit(c = getc(cf)))
  62.             value = value * 10 + c - '0';
  63.         ointval = intval;
  64.         intval = value;
  65.         (void) ungetc(c, cf);
  66.         return NUMBER;
  67.     }
  68.  
  69.     (void) strcpy(ostrval, strval);
  70.  
  71.     if (c == '\'') { /* literal string */
  72.         c = getc(cf);
  73.         for (i=0; c != EOF && c != '\''; i++) {
  74.             str[i] = c;
  75.             c = getc(cf);
  76.         }
  77.         str[i] = '\0';
  78.         strval = str;
  79.         return STRING;
  80.     }
  81.  
  82.     /* nothing else matched.  Must be plain string (whitespace sep) */
  83.     for (i=1, str[0]=c; c != EOF && !isspace(c) && !index(words,c); i++) {
  84.         c = getc(cf);
  85.         str[i] = c;
  86.     }
  87.     (void) ungetc(c, cf);
  88.     str[i-1] = '\0';
  89.     strval = str;
  90.     return STRING;
  91. }
  92.