示例#1
0
/*
 *      getsym - get next symbol from input stream.
 *
 *      getsym is the basic lexical analyzer.  It builds
 *      basic tokens out of the characters on the input
 *      stream and sets the following global variables:
 *
 *      lastch:         A look behind buffer.
 *      lastst:         type of last symbol read.
 *      laststr:        last string constant read.
 *      lastid:         last identifier read.
 *      ival:           last integer constant read.
 *      rval:           last real constant read.
 *
 *      getsym should be called for all your input needs...
 */
void getsym(void)
{
    if (backupchar !=  - 1)
    {
        lastst = backupchar;
        backupchar =  - 1;
        return ;
    }
    if (! *lptr)
    {
        if ((lastst == eol && lastch ==  - 1) || lastst == eof)
            lastst = eof;
        else
            lastst = eol;
        getch();
        return ;
    }
    while (iswhitespacechar(lastch))
    {
        getch();
        if (! *lptr)
        {
            lastst = eol;
            getch();
            return ;
        }
    }
    if (lastch ==  - 1)
        lastst = eof;
    else if (isdigit(lastch))
        getnum();
    else if (isstartchar(lastch))
    {
        getid();
        searchkw();
    }
    else
        getsym2();
}
示例#2
0
文件: readkeyfile.c 项目: csware/ZKT
static	int	gettok (FILE *fp, char *val, size_t valsize)
{
    int	lastc;
    int	c;
    char	*p;
    char	*bufend;

    *val = '\0';
    do {
        while ( (c = getc (fp)) != EOF && isspace (c) )
            ;

        if ( c == '#' )		/* single line comment ? */
        {
            while ( (c = getc (fp)) != EOF && c != '\n' )
                ;
            continue;
        }

        if ( c == EOF )
            return EOF;

        if ( c == '{' || c == '}' || c == ';' )
            continue;

        if ( c == '/' )		/* begin of C comment ? */
        {
            if ( (c = getc (fp)) == '*' )	/* yes! */
            {
                lastc = EOF;		/* read until end of c comment */
                while ( (c = getc (fp)) != EOF && !(lastc == '*' && c == '/') )
                    lastc = c;
            }
            else if ( c == '/' )	/* is it a C single line comment ? */
            {
                while ( (c = getc (fp)) != EOF && c != '\n' )
                    ;
            }
            else		/* no ! */
                ungetc (c, fp);
            continue;
        }

        if ( c == '\"' )
        {
            p = val;
            bufend = val + valsize - 1;
            while ( (c = getc (fp)) != EOF && p < bufend && c != '\"' )
                *p++ = c;
            *p = '\0';
            /* if string buffer is too small, eat up rest of string */
            while ( c != EOF && c != '\"' )
                c = getc (fp);

            return TOK_STRING;
        }

        p = val;
        bufend = val + valsize - 1;
        do
            *p++ = tolower (c);
        while ( (c = getc (fp)) != EOF && p < bufend && (isalnum (c) || c == '-') );
        *p = '\0';
        ungetc (c, fp);

        if ( (c = searchkw (val)) != TOK_UNKNOWN )
            return c;
    }  while ( c != EOF );

    return EOF;
}