psychotrip

Untitled

Jan 16th, 2024
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <regex>
  4. #include <unordered_set>
  5.  
  6. void analyzeVariables(const std::string& inputFileName, const std::string& outputFileName) {
  7. std::ifstream inputFile(inputFileName);
  8. std::ofstream outputFile(outputFileName);
  9.  
  10. if (!inputFile.is_open()) {
  11. std::cerr << "Ошибка открытия файла: " << inputFileName << std::endl;
  12. return;
  13. }
  14.  
  15. std::string inputData((std::istreambuf_iterator<char>(inputFile)), std::istreambuf_iterator<char>());
  16.  
  17. // Регулярное выражение для анализа описания переменных (пример для языка C++)
  18. std::regex variablePattern(R"(\b(?:int|double|long|short|char|float)\s+([a-zA-Z_]\w*)\s*(?:=\s*(?:\"[^\"]*\"|-?\d+(?:\.\d+)?)\s*)?;)");
  19.  
  20. std::smatch match;
  21. std::regex_iterator<std::string::iterator> it(inputData.begin(), inputData.end(), variablePattern);
  22. std::regex_iterator<std::string::iterator> end;
  23.  
  24. // Множество для отслежшвания дублирования имен переменных
  25. std::unordered_set<std::string> variableNames;
  26.  
  27. while (it != end) {
  28. std::string variableName = (*it)[1].str();
  29.  
  30. // Проверка на дублирование имен переменных
  31. if (variableNames.find(variableName) != variableNames.end()) {
  32. outputFile << "Ошибка: Дублирование имени переменной '" << variableName
  33. << "' в строке " << std::count(inputData.begin(), it->suffix().first, '\n') + 1
  34. << ", позиция " << it->position(1) + 1 << "." << std::endl;
  35. return;
  36. }
  37.  
  38. // Запись имени переменной в множество
  39. variableNames.insert(variableName);
  40.  
  41. ++it;
  42. }
  43.  
  44. // Если программа дошла сюда, значит описание переменных корректное
  45. outputFile << "Описание переменных корректное." << std::endl;
  46. }
  47.  
  48. int main() {
  49. analyzeVariables("input.txt", "output.txt");
  50.  
  51. return 0;
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment