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>
- #include <algorithm>
- int main() {
- std::ifstream inputFile("input.txt");
- std::ofstream outputFile("output.txt");
- if (!inputFile.is_open() || !outputFile.is_open()) {
- std::cerr << "Не удалось открыть файлы ввода/вывода." << std::endl;
- return 1;
- }
- std::string regexPattern =
- "\\b"
- "(?:int|long\\s+int|long\\s+long\\s+int|double|float|char|short\\s+int|long\\s+double|long|long\\s+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);
- std::unordered_set<std::string> variableNames;
- int lineNumber = 0;
- bool hasErrors = false;
- std::string line;
- std::string currentLine;
- while (std::getline(inputFile, line)) {
- currentLine += line;
- if (line.back() == ';') {
- lineNumber++;
- boost::smatch match;
- if (regex_search(currentLine, match, variablePattern)) {
- std::string syntaxCheck = match[0];
- if (syntaxCheck != currentLine) {
- outputFile << "Синтаксическая ошибка в строке " << lineNumber << ", позиция " << (match.position()) << std::endl;
- hasErrors = true;
- }
- for (size_t i = 1; i < match.size(); ++i) {
- std::string variables = match[i];
- std::istringstream variableStream(variables);
- std::string variableName;
- size_t posInLine = match.position(i)+ 1;
- while (std::getline(variableStream, variableName, ',')) {
- variableName.erase(std::remove_if(variableName.begin(), variableName.end(), ::isspace), variableName.end());
- if (variableName.empty()) {}
- if (variableNames.count(variableName) > 0) {
- outputFile << "Дублирование переменной '" << variableName << "' в строке " << lineNumber << ", позиция " << posInLine << std::endl;
- hasErrors = true;
- } else {
- variableNames.insert(variableName);
- }
- posInLine += variableName.length() + 2;
- }
- }
- } else {
- outputFile << "Синтаксическая ошибка в строке " << lineNumber << std::endl;
- hasErrors = true;
- }
- currentLine.clear();
- } else if (line.back() == ',') {
- currentLine += '\n';
- }
- }
- if (!hasErrors) {
- outputFile << "Нет ошибок синтаксиса." << std::endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment