I am currently working on a compiler under C and I am abit lost at the part where we construct the data structure for AST, especially for the part where we construct stucture for IDs, it is called “Symbol Table Entry”
I see structures over net such as:
struct ste {
struct id *name; /* pointer into hash table for assoc. id */
struct decl *decl; /* pointer into symbol table for its decl */
struct ste *prev; /* pointer to previous entry in symbol table */
};
It looks like a linked list as it holds a pointer to the previous entry (*prev) but what is the logic behind this ?
The answer to your concrete question is this: the prev link means that, when your code has a pointer to one of these node, it can follow the link to the previous link in the chain. One reason a symbol table might have a list like this is to deal with nested scope:
However, there are many, many other reasons why the symbols nodes might want to be arranged in a list. Any reason why the compiler needs to visit all the nodes is a reason.