//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //+ Student: Junshan Li + //+ Proj. Assignemnt: Five + //+ Due Date: Febuary 26, 2001 + //+ Course Name: MSCS 518 - Compiler Design + //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // rwtable.cpp // rwtable class implementation. #include #include #include "rwtable.h" rwtable::rwtable() { for (int ch = ' '; ch <= '~'; ch++) C[ch] = 100; C['A'] = 1; C['B'] = -8; C['C'] = 6; C['D'] = 0; C['E'] = -1; C['F'] = 0; C['I'] = 21; C['M'] = -7; C['N'] = 3; C['O'] = 0; C['P'] = 3; C['R'] = -3; C['S'] = 25; C['T'] = -2; C['V'] = 22; C['W'] = 11; C['Y'] = 5; //========= 0 1 2 3 4 5 char *rwArr1[35] = {"BY", "READ", "BOOLEAN", "PROGRAM", "REF", "DO", "ODD", "CHAR", "BEGIN", "THEN", "MOD", "ARRAY", "RESULT", "OR", "FOR", "END", "OF", "AND", "NOT", "TO", "ELSE", "TRUE", "FALSE", "DECLARE", "FORWARD", "FUNCTION", "WHILE", "CONST", "PROCEDURE", "INTEGER", "ABS", "IF", "WRITELN", "DIV", "WRITE" }; for (int i = 0; i < 35; i++) { rwArr[i] = rwArr1[i]; //cout << i << ":" << rwArr[i] << ";"; } cout << endl; /*** for (ch = 'A'; ch <= 'Z'; ch++) { cout << "C['" << char(ch) << "'] = " << C[ch] << "; "; } cout << endl; ***/ } void rwtable::display() { // Display reserved word table characteristics. cout << "===== Displaying Reserved Word index ======\n"; cout << "Reserved Word\t" << "Index" << endl; for (int i = 0; i < 35; i++) { cout << rwArr[i] << "\t" << i << endl; } cout << "\n===== Displaying Charactor code ======\n"; for (int ch = 'A'; ch <= 'Z'; ch++) { cout << "Char. code for: " << char(ch) << ": " << C[ch] << endl; } cout << "\n===== Displaying hashCode for each reserved word =====\n"; for (i = 0; i < 35; i++) { int hashCode; hashCode = rwhash(rwArr[i]); cout << "The rw was: " << rwArr[i] << " the hash code was: " << hashCode << endl; } } int rwtable::numPosCode(int c) { int positionCode; int codeArr[26] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26}; return codeArr[c-int('A')]; } int rwtable::rwhash(char* w) { const first = 0; //cout << "In rwtable::rwhash, w == " << w << endl; int last = strlen(w) - 1; int code = 1; // initialize the code of 'A' to 0. int hashCode; hashCode = C[w[first]] + C[w[last]] + last + numPosCode(w[last-1]); // using last instead of last+1 return hashCode; }