Hello, I'm new to this forum, so let me know if there's a better place for this post.
I'm working on writing a compiler from scratch, following (to some extent) the course at
https://lagunita.stanford.edu/cou...gineering/Compilers/Fall2014/info.
I've gotten to the part where I'm constructing an abstract syntax tree and I'm having dificulty figuring out how to construct the data structure in a HMH way. Each node of the tree is a Token that can have any number of Children nodes.
I've decided on one of 2 options for the child pointer:
1. A pointer to an array of Token pointers
2. A pointer to a linked list of Tokens
My question is how to allocate memory for these scenarios:
1. In this case the only solution I can think of is to call realloc() every time a node gets added (or to do it less fequently by mainting a capacity and doubling when needed.) The problem is most nodes will only have 1 or 2 children, but some can have 100s or more. Perhaps the most HMH way to do this would be to write my own version of realloc?
2. This way seems pretty straight forward to allocate memory for: An array of Tokens and pointers address the children and siblings. I didn't particularly like this option because seeking through the linked list might not be ideal performance-wise.
What are the benefits or drawbacks of these ideas? Is there other ways that I haven't thought of?
And if there's a HMH episode where he does something similar, or any other resources, post a link (I'm still not very far in the series).