psychotrip

Untitled

May 14th, 2024
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <boost/regex.hpp>
  4.  
  5. int main() {
  6. std::ifstream file("input.txt");
  7. std::string test_str((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
  8. file.close();
  9.  
  10. boost::regex regexPattern(
  11. "\\b"
  12. "(?:int|long int|double|float|char|short int|long double|long|short|long long)?"
  13. "\\s+"
  14. "([a-zA-Z_]\\w*)"
  15. "\\s*"
  16. "(?:\\[[1-9][0-9]*\\]\\s*)*"
  17. "(?:,\\s*([a-zA-Z_]\\w*)\\s*(?:\\[[1-9][0-9]*\\]\\s*)*)*"
  18. ";"
  19. );
  20.  
  21. boost::smatch matches;
  22. std::ofstream output_file("output.txt");
  23. int error_line = 1;
  24. bool error_found = false;
  25.  
  26. auto start = test_str.cbegin();
  27. auto end = test_str.cend();
  28. while (boost::regex_search(start, end, matches, regexPattern)) {
  29. for (size_t i = 1; i < matches.size(); ++i) {
  30. if (!matches[i].str().empty()) {
  31. error_found = true;
  32. output_file << "Error found at line " << error_line << ": " << matches[i] << std::endl;
  33. break;
  34. }
  35. }
  36. if (error_found)
  37. break;
  38. ++error_line;
  39. start = matches[0].second;
  40. }
  41.  
  42. if (!error_found)
  43. output_file << "\nAnalysis of variable declarations completed successfully!\n";
  44. else
  45. output_file << "\nError in variable declaration. Stopping execution!\n";
  46.  
  47. output_file.close();
  48.  
  49. return 0;
  50. }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment