Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <boost/regex.hpp>
- int main() {
- std::ifstream file("input.txt");
- std::string test_str((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
- file.close();
- boost::regex regexPattern(
- "\\b"
- "(?:int|long int|double|float|char|short int|long double|long|short|long long)?"
- "\\s+"
- "([a-zA-Z_]\\w*)"
- "\\s*"
- "(?:\\[[1-9][0-9]*\\]\\s*)*"
- "(?:,\\s*([a-zA-Z_]\\w*)\\s*(?:\\[[1-9][0-9]*\\]\\s*)*)*"
- ";"
- );
- boost::smatch matches;
- std::ofstream output_file("output.txt");
- int error_line = 1;
- bool error_found = false;
- auto start = test_str.cbegin();
- auto end = test_str.cend();
- while (boost::regex_search(start, end, matches, regexPattern)) {
- for (size_t i = 1; i < matches.size(); ++i) {
- if (!matches[i].str().empty()) {
- error_found = true;
- output_file << "Error found at line " << error_line << ": " << matches[i] << std::endl;
- break;
- }
- }
- if (error_found)
- break;
- ++error_line;
- start = matches[0].second;
- }
- if (!error_found)
- output_file << "\nAnalysis of variable declarations completed successfully!\n";
- else
- output_file << "\nError in variable declaration. Stopping execution!\n";
- output_file.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment