psychotrip

Untitled

May 14th, 2024
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <boost/regex.hpp>
  5. #include <unordered_set>
  6.  
  7. using namespace std;
  8. using namespace boost;
  9.  
  10. int main() {
  11.  
  12. ifstream inputFile("input.txt");
  13. ofstream outputFile("output.txt");
  14.  
  15. if (!inputFile.is_open() || !outputFile.is_open()) {
  16. cerr << "Не удалось открыть файлы ввода/вывода." << endl;
  17. return 1;
  18. }
  19.  
  20. // Считываем весь файл в строку
  21. string fileContent((istreambuf_iterator<char>(inputFile)), istreambuf_iterator<char>());
  22.  
  23. string regexPattern =
  24. "\\b"
  25. "(?<type>"
  26. "(?:(?:int|long)\\s+)?(?:int|long|double|float|char|short)?"
  27. ")"
  28. "\\s+"
  29. "(?<name>[a-zA-Z_]\\w*)"
  30. "\\s*"
  31. "(?:\\[(?<size>[1-9][0-9]*)\\]\\s*)*"
  32. "(?:(?<error_comma>,\\s*;)|(?<comma>,\\s*))?"
  33. ";";
  34.  
  35. regex variablePattern(regexPattern);
  36.  
  37. smatch match;
  38. bool hasErrors = false;
  39. unordered_set<string> variableNames;
  40. int lineNumber = 0;
  41.  
  42. // Поиск переменных и проверка синтаксических ошибок
  43. auto it = sregex_iterator(fileContent.begin(), fileContent.end(), variablePattern);
  44. auto end = sregex_iterator();
  45. for (; it != end; ++it) {
  46. lineNumber++;
  47.  
  48. string syntaxCheck = it->str();
  49. if (syntaxCheck != it->str()) {
  50. outputFile << "Синтаксическая ошибка в строке " << lineNumber << ", позиция " << (it->position() + 1) << endl;
  51. hasErrors = true;
  52. continue;
  53. }
  54.  
  55. string type = (*it)["type"];
  56. string name = (*it)["name"];
  57. string size = (*it)["size"];
  58.  
  59. if (type.empty()) {
  60. outputFile << "Ошибка в типе данных в строке " << lineNumber << endl;
  61. hasErrors = true;
  62. }
  63.  
  64. if (name.empty()) {
  65. outputFile << "Ошибка в имени переменной в строке " << lineNumber << endl;
  66. hasErrors = true;
  67. } else {
  68. istringstream variableStream(name);
  69. string variableName;
  70.  
  71. while (getline(variableStream, variableName, ',')) {
  72. variableName.erase(remove_if(variableName.begin(), variableName.end(), ::isspace),
  73. variableName.end());
  74.  
  75. if (variableNames.count(variableName) > 0) {
  76. outputFile << "Дублирование переменной '" << variableName << "' в строке " << lineNumber <<
  77. ", позиция " << (it->position("name") + 1) << endl;
  78. hasErrors = true;
  79. } else {
  80. variableNames.insert(variableName);
  81. }
  82. }
  83. }
  84.  
  85. if (!size.empty()) {
  86. size_t pos = it->position("size");
  87. outputFile << "Размер массива в строке " << lineNumber << " обнаружен в позиции " << (pos + 1) << ": " << size << endl;
  88. }
  89.  
  90. if ((*it)["error_comma"].matched || (*it)["comma"].matched) {
  91. outputFile << "Ошибка: неожиданная запятая или точка с запятой в строке " << lineNumber << endl;
  92. hasErrors = true;
  93. }
  94. }
  95.  
  96. // Проверка, были ли обнаружены ошибки
  97. if (!hasErrors)
  98. outputFile << "Описание переменных корректное." << endl;
  99.  
  100. inputFile.close();
  101. outputFile.close();
  102.  
  103. cout << "Программа завершила выполнение." << endl;
  104.  
  105. return 0;
  106. }
  107.  
Advertisement
Add Comment
Please, Sign In to add comment