enum lexkind { _int, _var, _op, _openp, _closep, _string, _float, _end }; string kindnames[] = { "int", "var", "op", "openp", "closep", "string", "float", "end" }; enum oper { _add, _sub, _mul, _div, _power }; string opnames[] = { "add", "sub", "mul", "div", "power" }; class lexeme { protected: lexkind kind; string form; public: lexeme(lexkind k, string f) { kind = k; form = f; } lexkind get_kind() { return kind; } string get_form() { return form; } virtual int get_intval() { cerr << "a lexeme hasn't got an intval\n"; return 0; } virtual double get_floatval() { cerr << "a lexeme hasn't got a floatval\n"; return 0.0; } virtual string get_stringval() { cerr << "a lexeme hasn't got a stringval\n"; return ""; } virtual hashentry * get_hashentry() { cerr << "a lexeme hasn't got a hashentry\n"; return NULL; } }; class ilexeme: public lexeme { // an ilexeme is a kind of lexeme // ilexeme autuomatically inherits all the members and methods of lexeme // but may replace any of them with its own versions, and add its own // new stuff protected: int intval; public: ilexeme(lexkind k, string f, int v): lexeme(k, f) { intval = v; } virtual int get_intval() { return intval; } }; class flexeme: public lexeme { protected: double floatval; public: flexeme(lexkind k, string f, double v): lexeme(k, f) { floatval = v; } virtual double get_floatval() { return floatval; } }; class slexeme: public lexeme { protected: string stringval; public: slexeme(lexkind k, string f, string v): lexeme(k, f) { stringval = v; } virtual string get_stringval() { return stringval; } }; class helexeme: public lexeme { protected: hashentry * heval; public: helexeme(lexkind k, string f, hashentry * v): lexeme(k, f) { heval = v; } virtual hashentry * get_hashentry() { return heval; } }; void main() { lexeme * tokens[10]; tokens[0] = new ilexeme(_int, "234", 234); tokens[1] = new ilexeme(_op, "+", _add); tokens[2] = new flexeme(_float, "0.00000001", 1.0e-8); tokens[3] = new lexeme(_end, ""); for (int i=0; i<4; i+=1) { lexeme * L = tokens[i]; lexkind k = L->get_kind(); cout << i << ": " << kindnames[k] << " " << L->get_form(); if (k==_int) cout << " " << L->get_intval(); else if (k==_op) cout << " " << opnames[L->get_intval()]; else if (k==_float) cout << " " << L->get_floatval(); cout << "\n"; } }