SymbolTable, Hashtable with multiple values

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

SymbolTable, Hashtable with multiple values

peterxu422
The book suggested we use a Hashtable for the class and subroutine scopes for the SymbolTable class.

However, for each identifier(key) in SymbolTable, there are multiple values (kind, type, index) whereas the Hashtable in Java.util is only a key, value pair. My question is how would you incorporate multiple values for a single key in a hashtable?

One way I could think of accomplishing this is to create a new class called SymbolTableValues and just have the value in the hashtable be one of these objects with the appropriate data stored in them. But I thought I should avoid creating an extra class since it wasn't suggested in the book. I'm thinking there's another way to achieve this and was wondering if anyone had any suggestions.
Reply | Threaded
Open this post in threaded view
|

Re: SymbolTable, Hashtable with multiple values

cadet1620
Administrator
peterxu422 wrote
One way I could think of accomplishing this is to create a new class called SymbolTableValues and just have the value in the hashtable be one of these objects with the appropriate data stored in them. But I thought I should avoid creating an extra class since it wasn't suggested in the book. I'm thinking there's another way to achieve this and was wondering if anyone had any suggestions.
I look at this as an implementation detail that would be up to the programmer, not a part of the recommended architecture.

Using a class to contain the values, as you are thinking, is the classic way to do this sort of thing if your programming language supports it.

[You could avoid creating the SymbolTableValues object by having the hash table values be arrays, but that's just plain ugly and very error prone!]

--Mark