psychotrip

Untitled

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