Never been to DZone Snippets before?

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

Simple integer expression interpreter (See related posts)

This is a simple integer expression interpreter.

   1  
   2  #include <iostream>
   3  #include <string>
   4  #include <map>
   5  #include <cctype>
   6  
   7  char look;
   8  std::map<std::string, int> table;
   9  
  10  void getChar() {
  11      look = std::cin.get();
  12  }
  13  
  14  void error(const std::string &s) {
  15      std::cout << std::endl;
  16      std::cout << "Error: " << s << "." << std::endl;
  17  }
  18  
  19  void abort(const std::string &s) {
  20      error(s);
  21      exit(1);
  22  }
  23  
  24  void expected(const std::string &s) {
  25      abort(s + " Expected");
  26  }
  27  
  28  bool isAlpha(char c) {
  29      return isalpha(c);
  30  }
  31  
  32  bool isDigit(char c) {
  33      return isdigit(c);
  34  }
  35  
  36  bool isAlNum(char c) {
  37      return isalnum(c);
  38  }
  39  
  40  bool isAddop(char c) {
  41      return ((c == '+') || (c == '-'));
  42  }
  43  
  44  bool isMulop(char c) {
  45      return (('*' == c) || ('/' == c));
  46  }
  47  
  48  bool isWhite(char c) {
  49      return ((' ' == c) || ('\t' == c));
  50  }
  51  
  52  void skipWhite() {
  53      while (isWhite(look))
  54          getChar();
  55  }
  56  
  57  void match(char x) {
  58      if (look != x)
  59          expected(std::string("\"") + x + "\"");
  60      else {
  61          getChar();
  62          skipWhite();
  63      }
  64  }
  65  
  66  void emit(const std::string &s) {
  67      std::cout << "\t"  << s;
  68  }
  69  
  70  void emitLn(const std::string &s) {
  71      emit(s);
  72      std::cout << std::endl;
  73  }
  74  
  75  void newLine() {
  76      if (look == '\n')
  77          getChar();
  78  }
  79  
  80  std::string getName() {
  81      if (!isAlpha(look))
  82          expected("Name");
  83  
  84      std::string name;
  85      while (isAlNum(look)) {
  86          name += look;
  87          getChar();
  88      }
  89      skipWhite();
  90  
  91      return name;
  92  }
  93  
  94  int getNum() {
  95      int value(0);
  96  
  97      if (!isDigit(look))
  98          expected("Integer");
  99  
 100      while (isDigit(look)) {
 101          value = value * 10 + look - '0';
 102          getChar();
 103      }
 104      skipWhite();
 105  
 106      return value;
 107  }
 108  
 109  int expression();
 110  
 111  int factor() {
 112      int ret;
 113  
 114      if (look == '(') {
 115          match('(');
 116          ret = expression();
 117          match(')');
 118      } else if (isAlpha(look)) {
 119          std::string name = getName();
 120          if (table.count(name) == 0)
 121              table.insert(std::pair<std::string, int>(name, 0));
 122          ret = table[name];
 123      } else
 124          ret = getNum();
 125  
 126      return ret;
 127  }
 128  
 129  int term() {
 130      int value = factor();
 131  
 132      while (isMulop(look)) {
 133          if (look == '*') {
 134              match('*');
 135              value *= factor();
 136          } else if (look == '/') {
 137              match('/');
 138              value /= factor();
 139          }
 140      }
 141  
 142      return value;
 143  }
 144  
 145  int expression() {
 146      int value(0);
 147  
 148      if (!isAddop(look))
 149          value = term();
 150  
 151      while (isAddop(look)) {
 152          if (look == '+') {
 153              match('+');
 154              value += term();
 155          } else if (look == '-') {
 156              match('-');
 157              value -= term();
 158          }
 159      }
 160  
 161      return value;
 162  }
 163  
 164  void assignment() {
 165      std::string name = getName();
 166      match('=');
 167      if (table.count(name) == 1)
 168          table[name] = expression();
 169      else
 170          table.insert(std::pair<std::string, int>(name, expression()));
 171  
 172      std::cout << name << " = " << table[name] << std::endl;
 173  }
 174  
 175  void init() {
 176      getChar();
 177      skipWhite();
 178  }
 179  
 180  int main(int argc, char *argv[]) {
 181      init();
 182  
 183      do {
 184          assignment();
 185          newLine();
 186      } while ((look != '.') && (look != '\n'));
 187  
 188      return 0;
 189  }

You need to create an account or log in to post comments to this site.


Click here to browse all 5355 code snippets

Related Posts