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 / ydefin.2c < prev    next >
Encoding:
Text File  |  1983-12-23  |  1.9 KB  |  79 lines

  1. #include "y2.h"
  2.  
  3. defin( t, s ) register char  *s; 
  4.  
  5.    {
  6.    /*   define s to be a terminal if t=0
  7.         or a nonterminal if t=1         */
  8.  
  9.    register val;
  10.  
  11.    if (t) 
  12.       {
  13.       if( ++nnonter >= NNONTERM ) error("too many nonterminals, limit %d",NNONTERM);
  14.       nontrst[nnonter].name = cstash(s);
  15.       return( NTBASE + nnonter );
  16.       }
  17.    /* must be a token */
  18.    if( ++ntokens >= NTERMS ) error("too many terminals, limit %d",NTERMS );
  19.    tokset[ntokens].name = cstash(s);
  20.  
  21.    /* establish value for token */
  22.  
  23.    if( s[0]==' ' && s[2]=='\0' ) /* single character literal */
  24.       val = s[1];
  25.    else if ( s[0]==' ' && s[1]=='\\' ) 
  26.       {
  27.       /* escape sequence */
  28.       if( s[3] == '\0' )
  29.          {
  30.          /* single character escape sequence */
  31.          switch ( s[2] )
  32.             {
  33.             /* character which is escaped */
  34.          case 'n': 
  35.             val = '\n'; 
  36.             break;
  37.          case 'r': 
  38.             val = '\r'; 
  39.             break;
  40.          case 'b': 
  41.             val = '\b'; 
  42.             break;
  43.          case 't': 
  44.             val = '\t'; 
  45.             break;
  46.          case 'f': 
  47.             val = '\f'; 
  48.             break;
  49.          case '\'': 
  50.             val = '\''; 
  51.             break;
  52.          case '"': 
  53.             val = '"'; 
  54.             break;
  55.          case '\\': 
  56.             val = '\\'; 
  57.             break;
  58.          default: 
  59.             error( "invalid escape" );
  60.             }
  61.          }
  62.  
  63.       else if( s[2] <= '7' && s[2]>='0' )
  64.          {
  65.          /* \nnn sequence */
  66.          if( s[3]<'0' || s[3] > '7' || s[4]<'0' ||
  67.              s[4]>'7' || s[5] != '\0' ) error("illegal \\nnn construction" );
  68.          val = 64*s[2] + 8*s[3] + s[4] - 73*'0';
  69.          if( val == 0 ) error( "'\\000' is illegal" );
  70.          }
  71.       }
  72.    else 
  73.       {
  74.       val = extval++;
  75.       }
  76.    tokset[ntokens].value = val;
  77.    toklev[ntokens] = 0;
  78.    return( ntokens );
  79.    }
  80.