psychotrip

Untitled

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