⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lex.c

📁 compiler
💻 C
字号:
/*@A (C) 1992 Allen I. Holub                                                */
#include "lex.h"
#include <stdio.h>
#include <ctype.h>

char       *yytext   = "";  /* Lexeme (not '\0' terminated)   */
int        yyleng    = 0;   /* Lexeme length.                 */
int        yylineno  = 0;   /* Input line number              */

lex()
{
    static char input_buffer[128];
    char        *current;

    current = yytext + yyleng;  	/* Skip current lexeme  */

    while( 1 )                  	/* Get the next one     */
    {
        while( !*current )
        {
            /* Get new lines, skipping any leading white space on the line,
	     * until a nonblank line is found.
             */

            current = input_buffer;
            if( !gets( input_buffer ) )
            {
                *current = '\0' ;
                return EOI;
            }

            ++yylineno;

            while( isspace(*current) )
                ++current;
        }

        for( ; *current ; ++current )
        {
            /* Get the next token */

            yytext = current;
            yyleng = 1;

            switch( *current )
            {
            case ';': return SEMI  ;
            case '+': return PLUS  ;
            case '*': return TIMES ;
            case '(': return LP    ;
            case ')': return RP    ;

            case '\n':
            case '\t':
            case ' ' : break;

            default:
                if( !isalnum(*current) )
                    fprintf(stderr, "Ignoring illegal input <%c>\n", *current);
                else
                {
                    while( isalnum(*current) )
                        ++current;

                    yyleng = current - yytext;
                    return NUM_OR_ID;
                }

                break;
            }
        }
    }
}
static int Lookahead = -1;      /* Lookahead token  */

int match( token )
int token;
{
    /* Return true if "token" matches the current lookahead symbol. */

    if( Lookahead == -1 )
        Lookahead = lex();

    return token == Lookahead;
}

void advance()
{
    /* Advance the lookahead to the next input symbol.  */

    Lookahead = lex();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -