コード例 #1
0
ファイル: ygttok.c プロジェクト: mingpen/OpenNT
gettok()
   {
   register i, base;
   register c, match, reserve;

begin:
   reserve = 0;
   lineno += peekline;
   peekline = 0;
   c = unix_getc(finput);
   while( c==' ' || c=='\n' || c=='\t' || c=='\f' || c=='\r')
      {
      if( c == '\n' ) ++lineno;
      c=unix_getc(finput);
      }
   if( c == '/' )
      {
      /* skip comment */
      lineno += skipcom();
      goto begin;
      }

   switch(c)
      {

   case -1: /* EOF */
      return(ENDFILE);
   case '{':
      ungetc( c, finput );
      return( '=' );  /* action ... */
   case '<':  /* get, and look up, a type name (union member name) */
      i = 0;
      while( (c=unix_getc(finput)) != '>' && c>=0 && c!= '\n' )
         {
         tokname[i] = c;
         if( ++i >= NAMESIZE ) --i;
         }
      if( c != '>' ) error( "unterminated < ... > clause" );
      tokname[i] = '\0';
      for( i=1; i<=ntypes; ++i )
         {
         if( !strcmp( typeset[i], tokname ) )
            {
            numbval = i;
            return( TYPENAME );
            }
         }
      typeset[numbval = ++ntypes] = cstash( tokname );
      return( TYPENAME );

   case '"':
   case '\'':
      match = c;
      tokname[0] = ' ';
      i = 1;
      for(;;)
         {
         c = unix_getc(finput);
         if( c == '\n' || c == EOF )
            error("illegal or missing ' or \"" );
         if( c == '\\' )
            {
            c = unix_getc(finput);
            tokname[i] = '\\';
            if( ++i >= NAMESIZE ) --i;
            }
         else if( c == match ) break;
         tokname[i] = c;
         if( ++i >= NAMESIZE ) --i;
         }
      break;

   case '%':
   case '\\':

      switch(c=unix_getc(finput))
         {

      case '0':
         return(TERM);
      case '<':
         return(LEFT);
      case '2':
         return(BINARY);
      case '>':
         return(RIGHT);
      case '%':
      case '\\':
         return(MARK);
      case '=':
         return(PREC);
      case '{':
         return(LCURLY);
      default:
         reserve = 1;
         }

   default:

      if( isdigit(c) )
         {
         /* number */
         numbval = c-'0' ;
         base = (c=='0') ? 8 : 10 ;
         for( c=unix_getc(finput); isdigit(c) ; c=getc(finput) )
            {
            numbval = numbval*base + c - '0';
            }
         ungetc( c, finput );
         return(NUMBER);
         }
      else if( islower(c) || isupper(c) || c=='_' || c=='.' || c=='$' )
         {
         i = 0;
         while( islower(c) || isupper(c) || isdigit(c) || c=='_' || c=='.' || c=='$' )
            {
            tokname[i] = c;
            if( reserve && isupper(c) ) tokname[i] += 'a'-'A';
            if( ++i >= NAMESIZE ) --i;
            c = unix_getc(finput);
            }
         }
      else return(c);

      ungetc( c, finput );
      }

   tokname[i] = '\0';

   if( reserve )
      {
      /* find a reserved word */
      if( !strcmp(tokname,"term")) return( TERM );
      if( !strcmp(tokname,"token")) return( TERM );
      if( !strcmp(tokname,"left")) return( LEFT );
      if( !strcmp(tokname,"nonassoc")) return( BINARY );
      if( !strcmp(tokname,"binary")) return( BINARY );
      if( !strcmp(tokname,"right")) return( RIGHT );
      if( !strcmp(tokname,"prec")) return( PREC );
      if( !strcmp(tokname,"start")) return( START );
      if( !strcmp(tokname,"type")) return( TYPEDEF );
      if( !strcmp(tokname,"union")) return( UNION );
      error("invalid escape, or illegal reserved word: %s", tokname );
      }

   /* look ahead to distinguish IDENTIFIER from C_IDENTIFIER */

   c = unix_getc(finput);
   while( c==' ' || c=='\t'|| c=='\n' || c=='\f' || c== '/' )
      {
      if( c == '\n' ) ++peekline;
      else if( c == '/' )
         {
         /* look for comments */
         peekline += skipcom();
         }
      c = unix_getc(finput);
      }
   if( c == ':' ) return( C_IDENTIFIER );
   ungetc( c, finput );
   return( IDENTIFIER );
   }
コード例 #2
0
ファイル: lex.c プロジェクト: UIKit0/turtle
int yylex(void)
{
    char errmsg[80];
    int keyword, c;

loop:
    skipws();

    xline = line;
    xcolumn = column;
    if (ch == '/')
    {
        getch();
        if (ch == '/')
        {
            skipcom();
            goto loop;
        }

        return '/';
    }

    if (ch == EOF)
        return EOF;

    if (isalpha(ch))
    {
        getword();
        keyword = lookup(wbuf);
        wpos = 0;

        if (keyword >= 0)
            return keyword;

        snprintf(yylval.name, BUFSIZ, "%s", wbuf);
        return LIDENT;
    }

    if (isdigit(ch))
    {
        getnum();
        yylval.val = atoi(wbuf);
        wpos = 0;
        return LNUMBER;
    }

    c = ch;
    getch();

    if (c == '#')
    {
        gethex();
        yylval.val = hex(wbuf+1);
        wpos = 0;
        return LHEX;
    }

    switch (c)
    {
        case '+':
        case '-':
        case '{':
        case '}':
        case ',':
        case '*':
        case '%':
        case '|':
        case '&':
        case '^':
        case '~':
        case '(':
        case ')':
            return c;
    }

    snprintf(errmsg, sizeof(errmsg), "unknown symbol: %c", c);
    yyerror(errmsg);
    return 0;
}