I get the same data in struct containing a pointers array

  abstract-syntax-tree, c++, c++17, pointers

I’m currently creating an interpreter and I’m trying to create an ast tree.
I’m doing it in c++. And I’m also learning c++

So basicly, each node of the tree is a struct wich contains the number ‘n’ of children it posses, a list of node pointers and const char value :

typedef struct Node Node;
struct Node
{
    Node *children[256];
    const char *value = "default";
    int n = 0;
};

I also have a function that prints out the tree (as the tree command on linux) :

void ast_t_rep(Node *ast_t, int tab)
{
    string t;
    for (int i = 0; i < tab; ++i)
    {
        t += "│   ";
    }
    if (tab != 0)
    {
        t += "├── ";
    }
    printf("%s%sn", t.c_str(), ast_t->value);
    for (int i = 0; i < ast_t->n; ++i)
    {
        ast_t_rep(ast_t->children[i], tab + 1);
    }
}

As it go trough the source code written in the new language, if the is parenthesis/brackets, it gets the code inside the parenthesis/brackets and analyse it in a new node

so a code like that : 1 * (3 + {4 , 3})
shoud give something like that :

main
│   ├── 1
│   ├── *
│   ├── ()
│   │    ├── 3
│   │    ├── +
│   │    ├── {}
│   │    │   ├── 4
│   │    │   ├── ,
│   │    │   ├── 3

Sorry for this not very concise example but I want to make sure I am clear.

I created a recursive function for this purpose.

The lexeme vector is the list of bits of code like 3 or * or even ].

Node *create_ast(Node *root, vector<string> lexeme){
    if they are parenthesis
    [...] // takes the inside of the parenthesis
    Node *nw = add_child(ast_t);
    // Setting the name
    char *s = new char[strlen("bracket")+strlen(lexeme[index].c_str())+strlen(lexeme[index+index2].c_str())+1];
    strcpy(s,"bracket");
    strcat(s, lexeme[index].c_str());
    strcat(s, lexeme[index+index2].c_str());
    name = s;
    // Setting the value in the abstract syntax tree
    nw->value = name;
    nw = create_ast(bewteen, nw);
    
    else
    Node *ni = add_child(ast_t);
    ni->value = token; // the bit of code
}

The function I use to add a child

Node *add_child(Node *tree)
{
    Node *ne = new Node;
    tree->children[tree->n] = ne;
    //cout << tree->n << " " << tree->value << endl;
    tree->n += 1;
    return ne;
}

So this is what I created.

However, when I run the code, it replaces the content of the same length of a node with the last one it knows

For instance

(A + B)
[C + D]

outputs

[...]
│   ├── bracket()
│   │   ├── C
│   │   ├── +
│   │   ├── D
│   ├── bracket[]
│   │   ├── C
│   │   ├── +
│   │   ├── D

As I am learning c++ I don’t really know everything about pointers. I think the error comes from the fact I use a Node *list[256] and as I asigne some places of the memory one by one, I sometimes asigne the same pointer as a child for two nodes.

If you can help me, thank you.

I must apologies for my really bad english, but it is not my native language. Also I am learning c++, and my question can be really dumb.

Source: Windows Questions C++

LEAVE A COMMENT