|
| 1 | +#include "Translator_LanguageC.h" |
| 2 | +#include "LexicalAnalisator.h" |
| 3 | +#include "function.h" |
| 4 | + |
| 5 | +LexicalAnalisator::LexicalAnalisator() |
| 6 | +{ |
| 7 | +} |
| 8 | + |
| 9 | + |
| 10 | +LexicalAnalisator::~LexicalAnalisator() |
| 11 | +{ |
| 12 | +} |
| 13 | + |
| 14 | + |
| 15 | +System::String^ StlWStringToString(std::string const& os) |
| 16 | +{ |
| 17 | + System::String^ str = gcnew System::String(os.c_str()); |
| 18 | + //String^ str = gcnew String(""); |
| 19 | + return str; |
| 20 | +} |
| 21 | + |
| 22 | +void LexicalAnalisator::addCode(std::string str, std::map<std::string, std::string>& table, int numTable) |
| 23 | +{ |
| 24 | + std::string result = fillTable(str, table, numTable); |
| 25 | + if (result.find("Error") != std::string::npos) |
| 26 | + { |
| 27 | + System::String^ temp = StlWStringToString(str); |
| 28 | + System::String^ tempResult = StlWStringToString(result); |
| 29 | + System::Windows::Forms::MessageBox::Show(tempResult, temp, System::Windows::Forms::MessageBoxButtons::OK, System::Windows::Forms::MessageBoxIcon::Error); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +int LexicalAnalisator::checkStringSingleElem(std::string const& word) |
| 34 | +{ |
| 35 | + if (isDigit((int)word[0]) == true) |
| 36 | + return 1; |
| 37 | + if (isOperation((int)word[0]) == true || isLogicalSingleOperation((int)word[0]) == true) |
| 38 | + return 2; |
| 39 | + if (isSeparators((int)word[0]) == true) |
| 40 | + return 3; |
| 41 | + if (isLetter((int)word[0]) == true) |
| 42 | + return 4; |
| 43 | + return 0; |
| 44 | +} |
| 45 | + |
| 46 | +std::string LexicalAnalisator::getCodeWordLength_1(std::string word) |
| 47 | +{ |
| 48 | + switch (checkStringSingleElem(word)) |
| 49 | + { |
| 50 | + case 1: |
| 51 | + if (getCodeByName(numberConst,word) == "\0") |
| 52 | + addCode(word, numberConst, 2); |
| 53 | + return getCodeByName(numberConst, word); |
| 54 | + case 2: |
| 55 | + return getOperations(word,true); |
| 56 | + case 3: |
| 57 | + return getSeparators(word,true); |
| 58 | + case 4: |
| 59 | + if (getCodeByName(identifier,word) == "\0") |
| 60 | + addCode(word, identifier, 1); |
| 61 | + return getCodeByName(identifier, word); |
| 62 | + default: |
| 63 | + return ""; |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | + |
| 68 | +std::string LexicalAnalisator::getCodeWordLengthGreaterOne(std::string word) |
| 69 | +{ |
| 70 | + std::string code = getServiceWord(word,true); |
| 71 | + if (code == "\0") |
| 72 | + code = getOperations(word,true); |
| 73 | + if (code == "\0") |
| 74 | + { |
| 75 | + if (isNumber(word) == true) |
| 76 | + { |
| 77 | + if (getCodeByName(numberConst, word) == "\0") |
| 78 | + addCode(word, numberConst, 2); |
| 79 | + return getCodeByName(numberConst, word); |
| 80 | + } |
| 81 | + else |
| 82 | + { |
| 83 | + if ((int)word[0] == 34)// \" |
| 84 | + { |
| 85 | + if (isLibrary_header(word) == false) |
| 86 | + { |
| 87 | + if (getCodeByName(symbolsConst, word) == "\0") |
| 88 | + addCode(word, symbolsConst, 3); |
| 89 | + return getCodeByName(symbolsConst, word); |
| 90 | + } |
| 91 | + } |
| 92 | + if (getCodeByName(identifier, word) == "\0") |
| 93 | + addCode(word, identifier, 1); |
| 94 | + return getCodeByName(identifier, word); |
| 95 | + } |
| 96 | + } |
| 97 | + else |
| 98 | + return code; |
| 99 | +} |
| 100 | + |
| 101 | +std::string LexicalAnalisator::getCodeWord(std::string word) |
| 102 | +{ |
| 103 | + if (word.length() == 1) |
| 104 | + return getCodeWordLength_1(word); |
| 105 | + else |
| 106 | + return getCodeWordLengthGreaterOne(word); |
| 107 | +} |
| 108 | + |
| 109 | +bool LexicalAnalisator::skipAnalyzeOneLineComment(bool readComment, std::string line, __int64 index,std::ofstream& file) |
| 110 | +{ |
| 111 | + if (readComment == false && isOneStringComment((int)line[index], (int)line[index + 1]) == true) |
| 112 | + { |
| 113 | + std::string oneLineComment = ""; |
| 114 | + oneLineComment.assign(line, index, line.length() - index); |
| 115 | + file << oneLineComment << " "; |
| 116 | + return true; |
| 117 | + } |
| 118 | + return false; |
| 119 | +} |
| 120 | + |
| 121 | +bool LexicalAnalisator::skipAnalyzeComment(bool& readComment, std::string line, __int64& index, std::ofstream& file, std::string& word) |
| 122 | +{ |
| 123 | + if (readComment == true && isComment((int)line[index + 1], (int)line[index]) == true) |
| 124 | + { |
| 125 | + readComment = false; |
| 126 | + word += line[index]; |
| 127 | + word += line[index + 1]; |
| 128 | + if (word != "\0" && word != "") |
| 129 | + file << word << " "; |
| 130 | + word = ""; |
| 131 | + index++; |
| 132 | + return true; |
| 133 | + } |
| 134 | + return false; |
| 135 | +} |
| 136 | +bool LexicalAnalisator::isLibrary_header(std::string const& word) |
| 137 | +{ |
| 138 | + return (int)word[0] == 34 && (int)word[word.length() - 1] == 34 && (int)word[word.length() - 2] == 104 && (int)word[word.length() - 3] == 46 ? true : false; |
| 139 | +} |
| 140 | + |
| 141 | +void LexicalAnalisator::makeLexicalAnalyze(std::string filePathOrName_C, std::string fileName_Path_SaveAnalis) |
| 142 | +{ |
| 143 | + std::ifstream fileC; |
| 144 | + std::ofstream fileAnalysis(fileName_Path_SaveAnalis); |
| 145 | + fileC.exceptions(std::ifstream::badbit); |
| 146 | + try |
| 147 | + { |
| 148 | + fileC.open(filePathOrName_C); |
| 149 | + |
| 150 | + if (fileC.is_open()) |
| 151 | + { |
| 152 | + bool readComment = false; |
| 153 | + std::string word = ""; |
| 154 | + while (!fileC.eof()) |
| 155 | + { |
| 156 | + std::string stringLanguageC = ""; |
| 157 | + getline(fileC, stringLanguageC); |
| 158 | + for (__int64 i = 0; i < stringLanguageC.length(); i++) |
| 159 | + { |
| 160 | + if (isServiceSymbols((int)stringLanguageC[i]) == true) |
| 161 | + continue; |
| 162 | + if (isComment((int)stringLanguageC[i], (int)stringLanguageC[i + 1]) == true) |
| 163 | + readComment = true; |
| 164 | + if (skipAnalyzeOneLineComment(readComment,stringLanguageC,i,fileAnalysis)==true) |
| 165 | + break; |
| 166 | + |
| 167 | + if (skipAnalyzeComment(readComment, stringLanguageC, i, fileAnalysis,word)) |
| 168 | + continue; |
| 169 | + |
| 170 | + if (readComment == false) |
| 171 | + { |
| 172 | + if (isSeparators((int)stringLanguageC[i]) == true && word[0] != '\"') |
| 173 | + { |
| 174 | + if (word.length() != 0) |
| 175 | + fileAnalysis << getCodeWord(word) << " "; |
| 176 | + word = stringLanguageC[i]; |
| 177 | + fileAnalysis << getCodeWord(word) << " "; |
| 178 | + word = ""; |
| 179 | + continue; |
| 180 | + } |
| 181 | + |
| 182 | + // <library.h> and "string" |
| 183 | + if (stringLanguageC[i] == '<' || stringLanguageC[i] == '\"') |
| 184 | + { |
| 185 | + int posClose = 0; |
| 186 | + int countSymbols = 0; |
| 187 | + if (stringLanguageC[i] == '<') |
| 188 | + posClose = stringLanguageC.find(">", 1); |
| 189 | + else |
| 190 | + posClose = stringLanguageC.rfind('\"'); |
| 191 | + |
| 192 | + if (posClose != -1) |
| 193 | + { |
| 194 | + countSymbols = posClose + 1 - i; |
| 195 | + word.assign(stringLanguageC, i, countSymbols); |
| 196 | + if (word.find(".h") != -1) |
| 197 | + { |
| 198 | + fileAnalysis << getCodeWord(word) << " "; |
| 199 | + word = ""; |
| 200 | + if (stringLanguageC[static_cast<__int64>(posClose) + 1] == '\0') |
| 201 | + break; |
| 202 | + else |
| 203 | + i = posClose; |
| 204 | + } |
| 205 | + else |
| 206 | + { |
| 207 | + if (word[0] == '\"') |
| 208 | + { |
| 209 | + fileAnalysis << getCodeWord(word) << " "; |
| 210 | + i = static_cast<__int64>(posClose) + 1; |
| 211 | + |
| 212 | + } |
| 213 | + } |
| 214 | + word = ""; |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + if (isOperation((int)stringLanguageC[i]) == true || isLogicalSingleOperation((int)stringLanguageC[i]) == true) |
| 219 | + { |
| 220 | + if (isIncrement((int)stringLanguageC[i], (int)stringLanguageC[i + 1]) == true || |
| 221 | + isDoubleOperation((int)stringLanguageC[i], (int)stringLanguageC[i + 1]) == true || |
| 222 | + isLogicalDoubleOperation((int)stringLanguageC[i], (int)stringLanguageC[i + 1]) == true) |
| 223 | + { |
| 224 | + word += stringLanguageC[i]; |
| 225 | + i++; |
| 226 | + } |
| 227 | + word += stringLanguageC[i]; |
| 228 | + fileAnalysis << getCodeWord(word) << " "; |
| 229 | + word = ""; |
| 230 | + continue; |
| 231 | + } |
| 232 | + |
| 233 | + if (stringLanguageC[i] != ' ') |
| 234 | + { |
| 235 | + if (isLetter((int)stringLanguageC[i]) == true && (isLetter((int)stringLanguageC[i + 1]) == false && isDigit((int)stringLanguageC[i + 1]) == false)) |
| 236 | + { |
| 237 | + word += stringLanguageC[i]; |
| 238 | + if (isTypeDeclaration(word) && (stringLanguageC[i + 1] == '*' || stringLanguageC[i + 2] == '*')) |
| 239 | + { |
| 240 | + word += '*'; |
| 241 | + if (stringLanguageC[i + 2] == '*') |
| 242 | + i += 2; |
| 243 | + else |
| 244 | + i++; |
| 245 | + } |
| 246 | + fileAnalysis << getCodeWord(word) << " "; |
| 247 | + word = ""; |
| 248 | + continue; |
| 249 | + } |
| 250 | + else |
| 251 | + { |
| 252 | + if (stringLanguageC[i] == '#') |
| 253 | + { |
| 254 | + word += stringLanguageC[i]; |
| 255 | + continue; |
| 256 | + } |
| 257 | + |
| 258 | + } |
| 259 | + word += stringLanguageC[i]; |
| 260 | + } |
| 261 | + else |
| 262 | + { |
| 263 | + if (word == "\0") |
| 264 | + continue; |
| 265 | + else |
| 266 | + { |
| 267 | + fileAnalysis << getCodeWord(word) << " "; |
| 268 | + word = ""; |
| 269 | + } |
| 270 | + } |
| 271 | + } |
| 272 | + else |
| 273 | + { |
| 274 | + word += stringLanguageC[i]; |
| 275 | + } |
| 276 | + |
| 277 | + } |
| 278 | + if (word != "\0") |
| 279 | + { |
| 280 | + if (readComment == false) |
| 281 | + fileAnalysis << getCodeWord(word); |
| 282 | + else |
| 283 | + word += '\n'; |
| 284 | + } |
| 285 | + if (readComment == false) |
| 286 | + fileAnalysis << "\n"; |
| 287 | + } |
| 288 | + } |
| 289 | + |
| 290 | + } |
| 291 | + catch (const std::ifstream::failure & exep) |
| 292 | + { |
| 293 | + std::cout << " Exception opening/reading file"; |
| 294 | + std::cout << exep.what(); |
| 295 | + System::Windows::Forms::MessageBox::Show("File don't open", "error", System::Windows::Forms::MessageBoxButtons::OK, System::Windows::Forms::MessageBoxIcon::Error); |
| 296 | + } |
| 297 | + |
| 298 | + fileC.close(); |
| 299 | + fileAnalysis.close(); |
| 300 | +} |
| 301 | + |
| 302 | + |
| 303 | + |
0 commit comments