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