psychotrip

Untitled

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