DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Simple Integer Expression Interpreter

08.30.2007
| 3898 views |
  • submit to reddit
        This is a simple integer expression interpreter.

#include <iostream>
#include <string>
#include <map>
#include <cctype>

char look;
std::map<std::string, int> table;

void getChar() {
    look = std::cin.get();
}

void error(const std::string &s) {
    std::cout << std::endl;
    std::cout << "Error: " << s << "." << std::endl;
}

void abort(const std::string &s) {
    error(s);
    exit(1);
}

void expected(const std::string &s) {
    abort(s + " Expected");
}

bool isAlpha(char c) {
    return isalpha(c);
}

bool isDigit(char c) {
    return isdigit(c);
}

bool isAlNum(char c) {
    return isalnum(c);
}

bool isAddop(char c) {
    return ((c == '+') || (c == '-'));
}

bool isMulop(char c) {
    return (('*' == c) || ('/' == c));
}

bool isWhite(char c) {
    return ((' ' == c) || ('\t' == c));
}

void skipWhite() {
    while (isWhite(look))
        getChar();
}

void match(char x) {
    if (look != x)
        expected(std::string("\"") + x + "\"");
    else {
        getChar();
        skipWhite();
    }
}

void emit(const std::string &s) {
    std::cout << "\t"  << s;
}

void emitLn(const std::string &s) {
    emit(s);
    std::cout << std::endl;
}

void newLine() {
    if (look == '\n')
        getChar();
}

std::string getName() {
    if (!isAlpha(look))
        expected("Name");

    std::string name;
    while (isAlNum(look)) {
        name += look;
        getChar();
    }
    skipWhite();

    return name;
}

int getNum() {
    int value(0);

    if (!isDigit(look))
        expected("Integer");

    while (isDigit(look)) {
        value = value * 10 + look - '0';
        getChar();
    }
    skipWhite();

    return value;
}

int expression();

int factor() {
    int ret;

    if (look == '(') {
        match('(');
        ret = expression();
        match(')');
    } else if (isAlpha(look)) {
        std::string name = getName();
        if (table.count(name) == 0)
            table.insert(std::pair<std::string, int>(name, 0));
        ret = table[name];
    } else
        ret = getNum();

    return ret;
}

int term() {
    int value = factor();

    while (isMulop(look)) {
        if (look == '*') {
            match('*');
            value *= factor();
        } else if (look == '/') {
            match('/');
            value /= factor();
        }
    }

    return value;
}

int expression() {
    int value(0);

    if (!isAddop(look))
        value = term();

    while (isAddop(look)) {
        if (look == '+') {
            match('+');
            value += term();
        } else if (look == '-') {
            match('-');
            value -= term();
        }
    }

    return value;
}

void assignment() {
    std::string name = getName();
    match('=');
    if (table.count(name) == 1)
        table[name] = expression();
    else
        table.insert(std::pair<std::string, int>(name, expression()));

    std::cout << name << " = " << table[name] << std::endl;
}

void init() {
    getChar();
    skipWhite();
}

int main(int argc, char *argv[]) {
    init();

    do {
        assignment();
        newLine();
    } while ((look != '.') && (look != '\n'));

    return 0;
}