#include #include #include #include #include #include using namespace std; struct inputsystem { string line; int pos, len, linenumber, numerrors; istream * file; bool dontread; static const char control_d = 'D' - 64; void clear() { line = ""; pos = 0; len = 0; numerrors = 0; linenumber = 0; dontread = false; file = & cin; } void open(istream & f) { file = & f; } void close() { if (file != & cin) ((ifstream *)file)->close(); } inputsystem(istream & f) { clear(); open(f); } inputsystem() { clear(); } char get() { while (true) { if (pos < 0) { pos += 1; return ' '; } else if (pos == len) { pos += 1; return ' '; } else if (pos > len) { getline(* file, line); if (file->fail()) return control_d; pos = 0; len = line.length(); linenumber += 1; continue; } char c = line[pos]; pos += 1; return c; } } void unget() { pos -= 1; } void unget(char c) { pos -= 1; if (pos >= 0) line[pos] = c; } void error(string message, string detail = "") { cout << "Line " << linenumber << " Error " << message << detail << "\n"; cout << line << "\n"; numerrors += 1; if (numerrors >= 10) { cout << "Too many errors, giving up.\n"; exit(1); } } }; int main(int argc, char * argv[]) { ifstream fi; inputsystem in; if (argc > 1) { fi.open(argv[1]); if (fi.fail()) { cout << "Can't open \"" << argv[1] << "\"\n"; exit(1); } in.open(fi); } while (true) { char c = in.get(); if (c == in.control_d) { cout << "end of file\n"; break; } cout << "got '" << c << "'\n"; if (c == '*') in.error("multiplication forbidden!\n"); else if (c == 'i') { cout << "unget four times\n"; in.unget('I'); in.unget(); in.unget(); in.unget(); } } in.close(); cout << "The End.\n"; }