home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / lex / yylex.c < prev   
Encoding:
C/C++ Source or Header  |  1991-04-12  |  4.9 KB  |  230 lines

  1. /* yylex - scanner front-end for flex */
  2.  
  3. /*-
  4.  * Copyright (c) 1990 The Regents of the University of California.
  5.  * All rights reserved.
  6.  *
  7.  * This code is derived from software contributed to Berkeley by
  8.  * Vern Paxson of Lawrence Berkeley Laboratory.
  9.  * 
  10.  * The United States Government has rights in this work pursuant
  11.  * to contract no. DE-AC03-76SF00098 between the United States
  12.  * Department of Energy and the University of California.
  13.  *
  14.  * Redistribution and use in source and binary forms, with or without
  15.  * modification, are permitted provided that the following conditions
  16.  * are met:
  17.  * 1. Redistributions of source code must retain the above copyright
  18.  *    notice, this list of conditions and the following disclaimer.
  19.  * 2. Redistributions in binary form must reproduce the above copyright
  20.  *    notice, this list of conditions and the following disclaimer in the
  21.  *    documentation and/or other materials provided with the distribution.
  22.  * 3. All advertising materials mentioning features or use of this software
  23.  *    must display the following acknowledgement:
  24.  *    This product includes software developed by the University of
  25.  *    California, Berkeley and its contributors.
  26.  * 4. Neither the name of the University nor the names of its contributors
  27.  *    may be used to endorse or promote products derived from this software
  28.  *    without specific prior written permission.
  29.  *
  30.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  31.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  34.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  38.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  39.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  40.  * SUCH DAMAGE.
  41.  */
  42.  
  43. #ifndef lint
  44. static char sccsid[] = "@(#)yylex.c    5.2 (Berkeley) 6/18/90";
  45. #endif /* not lint */
  46.  
  47. #include <ctype.h>
  48. #include "flexdef.h"
  49. #include "parse.h"
  50.  
  51.  
  52. /* ANSI C does not guarantee that isascii() is defined */
  53. #ifndef isascii
  54. #define isascii(c) ((c) <= 0177)
  55. #endif
  56.  
  57.  
  58. /* yylex - scan for a regular expression token
  59.  *
  60.  * synopsis
  61.  *
  62.  *   token = yylex();
  63.  *
  64.  *     token - return token found
  65.  */
  66.  
  67. int yylex()
  68.  
  69.     {
  70.     int toktype;
  71.     static int beglin = false;
  72.  
  73.     if ( eofseen )
  74.     toktype = EOF;
  75.     else
  76.     toktype = flexscan();
  77.  
  78.     if ( toktype == EOF || toktype == 0 )
  79.     {
  80.     eofseen = 1;
  81.  
  82.     if ( sectnum == 1 )
  83.         {
  84.         synerr( "premature EOF" );
  85.         sectnum = 2;
  86.         toktype = SECTEND;
  87.         }
  88.  
  89.     else if ( sectnum == 2 )
  90.         {
  91.         sectnum = 3;
  92.         toktype = 0;
  93.         }
  94.  
  95.     else
  96.         toktype = 0;
  97.     }
  98.  
  99.     if ( trace )
  100.     {
  101.     if ( beglin )
  102.         {
  103.         fprintf( stderr, "%d\t", num_rules + 1 );
  104.         beglin = 0;
  105.         }
  106.  
  107.     switch ( toktype )
  108.         {
  109.         case '<':
  110.         case '>':
  111.         case '^':
  112.         case '$':
  113.         case '"':
  114.         case '[':
  115.         case ']':
  116.         case '{':
  117.         case '}':
  118.         case '|':
  119.         case '(':
  120.         case ')':
  121.         case '-':
  122.         case '/':
  123.         case '\\':
  124.         case '?':
  125.         case '.':
  126.         case '*':
  127.         case '+':
  128.         case ',':
  129.         (void) putc( toktype, stderr );
  130.         break;
  131.  
  132.         case '\n':
  133.         (void) putc( '\n', stderr );
  134.  
  135.         if ( sectnum == 2 )
  136.             beglin = 1;
  137.  
  138.         break;
  139.  
  140.         case SCDECL:
  141.         fputs( "%s", stderr );
  142.         break;
  143.  
  144.         case XSCDECL:
  145.         fputs( "%x", stderr );
  146.         break;
  147.  
  148.         case WHITESPACE:
  149.         (void) putc( ' ', stderr );
  150.         break;
  151.  
  152.         case SECTEND:
  153.         fputs( "%%\n", stderr );
  154.  
  155.         /* we set beglin to be true so we'll start
  156.          * writing out numbers as we echo rules.  flexscan() has
  157.          * already assigned sectnum
  158.          */
  159.  
  160.         if ( sectnum == 2 )
  161.             beglin = 1;
  162.  
  163.         break;
  164.  
  165.         case NAME:
  166.         fprintf( stderr, "'%s'", nmstr );
  167.         break;
  168.  
  169.         case CHAR:
  170.         switch ( yylval )
  171.             {
  172.             case '<':
  173.             case '>':
  174.             case '^':
  175.             case '$':
  176.             case '"':
  177.             case '[':
  178.             case ']':
  179.             case '{':
  180.             case '}':
  181.             case '|':
  182.             case '(':
  183.             case ')':
  184.             case '-':
  185.             case '/':
  186.             case '\\':
  187.             case '?':
  188.             case '.':
  189.             case '*':
  190.             case '+':
  191.             case ',':
  192.             fprintf( stderr, "\\%c", yylval );
  193.             break;
  194.  
  195.             default:
  196.             if ( ! isascii( yylval ) || ! isprint( yylval ) )
  197.                 fprintf( stderr, "\\%.3o", yylval );
  198.             else
  199.                 (void) putc( yylval, stderr );
  200.             break;
  201.             }
  202.             
  203.         break;
  204.  
  205.         case NUMBER:
  206.         fprintf( stderr, "%d", yylval );
  207.         break;
  208.  
  209.         case PREVCCL:
  210.         fprintf( stderr, "[%d]", yylval );
  211.         break;
  212.  
  213.         case EOF_OP:
  214.         fprintf( stderr, "<<EOF>>" );
  215.         break;
  216.  
  217.         case 0:
  218.         fprintf( stderr, "End Marker" );
  219.         break;
  220.  
  221.         default:
  222.         fprintf( stderr, "*Something Weird* - tok: %d val: %d\n",
  223.              toktype, yylval );
  224.         break;
  225.         }
  226.     }
  227.         
  228.     return ( toktype );
  229.     }
  230.