Задать вопрос
@DobriyParen

Не выводит в консоли дерево вывода в чём беда?

#include <iostream>
#include <string>
#include <vector>
#include <regex>
 
using namespace std;
 
enum TokenType {
    IDENTIFIER,
    HEX_NUMBER,
    KEYWORD_IF,
    KEYWORD_THEN,
    KEYWORD_ELSE,
    ASSIGNMENT_OPERATOR,
    EQUALITY_OPERATOR
};
 
struct Token {
    TokenType type;
    string value;
    int line;
    int column;
 
    Token(TokenType t, string v, int l, int c) : type(t), value(v), line(l), column(c) {}
};
 
struct Node {
    string value;
    vector<Node*> children;
 
    Node(string v) : value(v) {}
};
 
string to_upper(string str) {
    string result = "";
    for (char c : str) {
        result += toupper(c);
    }
    return result;
}
 
vector<Token> tokenize(string input) {
    vector<Token> tokens;
 
    regex identifier_regex("^[a-zA-Z]+[a-zA-Z0-9]*");
    regex hex_number_regex("^0x[0-9a-fA-F]+");
    regex keyword_if_regex("^IF");
    regex keyword_then_regex("^THEN");
    regex keyword_else_regex("^ELSE");
    regex assignment_operator_regex("^:=$");
    regex equality_operator_regex("^=$");
 
    int line = 1;
    int column = 1;
 
    while (!input.empty()) {
        smatch identifier_match;
        if (regex_search(input, identifier_match, identifier_regex)) {
            Token token(IDENTIFIER, identifier_match.str(), line, column);
            tokens.push_back(token);
            input = input.substr(identifier_match.length());
            column += identifier_match.length();
            continue;
        }
 
        smatch hex_number_match;
        if (regex_search(input, hex_number_match, hex_number_regex)) {
            Token token(HEX_NUMBER, hex_number_match.str(), line, column);
            tokens.push_back(token);
            input = input.substr(hex_number_match.length());
            column += hex_number_match.length();
            continue;
        }
 
        smatch keyword_if_match;
        if (regex_search(input, keyword_if_match, keyword_if_regex)) {
            Token token(KEYWORD_IF, to_upper(keyword_if_match.str()), line, column);
            tokens.push_back(token);
            input = input.substr(keyword_if_match.length());
            column += keyword_if_match.length();
            continue;
        }
 
        smatch keyword_then_match;
        if (regex_search(input, keyword_then_match, keyword_then_regex)) {
            Token token(KEYWORD_THEN, to_upper(keyword_then_match.str()), line, column);
            tokens.push_back(token);
            input = input.substr(keyword_then_match.length());
            column += keyword_then_match.length();
            continue;
        }
 
        smatch keyword_else_match;
        if (regex_search(input, keyword_else_match, keyword_else_regex)) {
            Token token(KEYWORD_ELSE, to_upper(keyword_else_match.str()), line, column);
            tokens.push_back(token);
            input = input.substr(keyword_else_match.length());
            column += keyword_else_match.length();
            continue;
        }
 
        smatch assignment_operator_match;
        if (regex_search(input, assignment_operator_match, assignment_operator_regex)) {
            Token token(ASSIGNMENT_OPERATOR, assignment_operator_match.str(), line, column);
            tokens.push_back(token);
            input = input.substr(assignment_operator_match.length());
            column += assignment_operator_match.length();
            continue;
        }
        smatch equality_operator_match;
        if (regex_search(input, equality_operator_match, equality_operator_regex)) {
            Token token(EQUALITY_OPERATOR, equality_operator_match.str(), line, column);
            tokens.push_back(token);
            input = input.substr(equality_operator_match.length());
            column += equality_operator_match.length();
            continue;
        }
 
        // This code should be outside of the while loop
        // int main() {
        //     string input = "if a=a then a:=a else if aa then if aa then a:=a else a:=a";
        //     vector<Token> tokens = tokenize(input);
        //     for (Token token : tokens) {
        //         cout << token.value << " ";
        //     }
        //     cout << endl;
        //     Node* root = parse(tokens);
        //     printTree(root, 0);
        //     return 0;
        // }
    }
 
    return tokens;
}
Node* parse(vector<Token>& tokens) {
    Node* root = new Node("ROOT");
    Node* current_node = root;
 
    for (Token token : tokens) {
        Node* new_node = new Node(token.value);
        if (token.type == KEYWORD_IF) {
            current_node->children.push_back(new_node);
            current_node = new_node;
        }
        else if (token.type == KEYWORD_THEN) {
            current_node->children.push_back(new_node);
            Node* if_body_node = new Node("IF_BODY");
            current_node->children.push_back(if_body_node);
            current_node = if_body_node;
        }
        else if (token.type == KEYWORD_ELSE) {
            Node* parent_node = current_node;
            while (parent_node->value != "IF_BODY") {
                parent_node = parent_node->children[parent_node->children.size() - 2];
            }
            parent_node->children.push_back(new_node);
            Node* else_body_node = new Node("ELSE_BODY");
            parent_node->children.push_back(else_body_node);
            current_node = else_body_node;
        }
        else if (token.type == ASSIGNMENT_OPERATOR) {
            Node* parent_node = current_node;
            while (parent_node->value != "IF_BODY" && parent_node->value != "ELSE_BODY") {
                parent_node = parent_node->children[parent_node->children.size() - 2];
            }
            parent_node->children.push_back(new_node);
        }
        else {
            current_node->children.push_back(new_node);
        }
    }
 
    return root;
}
 
void printTree(Node* root, int level) {
    for (int i = 0; i < level; i++) {
        cout << "  ";
    }
    cout << root->value << endl;
    for (Node* child : root->children) {
        printTree(child, level + 1);
    }
}
 
int main() {
    string input = "if a=a then a:=a else if aa then if aa then a:=a else a:=a";
    vector<Token> tokens = tokenize(input);
    for (Token token : tokens) {
        cout << token.value << " ";
    }
    cout << endl;
    Node* root = parse(tokens);
    printTree(root, 0);
    return 0;
}

не работает вывод в консоли по моей задумке должно выводиться f a=a then a:=a else if aa then if aa then a:=a else a:=a
if
=
a
a
:=
a
a
if
aa
if
aa
:=
a
a
else
:=
a
a
а просто выводит пустую консоль ребят если не сложно помогите ибо я не понимаю в чём проблема
  • Вопрос задан
  • 64 просмотра
Подписаться 1 Простой 2 комментария
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы