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"
- "(?<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*))?"
- ";"
- );
- 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)) {
- if (matches["error_comma"].matched || matches["comma"].matched) {
- error_found = true;
- output_file << "Error found at line " << error_line << ": Unexpected comma or semicolon" << std::endl;
- break;
- }
- if (matches["type"].matched && !matches["name"].matched) {
- error_found = true;
- output_file << "Error found at line " << error_line << ": Type without variable name" << std::endl;
- break;
- }
- for (size_t i = 0; i < matches.size(); ++i) {
- if (matches[i].matched && i != 0 && i != 3) { // Skip full match and 'error_comma' and 'comma' named groups
- 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