示例#1
0
文件: main.cpp 项目: rootial/Compiler
//checked
int readInt(string &fres, stringstream &strIn, bool can = true) {
    if (can)
        fres = "";
    string res = "";
    char ch;
    while (~(ch = strIn.get()) &&
            (ch == ' ' || ch == '\n' || ch == '\t')) {
        ;
    }

    if (strIn.eof()) return END;
    if (ch == '+' || ch == '-' || (ch >= '0' && ch <= '9')) {
        res += ch, fres += ch;
    } else {
        strIn.putback(ch);
        return ERR;
    }
    while (~(ch = strIn.get()) &&
            (ch >= '0' && ch <= '9')) {
        res += ch, fres += ch;
    }
    int st = strIn.eof();

    if (st) {
        if (isdigit(res[0]) || (int)res.size() > 1)
            return OKAY;
        else return ERR;
    }
    strIn.putback(ch);
    if ((int)res.size() == 1 && !isdigit(res[0])) {
        strIn.putback(res[0]);
    }
    return isdigit(res[0]) || (int)res.size() > 1 ? OKAY : ERR;
}
示例#2
0
文件: main.cpp 项目: rootial/Compiler
int readCha(string &res, stringstream &strIn) {
    res = "";
    char ch;
    while (~(ch = strIn.get()) &&
            (ch == ' ' || ch == '\n' || ch == '\t')) {
        ;
    }
    if (strIn.eof()) return END;
    if (ch == '\'') {
        res += ch;
        strIn >> ch;
        if (strIn.eof()) {
            strIn.putback(ch);
            return ERR;
        }
        if (ch == '\\') {
            res += ch;
            if ((ch = strIn.get()) == -1) return ERR;
            res += ch;
            if ((ch = strIn.get()) == -1 || ch != '\'') {
                return ERR;
            }
            res += ch;
        } else {
            res += ch;
//            cout << ch << endl;
            if ((ch = strIn.get()) == -1 || ch != '\'') {
                return ERR;
            }
            res += ch;
        }
    } else {
示例#3
0
文件: main.cpp 项目: rootial/Compiler
void Revc(string &str, stringstream &strIn) {
    for (int i = (int)str.size()-1; i >= 0; i--) {
        strIn.putback(str[i]);
    }
}
示例#4
0
文件: main.cpp 项目: rootial/Compiler
int readFloat(string &res, stringstream &strIn) {
//    cout << strIn.str() << endl;
    res = "";
    char ch;
    while (~(ch = strIn.get()) &&
            (ch == ' ' || ch == '\n' || ch == '\t')) {
        ;
    }
    if (strIn.eof()) return END;
    if (ch != '.' && ch != '+' && ch != '-' && !(ch <= '9' && ch >= '0')) {
        return ERR;
    }
    if (ch == '.') {
        res += ch;
        if ((ch = strIn.get()) == -1 || !isdigit(ch)) {

            if (!strIn.eof()) {
                strIn.putback(ch);
            }
            strIn.putback(res[0]);
            return ERR;
        } else {
//        cout << res << endl;
            strIn.putback(ch);
//            cout << ch << endl;
            readInt(res, strIn, false);
//            cout << res << endl;
            if ((ch = strIn.get()) == -1 || ch != 'e') {
                if (!strIn.eof()) {
                    strIn.putback(ch);
                }
                return OKAY;
            } else {
                res += ch;
                if ((ch = strIn.get()) == -1 || !(ch == '+' || ch == '-' || isdigit(ch))) {
                    return ERR;
                } else {
                    strIn.putback(ch);
                    if (readInt(res, strIn, false) == ERR) return ERR;
                }
            }
        }
    } else if (isdigit(ch)) {
        strIn.putback(ch);
        readInt(res, strIn, false);

        if ((ch = strIn.get()) == -1 || ch != '.') {

            if (ch != 'e') {

                if (ch != -1)
                    strIn.putback(ch);
                Revc(res, strIn);
                return ERR;
            } else {
                res += ch;
                if ((ch = strIn.get()) == -1 || !(ch == '+' || ch == '-' || isdigit(ch))) {
                    return ERR;
                } else {
                    strIn.putback(ch);
                    if (readInt(res, strIn, false) == ERR) return ERR;
                }
            }

        } else {
            res += ch;
            if ((ch = strIn.get()) == -1) {
                return OKAY;
            } else if (isdigit(ch)) {
                strIn.putback(ch);
                readInt(res, strIn, false);
                if ((ch = strIn.get()) == -1 || ch != 'e') {
                    if (!strIn.eof())strIn.putback(ch);
                    return OKAY;
                } else {
                    res += ch;
                    if ((ch = strIn.get()) == -1 || !(ch == '+' || ch == '-' || isdigit(ch))) {
                        return ERR;
                    } else {
                        strIn.putback(ch);
                        if (readInt(res, strIn, false) == ERR) return ERR;
                    }
                }
            } else if (ch == 'e') {
                res += ch;
                if ((ch = strIn.get()) == -1 || !(ch == '+' || ch == '-' || isdigit(ch))) {
                    return ERR;
                } else {
                    strIn.putback(ch);
                    if (readInt(res, strIn, false) == ERR) return ERR;
                }
            } else {
                strIn.putback(ch);
                return OKAY;
            }
        }
    } else {

    }
    return OKAY;
}