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.          if( c == '\n' || c == EOF )
  64.             error("illegal or missing ' or \"" );
  65.          if( c == '\\' )
  66.             {
  67.             c = unix_getc(finput);
  68.             tokname[i] = '\\';
  69.             if( ++i >= NAMESIZE ) --i;
  70.             }
  71.          else if( c == match ) break;
  72.          tokname[i] = c;
  73.          if( ++i >= NAMESIZE ) --i;
  74.          }
  75.       break;
  76.  
  77.    case '%':
  78.    case '\\':
  79.  
  80.       switch(c=unix_getc(finput)) 
  81.          {
  82.  
  83.       case '0': 
  84.          return(TERM);
  85.       case '<': 
  86.          return(LEFT);
  87.       case '2': 
  88.          return(BINARY);
  89.       case '>': 
  90.          return(RIGHT);
  91.       case '%':
  92.       case '\\':        
  93.          return(MARK);
  94.       case '=': 
  95.          return(PREC);
  96.       case '{': 
  97.          return(LCURLY);
  98.       default:  
  99.          reserve = 1;
  100.          }
  101.  
  102.    default:
  103.  
  104.       if( isdigit(c) )
  105.          {
  106.          /* number */
  107.          numbval = c-'0' ;
  108.          base = (c=='0') ? 8 : 10 ;
  109.          for( c=unix_getc(finput); isdigit(c) ; c=getc(finput) )
  110.             {
  111.             numbval = numbval*base + c - '0';
  112.             }
  113.          ungetc( c, finput );
  114.          return(NUMBER);
  115.          }
  116.       else if( islower(c) || isupper(c) || c=='_' || c=='.' || c=='$' )
  117.          {
  118.          i = 0;
  119.          while( islower(c) || isupper(c) || isdigit(c) || c=='_' || c=='.' || c=='$' )
  120.             {
  121.             tokname[i] = c;
  122.             if( reserve && isupper(c) ) tokname[i] += 'a'-'A';
  123.             if( ++i >= NAMESIZE ) --i;
  124.             c = unix_getc(finput);
  125.             }
  126.          }
  127.       else return(c);
  128.  
  129.       ungetc( c, finput );
  130.       }
  131.  
  132.    tokname[i] = '\0';
  133.  
  134.    if( reserve )
  135.       {
  136.       /* find a reserved word */
  137.       if( !strcmp(tokname,"term")) return( TERM );
  138.       if( !strcmp(tokname,"token")) return( TERM );
  139.       if( !strcmp(tokname,"left")) return( LEFT );
  140.       if( !strcmp(tokname,"nonassoc")) return( BINARY );
  141.       if( !strcmp(tokname,"binary")) return( BINARY );
  142.       if( !strcmp(tokname,"right")) return( RIGHT );
  143.       if( !strcmp(tokname,"prec")) return( PREC );
  144.       if( !strcmp(tokname,"start")) return( START );
  145.       if( !strcmp(tokname,"type")) return( TYPEDEF );
  146.       if( !strcmp(tokname,"union")) return( UNION );
  147.       error("invalid escape, or illegal reserved word: %s", tokname );
  148.       }
  149.  
  150.    /* look ahead to distinguish IDENTIFIER from C_IDENTIFIER */
  151.  
  152.    c = unix_getc(finput);
  153.    while( c==' ' || c=='\t'|| c=='\n' || c=='\f' || c== '/' ) 
  154.       {
  155.       if( c == '\n' ) ++peekline;
  156.       else if( c == '/' )
  157.          {
  158.          /* look for comments */
  159.          peekline += skipcom();
  160.          }
  161.       c = unix_getc(finput);
  162.       }
  163.    if( c == ':' ) return( C_IDENTIFIER );
  164.    ungetc( c, finput );
  165.    return( IDENTIFIER );
  166.    }
  167.