Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <boost/regex.hpp>
- #include <sstream>
- #include <unordered_set>
- using namespace std;
- int main() {
- ifstream inputFile("input.txt");
- ofstream outputFile("output.txt");
- if (!inputFile.is_open() || !outputFile.is_open()) {
- cerr << "Не удалось открыть файлы ввода/вывода." << endl;
- return 1;
- }
- // Считываем файл в строковый поток
- stringstream fileStream;
- fileStream << inputFile.rdbuf();
- string fileContents = fileStream.str();
- string regexPattern =
- "\\b"
- "(?:int|long int|long long int|double|float|char|short int|long double|long|long long|short)?"
- "\\s+"
- "([a-zA-Z_]\\w*)"
- "\\s*"
- "(?:\\[[1-9][0-9]*\\]\\s*)*"
- "(?:,\\s*([a-zA-Z_]\\w*)\\s*(?:\\[[1-9][0-9]*\\]\\s*)*)*"
- ";";
- boost::regex variablePattern(regexPattern);
- unordered_set<string> variableNames;
- int lineNumber = 0;
- bool hasErrors = false;
- // Используем итераторы для поиска по всему файлу
- auto start = boost::sregex_iterator(fileContents.begin(), fileContents.end(), variablePattern);
- auto end = boost::sregex_iterator();
- for (auto it = start; it != end; ++it) {
- lineNumber++;
- boost::smatch match = *it;
- string syntaxCheck = match.str();
- if (syntaxCheck != match.str()) {
- outputFile << "Синтаксическая ошибка в строке " << lineNumber << ", позиция " << (match.position() + 1)
- << endl;
- hasErrors = true;
- }
- for (size_t i = 1; i < match.size(); ++i) {
- string variables = match[i];
- istringstream variableStream(variables);
- string variableName;
- while (getline(variableStream, variableName, ',')) {
- variableName.erase(remove_if(variableName.begin(), variableName.end(), ::isspace),
- variableName.end());
- if (variableNames.count(variableName) > 0) {
- outputFile << "Дублирование переменной '" << variableName << "' в строке " << lineNumber <<
- ", позиция " << (match.position(i) + 1) << endl;
- hasErrors = true;
- } else {
- variableNames.insert(variableName);
- }
- }
- }
- }
- if (!hasErrors)
- outputFile << "Описание переменных корректное." << endl;
- inputFile.close();
- outputFile.close();
- cout << "Программа завершила выполнение." << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment