Can I use malloc to add symbol table entries? How do I traverse the table to check if something is already there?
In general, symbol tables are implemented through hash tables. Hash tables have the advantage of O(1) store and retrieve, but they don't store data sequentially.
Assuming you're working in C you can uses malloc()
, but it requires more work than that. The provided link should enlighten you.
A "symbol table" doesn't describe a particular kind of data structure. It merely describes the primary modes of operation: adding symbols and retrieving symbols by name. Symbols here are basically attributed names. For a compiler class, one such an attribute could be IsAFunction
.
C has very few built-in datastructures. You'd have to create one yourself in this case. In C++, it would just be a matter of a std::map<std::string, Attributes>
. Now presumably if you're in a compiler class, you should already know how to implement datastructures in C (including the use of malloc()
). If not, then a compiler class really isn't for you.
I'v done that with a double chained linked list before. But now i will definitively do it with a hashtable. It's just a datastructure.