recursive decent calculator parser

#include <iostream>
#include <assert.h>

using namespace std;
/**
 * calculator parser
 * express -> express+term | express-term | term
 * term -> term*primary | term/primary | primary
 * primary ->  (express) | num
 */
class Token{
public:
    Token(char ch):kind(ch), value(0){}
    Token(char ch, double value): kind(ch), value(value){}
    char kind;
    double value;
};

class Token_stream{
private:
    Token buffer;
    bool full;
public:
    Token_stream():buffer(0), full(false){}
    void putback(Token token);
    Token get();
};

void Token_stream::putback(Token token) {
    buffer = token;
    full = true;
}

Token Token_stream::get() {
    if (full){
        full = false;
        return buffer;
    }
    char ch;
    cin>>ch;
    switch(ch){
        case '(':
        case ')':
        case '+':
        case '-':
        case '*':
        case '/':
        case ';':
        case 'q':
            return Token(ch);
        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
            cin.putback(ch);
            double val;
            cin>>val;
            return Token('8', val);
    }
}
Token_stream ts;
double express();
double primary();
double term();
double express(){
    double left = term();
    Token t = ts.get();
    char ch = t.kind;
    while (true){
        switch (ch){
            case '+':
                left += term();
                t = ts.get();
                ch = t.kind;
                break;
            case '-':
                left -= term();
                t = ts.get();
                ch = t.kind;
                break;
            default:
                ts.putback(t);
                return left;
        }
    }

}
double term(){
    double left = primary();
    Token t = ts.get();
    char ch = t.kind;
    while (true){
        switch (ch){
            case '*':
                left *= primary();
                t = ts.get();
                ch = t.kind;
                break;
            case '/':
                left /= primary();
                t = ts.get();
                ch = t.kind;
                break;
            default:
                ts.putback(t);
                return left;
        }
    }
}
double primary(){
    Token t = ts.get();
    if (t.kind == '('){
        double val = express();
        assert(ts.get().kind == ')');
        return val;
    } else{
        return t.value;
    }
}

int main(){
    cout<<"input the express"<<endl;
    double val;
    while (cin){
        Token t = ts.get();
        if (t.kind == ';')
            cout<<"="<<val<<endl;
        if (t.kind == 'q')
            break;
        else
            ts.putback(t);
        val = express();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值