Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <regex>
- #include <string>
- #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;
- }
- 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*)*)*"
- ";";
- regex variablePattern(regexPattern);
- //int a[009];
- unordered_set<string> variableNames;
- string line;
- int lineNumber = 0;
- bool hasErrors = false;
- while (getline(inputFile, line)) {
- lineNumber++;
- smatch match;
- if (regex_search(line, match, variablePattern)) {
- string syntaxCheck = match[0];
- if (syntaxCheck != line) {
- 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);
- }
- }
- }
- } else {
- outputFile << "Синтаксическая ошибка в строке " << lineNumber << ", позиция " << (match.position() + 1)
- << endl;
- hasErrors = true;
- }
- }
- if (!hasErrors)
- outputFile << "Описание переменных корректное." << endl;
- inputFile.close();
- outputFile.close();
- cout << "Программа завершила выполнение." << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment