home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / OS2-YACC.ZIP / YGTTOK.2C < prev    next >
Text File  |  1989-09-29  |  5KB  |  189 lines

  1. /*
  2.   HEADER: CUG     nnn.nn;
  3.   TITLE:     YACC - Yet Another Compilier-Compilier
  4.   VERSION:     1.0 for IBM-PC
  5.   DATE:      JAN 28, 1985
  6.   DESCRIPTION:     LALR(1) Parser Generator. From UNIX
  7.   KEYWORDS:     Parser Generator Compilier-Compilier YACC
  8.   SYSTEM:     IBM-PC and Compatiables
  9.   FILENAME:     YGTTOK.2C
  10.   WARNINGS:     This program is not for the casual user. It will
  11.          be useful primarily to expert developers.
  12.   CRC:         N/A
  13.   SEE-ALSO:     LEX and PREP
  14.   AUTHORS:     Scott Guthery 11100 leafwood lane Austin, TX 78750
  15.   COMPILERS:     DESMET-C
  16.   REFERENCES:     UNIX Systems Manuals
  17. */
  18.  
  19. #include <string.h>
  20. #include "y1.h"
  21. #include "y2.h"
  22.  
  23. extern int peekline; /* number of '\n' seen in lookahead */
  24.  
  25. int gettok( void )
  26.    {
  27.    register i, base;
  28.    register c, match, reserve;
  29.  
  30. begin:
  31.    reserve = 0;
  32.    lineno += peekline;
  33.    peekline = 0;
  34.    c = unix_getc(finput);
  35.    while( c==' ' || c=='\n' || c=='\t' || c=='\f' || c=='\r')
  36.       {
  37.       if( c == '\n' ) ++lineno;
  38.       c=unix_getc(finput);
  39.       }
  40.    if( c == '/' )
  41.       {
  42.       /* skip comment */
  43.       lineno += skipcom();
  44.       goto begin;
  45.       }
  46.  
  47.    switch(c)
  48.       {
  49.  
  50.    case -1: /* EOF */
  51.       return(ENDFILE);
  52.    case '{':
  53.       uungetc( (char) c, finput );
  54.       return( '=' );  /* action ... */
  55.    case '<':  /* get, and look up, a type name (union member name) */
  56.       i = 0;
  57.       while( (c=unix_getc(finput)) != '>' && c>=0 && c!= '\n' )
  58.      {
  59.          tokname[i] = (char) c;
  60.      if( ++i >= NAMESIZE ) --i;
  61.      }
  62.       if( c != '>' ) error( "unterminated < ... > clause" );
  63.       tokname[i] = '\0';
  64.       for( i=1; i<=ntypes; ++i )
  65.      {
  66.      if( !strcmp( typeset[i], tokname ) )
  67.         {
  68.         numbval = i;
  69.         return( TYPENAME );
  70.         }
  71.      }
  72.       typeset[numbval = ++ntypes] = cstash( tokname );
  73.       return( TYPENAME );
  74.  
  75.    case '"':
  76.    case '\'':
  77.       match = c;
  78.       tokname[0] = ' ';
  79.       i = 1;
  80.       for(;;)
  81.      {
  82.      c = unix_getc(finput);
  83.  
  84.      if( c == '\n' || c == EOF )
  85.         error("illegal or missing ' or \"" );
  86.      if( c == '\\' )
  87.         {
  88.         c = unix_getc(finput);
  89.         tokname[i] = '\\';
  90.         if( ++i >= NAMESIZE ) --i;
  91.         }
  92.      else if( c == match ) break;
  93.          tokname[i] = (char) c;
  94.      if( ++i >= NAMESIZE ) --i;
  95.      }
  96.       break;
  97.  
  98.    case '%':
  99.    case '\\':
  100.  
  101.       switch(c=unix_getc(finput))
  102.      {
  103.  
  104.       case '0':
  105.      return(TERM);
  106.       case '<':
  107.      return(LEFT);
  108.       case '2':
  109.      return(BINARY);
  110.       case '>':
  111.      return(RIGHT);
  112.       case '%':
  113.       case '\\':
  114.      return(MARK);
  115.       case '=':
  116.      return(PREC);
  117.       case '{':
  118.      return(LCURLY);
  119.       default:
  120.      reserve = 1;
  121.      }
  122.  
  123.    default:
  124.  
  125.       if( isdigit(c) )
  126.      {
  127.      /* number */
  128.      numbval = c-'0' ;
  129.      base = (c=='0') ? 8 : 10 ;
  130.      for( c=unix_getc(finput); isdigit(c) ; c=getc(finput) )
  131.         {
  132.         numbval = numbval*base + c - '0';
  133.         }
  134.          uungetc( (char) c, finput );
  135.      return(NUMBER);
  136.      }
  137.       else if( islower(c) || isupper(c) || c=='_' || c=='.' || c=='$' )
  138.      {
  139.      i = 0;
  140.      while( islower(c) || isupper(c) || isdigit(c) || c=='_' || c=='.' || c=='$' )
  141.         {
  142.             tokname[i] = (char) c;
  143.         if( reserve && isupper(c) ) tokname[i] += 'a'-'A';
  144.         if( ++i >= NAMESIZE ) --i;
  145.         c = unix_getc(finput);
  146.  
  147.         }
  148.      }
  149.       else return(c);
  150.  
  151.       uungetc( (char) c, finput );
  152.       }
  153.  
  154.    tokname[i] = '\0';
  155.  
  156.    if( reserve )
  157.       {
  158.       /* find a reserved word */
  159.       if( !strcmp(tokname,"term")) return( TERM );
  160.       if( !strcmp(tokname,"token")) return( TERM );
  161.       if( !strcmp(tokname,"left")) return( LEFT );
  162.       if( !strcmp(tokname,"nonassoc")) return( BINARY );
  163.       if( !strcmp(tokname,"binary")) return( BINARY );
  164.       if( !strcmp(tokname,"right")) return( RIGHT );
  165.       if( !strcmp(tokname,"prec")) return( PREC );
  166.       if( !strcmp(tokname,"start")) return( START );
  167.       if( !strcmp(tokname,"type")) return( TYPEDEF );
  168.       if( !strcmp(tokname,"union")) return( UNION );
  169.       error("invalid escape, or illegal reserved word: %s", tokname );
  170.       }
  171.  
  172.    /* look ahead to distinguish IDENTIFIER from C_IDENTIFIER */
  173.  
  174.    c = unix_getc(finput);
  175.    while( c==' ' || c=='\t'|| c=='\n' || c=='\f' || c== '/' )
  176.       {
  177.       if( c == '\n' ) ++peekline;
  178.       else if( c == '/' )
  179.      {
  180.      /* look for comments */
  181.      peekline += skipcom();
  182.      }
  183.       c = unix_getc(finput);
  184.       }
  185.    if( c == ':' ) return( C_IDENTIFIER );
  186.    uungetc( (char) c, finput );
  187.    return( IDENTIFIER );
  188.    }
  189.