psychotrip

Untitled

May 14th, 2024
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 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. "(?:int|long int|long long int|double|float|char|short int|long double|long|long long|short)?"
  26. "\\s+"
  27. "([a-zA-Z_]\\w*)"
  28. "\\s*"
  29. "(?:\\[[1-9][0-9]*\\]\\s*)*"
  30. "(?:,\\s*([a-zA-Z_]\\w*)\\s*(?:\\[[1-9][0-9]*\\]\\s*)*)*"
  31. ";";
  32.  
  33. regex variablePattern(regexPattern);
  34.  
  35. smatch match;
  36. bool hasErrors = false;
  37. unordered_set<string> variableNames;
  38. int lineNumber = 0;
  39.  
  40. // Поиск переменных и проверка синтаксических ошибок
  41. auto it = sregex_iterator(fileContent.begin(), fileContent.end(), variablePattern);
  42. auto end = sregex_iterator();
  43. for (; it != end; ++it) {
  44. lineNumber++;
  45.  
  46. string syntaxCheck = it->str();
  47. if (syntaxCheck != it->str()) {
  48. outputFile << "Синтаксическая ошибка в строке " << lineNumber << ", позиция " << (it->position() + 1) << endl;
  49. hasErrors = true;
  50. continue;
  51. }
  52.  
  53. for (size_t i = 1; i < it->size(); ++i) {
  54. string variables = (*it)[i];
  55. istringstream variableStream(variables);
  56. string variableName;
  57.  
  58. while (getline(variableStream, variableName, ',')) {
  59. variableName.erase(remove_if(variableName.begin(), variableName.end(), ::isspace),
  60. variableName.end());
  61.  
  62. if (variableNames.count(variableName) > 0) {
  63. outputFile << "Дублирование переменной '" << variableName << "' в строке " << lineNumber <<
  64. ", позиция " << (it->position(i) + 1) << endl;
  65. hasErrors = true;
  66. } else {
  67. variableNames.insert(variableName);
  68. }
  69. }
  70. }
  71. }
  72.  
  73. // Проверка, были ли обнаружены ошибки
  74. if (!hasErrors)
  75. outputFile << "Описание переменных корректное." << endl;
  76.  
  77. inputFile.close();
  78. outputFile.close();
  79.  
  80. cout << "Программа завершила выполнение." << endl;
  81.  
  82. return 0;
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment