psychotrip

Untitled

May 14th, 2024
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 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. "(?<type>"
  13. "(?:(?:int|long)\\s+)?(?:int|long|double|float|char|short)?"
  14. ")"
  15. "\\s+"
  16. "(?<name>[a-zA-Z_]\\w*)"
  17. "\\s*"
  18. "(?:\\[(?<size>[1-9][0-9]*)\\]\\s*)*"
  19. "(?:(?<error_comma>,\\s*;)|(?<comma>,\\s*))?"
  20. ";"
  21. );
  22.  
  23. boost::smatch matches;
  24. std::ofstream output_file("output.txt");
  25. int error_line = 1;
  26. bool error_found = false;
  27.  
  28. auto start = test_str.cbegin();
  29. auto end = test_str.cend();
  30. while (boost::regex_search(start, end, matches, regexPattern)) {
  31. if (matches["error_comma"].matched || matches["comma"].matched) {
  32. error_found = true;
  33. output_file << "Error found at line " << error_line << ": Unexpected comma or semicolon" << std::endl;
  34. break;
  35. }
  36.  
  37. if (matches["type"].matched && !matches["name"].matched) {
  38. error_found = true;
  39. output_file << "Error found at line " << error_line << ": Type without variable name" << std::endl;
  40. break;
  41. }
  42.  
  43. for (size_t i = 0; i < matches.size(); ++i) {
  44. if (matches[i].matched && i != 0 && i != 3) { // Skip full match and 'error_comma' and 'comma' named groups
  45. error_found = true;
  46. output_file << "Error found at line " << error_line << ": " << matches[i] << std::endl;
  47. break;
  48. }
  49. }
  50.  
  51. if (error_found)
  52. break;
  53.  
  54. ++error_line;
  55. start = matches[0].second;
  56. }
  57.  
  58. if (!error_found)
  59. output_file << "\nAnalysis of variable declarations completed successfully!\n";
  60. else
  61. output_file << "\nError in variable declaration. Stopping execution!\n";
  62.  
  63. output_file.close();
  64.  
  65. return 0;
  66. }
  67.  
Advertisement
Add Comment
Please, Sign In to add comment