Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <boost/regex.hpp>
- void analyzeVariables(const std::string& input) {
- boost::regex regex(R"(
- \s*
- (?:
- (?P<type>
- int(?:\s+long|\s+short)?|
- long(?:\s+int|\s+double)?|
- double(?:\s+long)?|
- short(?:\s+int)?|
- float|char
- )|
- (?P<error_type>.*)
- )\s+
- (?:
- (?P<error_id>\b(int|long|short|double|float|char)\b)|
- (?:\b(?P<error_name_type>(?P=type))\b|\b(?P<name>[a-zA-Z_]\w*)\b\s*)
- (?:
- \[
- (?P<matrix_id>\s*[1-9]\d*)\s*\]|
- (?P<error_matrix_id>[^,;])\s*\]?
- )*\s*
- (?:
- (?P<error_comma>\,\s*(?=;))|
- (?P<comma>\,\s*)
- )+
- )*\s*;\s*
- )", boost::regex::extended);
- boost::smatch matches;
- if (boost::regex_search(input, matches, regex)) {
- for (size_t i = 0; i < matches.size(); ++i) {
- if (!matches["error_type"].str().empty()) {
- std::cout << "Error found: " << matches["error_type"].str() << std::endl;
- return;
- }
- else if (!matches["error_id"].str().empty()) {
- std::cout << "Error found: " << matches["error_id"].str() << std::endl;
- return;
- }
- else if (!matches["error_name_type"].str().empty()) {
- std::cout << "Error found: " << matches["error_name_type"].str() << std::endl;
- return;
- }
- else if (matches["name"].matched &&
- (matches["name"].str() == "int" ||
- matches["name"].str() == "long" ||
- matches["name"].str() == "short" ||
- matches["name"].str() == "double" ||
- matches["name"].str() == "float" ||
- matches["name"].str() == "char")) {
- std::cout << "Error found: " << matches["name"].str() << std::endl;
- return;
- }
- else if (!matches["error_matrix_id"].str().empty()) {
- std::cout << "Error found: " << matches["error_matrix_id"].str() << std::endl;
- return;
- }
- else if (!matches["error_comma"].str().empty()) {
- std::cout << "Error found: " << matches["error_comma"].str() << std::endl;
- return;
- }
- else if (!matches["error_type"].str().empty() ||
- !matches["error_id"].str().empty() ||
- !matches["error_matrix_id"].str().empty() ||
- !matches["error_comma"].str().empty()) {
- std::cout << "\nError in the statement. Stopping analysis!" << std::endl;
- return;
- }
- }
- }
- std::cout << "Analysis of variable declarations completed successfully!" << std::endl;
- }
- int main() {
- std::ifstream file("input.txt");
- if (!file.is_open()) {
- std::cerr << "Failed to open input file!" << std::endl;
- return 1;
- }
- std::string input((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
- file.close();
- analyzeVariables(input);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment