//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //+ Student: Junshan Li + //+ Proj. Assignemnt: Three + //+ Due Date: Febuary 19, 2001 + //+ Course Name: MSCS 518 - Compiler Design + //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // TokFiler.cpp // Implementing TokFiler class. #include #include #include "TokFiler.h" #include "pmglobs.h" #include #include #include // for function void remove(char* fileName). // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // TokFiler class constructor. // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ TokFiler::TokFiler() { ENDOFPROGRAM = false; lineNumber = 0; for (int i = 0; i < STRSIZE; i++) { // initialize fileName array with nil. fileName[i] = '\0'; } } // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Close the file that was opened. // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ void TokFiler::closeTokenFile() { // closes the file that was openned; finOut.close(); } // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Internal Use Only. Returns the total number of lines of the input // file. Used by function seeErrorLines(). // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ int TokFiler::countLines() { char buffer[STRSIZE]; int lineNumber1 = 0; retrieveTokenFile(); while (1) { finOut.getline(buffer, STRSIZE); if (finOut.eof()) break; lineNumber1++; } closeTokenFile(); return (lineNumber1 - 1); } // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Creates a new token file. // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ void TokFiler::createTokenFile() { // creates a new token file remove(fileName); // remove the old token file if exists. finOut.open(fileName, ios::in | ios::out); // open a new token file. //cout <<"****In TokFiler::createTokenFile(), fileName== *** " << fileName<> n1 >> n2; tok.setClass(TokenClass(n1)); tok.setValue(n2); return tok; } // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Initialized lineNumber to 0. // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ void TokFiler::initLineNumber() { lineNumber = 0; } // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Remove file // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ void TokFiler::removeTokenFile() { //finOut.close(); // 1st close the file, then able to remove it. remove(fileName); } // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Opens the named file fileName if it exists or else write an error // message and terminate. // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ void TokFiler::retrieveTokenFile() { cout << "In TokFiler:retrieveTokenFile, fileName == " << fileName << endl; finOut.open(fileName, ios::in | ios::out); if (finOut.fail()) { cout << "File: " << fileName << " opening failed!!!\n"; exit(1); } } // end of function retrieveTokenFile() // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Saves fileName in a char array (string) fileName[]. // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ void TokFiler::storeFileName(char * aFileName) { strcpy(fileName, aFileName); } // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // Displays the entire input file to out put device with line number // front of each line. // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ void TokFiler::seeTokenFile() { // displays entire file on output device (screen). char buffer[STRSIZE]; const NEWLINE=10, ENDOFTEXT=11; int n1, n2; retrieveTokenFile(); cout << "The set of tokens for " << fileName << " is: \n\n"; cout << "Line# tokens\n"; cout << "=============================================\n"; finOut >> n1 >> n2; while (n1 != ENDOFTEXT) { if (n1 == NEWLINE) { cout << "\n(" << n1 << ","; cout.width(2); cout << n2 << ") "; } else { cout << "(" << n1 << "," << n2 << ")"; } // end of if-else. finOut >> n1 >> n2; } // end of while loop. cout << "(" << n1 << "," << n2 << ")\n"; closeTokenFile(); cout << "\nEnd of token file.\n"; }