home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / alde_c / misc / comm / yaccunx / ygttok.2c < prev    next >
Encoding:
Text File  |  1983-12-24  |  4.1 KB  |  167 lines

  1. #include "y2.h"
  2.  
  3. extern int peekline; /* number of '\n' seen in lookahead */
  4.  
  5. gettok() 
  6.    {
  7.    register i, base;
  8.    register c, match, reserve;
  9.  
  10. begin:
  11.    reserve = 0;
  12.    lineno += peekline;
  13.    peekline = 0;
  14.    c = unix_getc(finput);
  15.    while( c==' ' || c=='\n' || c=='\t' || c=='\f' || c=='\r')
  16.       {
  17.       if( c == '\n' ) ++lineno;
  18.       c=unix_getc(finput);
  19.       }
  20.    if( c == '/' )
  21.       {
  22.       /* skip comment */
  23.       lineno += skipcom();
  24.       goto begin;
  25.       }
  26.  
  27.    switch(c)
  28.       {
  29.  
  30.    case -1: /* EOF */
  31.       return(ENDFILE);
  32.    case '{':
  33.       ungetc( c, finput );
  34.       return( '=' );  /* action ... */
  35.    case '<':  /* get, and look up, a type name (union member name) */
  36.       i = 0;
  37.       while( (c=unix_getc(finput)) != '>' && c>=0 && c!= '\n' )
  38.          {
  39.          tokname[i] = c;
  40.          if( ++i >= NAMESIZE ) --i;
  41.          }
  42.       if( c != '>' ) error( "unterminated < ... > clause" );
  43.       tokname[i] = '\0';
  44.       for( i=1; i<=ntypes; ++i )
  45.          {
  46.          if( !strcmp( typeset[i], tokname ) )
  47.             {
  48.             numbval = i;
  49.             return( TYPENAME );
  50.             }
  51.          }
  52.       typeset[numbval = ++ntypes] = cstash( tokname );
  53.       return( TYPENAME );
  54.  
  55.    case '"':    
  56.    case '\'':
  57.       match = c;
  58.       tokname[0] = ' ';
  59.       i = 1;
  60.       for(;;)
  61.          {
  62.          c = unix_getc(finput);
  63.  
  64.          if( c == '\n' || c == EOF )
  65.             error("illegal or missing ' or \"" );
  66.          if( c == '\\' )
  67.             {
  68.             c = unix_getc(finput);
  69.             tokname[i] = '\\';
  70.             if( ++i >= NAMESIZE ) --i;
  71.             }
  72.          else if( c == match ) break;
  73.          tokname[i] = c;
  74.          if( ++i >= NAMESIZE ) --i;
  75.          }
  76.       break;
  77.  
  78.    case '%':
  79.    case '\\':
  80.  
  81.       switch(c=unix_getc(finput)) 
  82.          {
  83.  
  84.       case '0': 
  85.          return(TERM);
  86.       case '<': 
  87.          return(LEFT);
  88.       case '2': 
  89.          return(BINARY);
  90.       case '>': 
  91.          return(RIGHT);
  92.       case '%':
  93.       case '\\':        
  94.          return(MARK);
  95.       case '=': 
  96.          return(PREC);
  97.       case '{': 
  98.          return(LCURLY);
  99.       default:  
  100.          reserve = 1;
  101.          }
  102.  
  103.    default:
  104.  
  105.       if( isdigit(c) )
  106.          {
  107.          /* number */
  108.          numbval = c-'0' ;
  109.          base = (c=='0') ? 8 : 10 ;
  110.          for( c=unix_getc(finput); isdigit(c) ; c=getc(finput) )
  111.             {
  112.             numbval = numbval*base + c - '0';
  113.             }
  114.          ungetc( c, finput );
  115.          return(NUMBER);
  116.          }
  117.       else if( islower(c) || isupper(c) || c=='_' || c=='.' || c=='$' )
  118.          {
  119.          i = 0;
  120.          while( islower(c) || isupper(c) || isdigit(c) || c=='_' || c=='.' || c=='$' )
  121.             {
  122.             tokname[i] = c;
  123.             if( reserve && isupper(c) ) tokname[i] += 'a'-'A';
  124.             if( ++i >= NAMESIZE ) --i;
  125.             c = unix_getc(finput);
  126.  
  127.             }
  128.          }
  129.       else return(c);
  130.  
  131.       ungetc( c, finput );
  132.       }
  133.  
  134.    tokname[i] = '\0';
  135.  
  136.    if( reserve )
  137.       {
  138.       /* find a reserved word */
  139.       if( !strcmp(tokname,"term")) return( TERM );
  140.       if( !strcmp(tokname,"token")) return( TERM );
  141.       if( !strcmp(tokname,"left")) return( LEFT );
  142.       if( !strcmp(tokname,"nonassoc")) return( BINARY );
  143.       if( !strcmp(tokname,"binary")) return( BINARY );
  144.       if( !strcmp(tokname,"right")) return( RIGHT );
  145.       if( !strcmp(tokname,"prec")) return( PREC );
  146.       if( !strcmp(tokname,"start")) return( START );
  147.       if( !strcmp(tokname,"type")) return( TYPEDEF );
  148.       if( !strcmp(tokname,"union")) return( UNION );
  149.       error("invalid escape, or illegal reserved word: %s", tokname );
  150.       }
  151.  
  152.    /* look ahead to distinguish IDENTIFIER from C_IDENTIFIER */
  153.  
  154.    c = unix_getc(finput);
  155.    while( c==' ' || c=='\t'|| c=='\n' || c=='\f' || c== '/' ) 
  156.       {
  157.       if( c == '\n' ) ++peekline;
  158.       else if( c == '/' )
  159.          {
  160.          /* look for comments */
  161.          peekline += skipcom();
  162.          }
  163.       c = unix_getc(finput);
  164.       }
  165.    if( c == ':' ) return( C_IDENTIFIER );
  166.    ungetc( c, finput );
  167.    return( IDENTIFIER );
  168.    }
  169.