const int unknown = 1, constint = 2, punctuation = 3, opr = 4, variable = 5, keyword = 6, endoffile = 7; string kinds[] = { "zero", "unknown", "constint", "punctuation", "opr", "variable", "keyword", "endoffile" }; struct token { int kind; string form; int value; }; struct LexAn { inputter & in; token t; LexAn(inputter & i): in(i) { t.kind = unknown; } void get() { t.kind = unknown; t.form = ""; t.value = 0; char c = in.get(); while (c == ' ' || c == '\n') c = in.get(); if (isdigit(c)) { t.form = ""; t.value = 0; while (isdigit(c)) { t.form += c; t.value = t.value * 10 + c - '0'; c = in.get(); } in.unget(); t.kind = constint; return; } else if (c == '{') { t.form = "{"; t.kind = punctuation; return; } else if (c == '}') { t.form = "}"; t.kind = punctuation; return; } // other forms of punctuation handled the same way else if (c == '+') { t.form = "+"; t.kind = opr; return; } else if (c == '-') { t.form = "-"; t.kind = opr; return; } // other operators handled the same way else if (isalpha(c)) { t.form = ""; while (isalpha(c) || isdigit(c)) { t.form += c; c = in.get(); } in.unget(); if (t.form == "print" || t.form == "while" || t.form == "do" || t.form == "if" || t.form == "then" || t.form == "else") t.kind = keyword; else t.kind = variable; return; } else if (c == 'D'-64) { t.kind = endoffile; return; } else { in.error("invalid character"); t.kind = unknown; return; } } }; void print_token(token t) { cout << "Token, kind=" << kinds[t.kind] << ", form=\"" << t.form << "\", value=" << t.value << "\n"; } int main() { inputter in("testfile.txt"); LexAn L(in); while (true) { L.get(); print_token(L.t); if (L.t.kind == endoffile) break; } }