I have to write a symbol table for a simple grammar in Java and I'm not sure what data structure would best represent it. I thought about using a vector of vectors but it doesn't seem to be working out the way I want it to. Any suggestions?
Joined: Oct 23, 2002
I think it's hard to give you concrete advice on your problem, based on the information you provide.
I think the choice datastructure isn't that relevant in your case.. it just have to "hold" the different symbols.
it doesn't seem to be working out the way I want it to
Please be more specific..
Joined: Feb 20, 2007
In your case, if there are no synchronization issues, then Vector is not a good choice. Probably the fastest accessible data structures are Arrays, you can use an array of characters. Though, they have their own limitations. In that case, use an ArrayList.
But it would be much more easier to help if you could be more specific.
Joined: Oct 13, 2005
Having tried parsing grammars, I would suggest you don't write the symbol table at all. Find a tool like lex or flex (try the GNU website to download them, or get Fedora Linux on your PC and it includes lex and flex) or JFlex for scanning. I prefer JFlex. Get a parsing tool, eg yacc, BYacc, bison, BYacc/J, CUP (all the previous tools are LALR) or ANTLR, which is top-down.
If you wish to know about symbol tables, see if you can get your hands on J P Bennett Introduction to Compiling Techniques. A first course using ANSI C lex and yacc Hemel Hempstead: McGraw-Hill 1996, or any of the other standard compiler design books (Aho or Grune are two examples). Bennett uses an array, but collisions in the array are handled by putting the symbols into a linked list.
In my opinion the more modern tools written in OO languages (Java) are more elegant and easier to use than the old tools written in C. Also in Java one doesn't have to know how to write one's own data structures, only how to write "import java.util.*;" "Aho" misspelt.[/edit] [ April 26, 2007: Message edited by: Campbell Ritchie ]