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

📄 previoustoken_expressionresult.cpp

📁 毛坯表达式 求值器
💻 CPP
字号:
%{
#include <stdio.h>
#include <math.h>

#define YYDEBUG 1

int previousToken;
FILE * yyin;
%}
%union {int i; double f;}
%token<i> FUNCTION_NAME
%token<f> NUMBER
%left '+' '-'
%left '*' '/'
%right '^'
%type<f> expr primary_expr

%%

root : expr
    {printf("%f", $1);}
    
expr : primary_expr 
    | expr '+' expr {$$ = $1 + $3;}
    | expr '-' expr {$$ = $1 - $3;}
    | expr '*' expr {$$ = $1 * $3;}
    | expr '/' expr {$$ = $1 / $3;}
    | expr '^' expr {$$ = pow($1,$3);}
    | FUNCTION_NAME '(' expr ')'
    {
        switch($1){
        case 'SIN ':
            $$ = sin($3);
            break;
        case 'COS ':
            $$ = cos($3);
            break;
        default:
            $$ = 0;
        }
    }
    

primary_expr : NUMBER {$$ = $1;}
    | '(' expr ')' {$$ = $2;}

%%

int skipBlank()
{
    int ch = ' ';
    while(!feof(yyin) && strchr(" \t\n", ch)){
        ch = getc(yyin);
    }
    if(!feof(yyin)){
        return ch;
    }
    return 0;
}

int getNumber()
{
    char buf[64];
    int i;
    int ch;
    
    i = 0;
    buf[i] = getc(yyin);
    if(buf[i] == '+' || buf[i] == '-'){
        i++;
        buf[i] = getc(yyin);
    }
    while(isdigit(buf[i]) || strchr("eE.", buf[i])){
        if(i == sizeof(buf)){
            return 0;
        }
        buf[++i] = getc(yyin);
    }
    if(!feof(yyin)){
        ungetc(buf[i], yyin);
    }
    buf[i] = 0;
    yylval.f = atof(buf);
    return NUMBER;
}

int getId()
{
    char buf[64];
    int ch;
    int i;  
    i = 0;
    ch = getc(yyin);
    while(isalpha(ch) || isdigit(ch) || ch == '_'){
        if(i == sizeof(buf)){
            return 0;
        }
        buf[i++] = ch;
        ch = getc(yyin);
    }
    if(!feof(yyin)){
        ungetc(ch, yyin);
    }
    buf[i] = 0;
    if(!strcmpi(buf, "sin")){
        yylval.i = 'SIN ';

    }else if(!strcmpi(buf, "cos")){
        yylval.i = 'COS ';
    }else{
        return 0;
    }
    return FUNCTION_NAME;
}

int yylex()
{
    int ch;
    
    ch = skipBlank();
    if(!ch){
        return 0;
    }
    
    // detect sign
    if(strchr("+-", ch)){
        // printf("last token is %d\n", previousToken);
        if(previousToken != '(' && previousToken != 0){
            goto __done;
        }else{
            // it's a sign prefixed on numbers
            // break through to read a number
        }
    }else if(strchr("*/^()", ch)){
        goto __done;
    }
    
    if(!feof(yyin)){
        ungetc(ch, yyin);
    }
    
    if(isdigit(ch) || strchr("+-.", ch)){
        ch = getNumber();
        goto __done;
    }
    
    ch = getId();
    
__done:
    previousToken = ch;
    return ch;
}

int yyerror()
{return 0;}


int main(int c, char **v)
{
    yydebug = 0;
    if(c > 1){
        yyin = fopen(v[1], "rt");
    }else{
        yyin = stdin;
    }
    yyparse();
    if(yyin != stdin){
        fclose(yyin);
    }
    return 0;
}

⌨️ 快捷键说明

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