psychotrip

Untitled

May 14th, 2024
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <boost/regex.hpp>
  5.  
  6. void analyzeVariables(const std::string& input) {
  7. boost::regex regex(R"(
  8. \s*
  9. (?:
  10. (?P<type>
  11. int(?:\s+long|\s+short)?|
  12. long(?:\s+int|\s+double)?|
  13. double(?:\s+long)?|
  14. short(?:\s+int)?|
  15. float|char
  16. )|
  17. (?P<error_type>.*)
  18. )\s+
  19. (?:
  20. (?P<error_id>\b(int|long|short|double|float|char)\b)|
  21. (?:\b(?P<error_name_type>(?P=type))\b|\b(?P<name>[a-zA-Z_]\w*)\b\s*)
  22. (?:
  23. \[
  24. (?P<matrix_id>\s*[1-9]\d*)\s*\]|
  25. (?P<error_matrix_id>[^,;])\s*\]?
  26. )*\s*
  27. (?:
  28. (?P<error_comma>\,\s*(?=;))|
  29. (?P<comma>\,\s*)
  30. )+
  31. )*\s*;\s*
  32. )", boost::regex::extended);
  33.  
  34. boost::smatch matches;
  35. if (boost::regex_search(input, matches, regex)) {
  36. for (size_t i = 0; i < matches.size(); ++i) {
  37. if (!matches["error_type"].str().empty()) {
  38. std::cout << "Error found: " << matches["error_type"].str() << std::endl;
  39. return;
  40. }
  41. else if (!matches["error_id"].str().empty()) {
  42. std::cout << "Error found: " << matches["error_id"].str() << std::endl;
  43. return;
  44. }
  45. else if (!matches["error_name_type"].str().empty()) {
  46. std::cout << "Error found: " << matches["error_name_type"].str() << std::endl;
  47. return;
  48. }
  49. else if (matches["name"].matched &&
  50. (matches["name"].str() == "int" ||
  51. matches["name"].str() == "long" ||
  52. matches["name"].str() == "short" ||
  53. matches["name"].str() == "double" ||
  54. matches["name"].str() == "float" ||
  55. matches["name"].str() == "char")) {
  56. std::cout << "Error found: " << matches["name"].str() << std::endl;
  57. return;
  58. }
  59. else if (!matches["error_matrix_id"].str().empty()) {
  60. std::cout << "Error found: " << matches["error_matrix_id"].str() << std::endl;
  61. return;
  62. }
  63. else if (!matches["error_comma"].str().empty()) {
  64. std::cout << "Error found: " << matches["error_comma"].str() << std::endl;
  65. return;
  66. }
  67. else if (!matches["error_type"].str().empty() ||
  68. !matches["error_id"].str().empty() ||
  69. !matches["error_matrix_id"].str().empty() ||
  70. !matches["error_comma"].str().empty()) {
  71. std::cout << "\nError in the statement. Stopping analysis!" << std::endl;
  72. return;
  73. }
  74. }
  75. }
  76. std::cout << "Analysis of variable declarations completed successfully!" << std::endl;
  77. }
  78.  
  79. int main() {
  80. std::ifstream file("input.txt");
  81. if (!file.is_open()) {
  82. std::cerr << "Failed to open input file!" << std::endl;
  83. return 1;
  84. }
  85.  
  86. std::string input((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
  87. file.close();
  88.  
  89. analyzeVariables(input);
  90.  
  91. return 0;
  92. }
  93.  
Advertisement
Add Comment
Please, Sign In to add comment