home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d156 / flex.lha / Flex / CommonFiles / yylex.c < prev   
C/C++ Source or Header  |  1988-10-02  |  4KB  |  222 lines

  1. /* yylex - scanner front-end for flex */
  2.  
  3. #include "flexdef.h"
  4. #include "parse.h"
  5.  
  6. /*
  7.  * Copyright (c) 1987, the University of California
  8.  * 
  9.  * The United States Government has rights in this work pursuant to
  10.  * contract no. DE-AC03-76SF00098 between the United States Department of
  11.  * Energy and the University of California.
  12.  * 
  13.  * This program may be redistributed.  Enhancements and derivative works
  14.  * may be created provided the new works, if made available to the general
  15.  * public, are made available for use by anyone.
  16.  */
  17.  
  18. /* yylex - scan for a regular expression token
  19.  *
  20.  * synopsis
  21.  *
  22.  *   token = yylex();
  23.  *
  24.  *     token - return token found
  25.  */
  26.  
  27. int yylex()
  28.  
  29.     {
  30.     int toktype;
  31.     static int beglin = false;
  32.  
  33.     if ( eofseen )
  34.     toktype = EOF;
  35.     else
  36.     toktype = flexscan();
  37.  
  38.     if ( toktype == EOF )
  39.     {
  40.     eofseen = 1;
  41.  
  42.     if ( sectnum == 1 )
  43.         {
  44.         synerr( "unexpected EOF" );
  45.         sectnum = 2;
  46.         toktype = SECTEND;
  47.         }
  48.  
  49.     else if ( sectnum == 2 )
  50.         {
  51.         sectnum = 3;
  52.         toktype = SECTEND;
  53.         }
  54.  
  55.     else
  56.         toktype = 0;
  57.     }
  58.  
  59.     if ( trace )
  60.     {
  61.     if ( beglin )
  62.         {
  63.         fprintf( stderr, "%d\t", accnum + 1 );
  64.         beglin = 0;
  65.         }
  66.  
  67.     switch ( toktype )
  68.         {
  69.         case '<':
  70.         case '>':
  71.         case '^':
  72.         case '$':
  73.         case '"':
  74.         case '[':
  75.         case ']':
  76.         case '{':
  77.         case '}':
  78.         case '|':
  79.         case '(':
  80.         case ')':
  81.         case '-':
  82.         case '/':
  83.         case '\\':
  84.         case '?':
  85.         case '.':
  86.         case '*':
  87.         case '+':
  88.         case ',':
  89.         (void) putc( toktype, stderr );
  90.         break;
  91.  
  92.         case '\n':
  93.         (void) putc( '\n', stderr );
  94.  
  95.         if ( sectnum == 2 )
  96.             beglin = 1;
  97.  
  98.         break;
  99.  
  100.         case SCDECL:
  101.         fputs( "%s", stderr );
  102.         break;
  103.  
  104.         case XSCDECL:
  105.         fputs( "%x", stderr );
  106.         break;
  107.  
  108.         case WHITESPACE:
  109.         (void) putc( ' ', stderr );
  110.         break;
  111.  
  112.         case SECTEND:
  113.         fputs( "%%\n", stderr );
  114.  
  115.         /* we set beglin to be true so we'll start
  116.          * writing out numbers as we echo rules.  flexscan() has
  117.          * already assigned sectnum
  118.          */
  119.  
  120.         if ( sectnum == 2 )
  121.             beglin = 1;
  122.  
  123.         break;
  124.  
  125.         case NAME:
  126.         fprintf( stderr, "'%s'", nmstr );
  127.         break;
  128.  
  129.         case CHAR:
  130.         switch ( yylval )
  131.             {
  132.             case '<':
  133.             case '>':
  134.             case '^':
  135.             case '$':
  136.             case '"':
  137.             case '[':
  138.             case ']':
  139.             case '{':
  140.             case '}':
  141.             case '|':
  142.             case '(':
  143.             case ')':
  144.             case '-':
  145.             case '/':
  146.             case '\\':
  147.             case '?':
  148.             case '.':
  149.             case '*':
  150.             case '+':
  151.             case ',':
  152.             fprintf( stderr, "\\%c", yylval );
  153.             break;
  154.  
  155.             case 1:
  156.             case 2:
  157.             case 3:
  158.             case 4:
  159.             case 5:
  160.             case 6:
  161.             case 7:
  162.             case 8:
  163.             case 9:
  164.             case 10:
  165.             case 11:
  166.             case 12:
  167.             case 13:
  168.             case 14:
  169.             case 15:
  170.             case 16:
  171.             case 17:
  172.             case 18:
  173.             case 19:
  174.             case 20:
  175.             case 21:
  176.             case 22:
  177.             case 23:
  178.             case 24:
  179.             case 25:
  180.             case 26:
  181.             case 27:
  182.             case 28:
  183.             case 29:
  184.             case 30:
  185.             case 31:
  186.             fprintf( stderr, "^%c", 'A' + yylval - 1 );
  187.             break;
  188.  
  189.             case 127:
  190.             (void) putc( '^', stderr );
  191.             (void) putc( '@', stderr );
  192.             break;
  193.  
  194.             default:
  195.             (void) putc( yylval, stderr );
  196.             break;
  197.             }
  198.             
  199.         break;
  200.  
  201.         case NUMBER:
  202.         fprintf( stderr, "%d", yylval );
  203.         break;
  204.  
  205.         case PREVCCL:
  206.         fprintf( stderr, "[%d]", yylval );
  207.         break;
  208.  
  209.         case 0:
  210.         fprintf( stderr, "End Marker" );
  211.         break;
  212.  
  213.         default:
  214.         fprintf( stderr, "*Something Weird* - tok: %d val: %d\n",
  215.              toktype, yylval );
  216.         break;
  217.         }
  218.     }
  219.         
  220.     return ( toktype );
  221.     }
  222.