Java Lexical Analyser

Irfan_Mohamed
Irfan_Mohamed used Ask the Experts™
on
hi

just wanted to know how modify this code so that different identifiers are recorded iin the symbol table.
any commensts, advice would be appreciated

import java.util.*;

class SymbolTable
{
   private Vector symbolTableEntries;

   public SymbolTable()
   {
      symbolTableEntries = new Vector();  // create "empty" vector
   }
   
   public int length()
   {
      /*
         Return the number of entries in the Symbol Table.
      */
      return symbolTableEntries.size();
   }
   
   public int lookupIdentifier(String id)
   {
      /*
         Return index of entry in the symbol table for identifier id.  If the identifier
         is not already in the table, it is added
      */
     
      // search existing entries and return if identifier found
      for (int i=0; i<symbolTableEntries.size(); i++)
         if ( ( (SymbolTableEntry) symbolTableEntries.elementAt(i) ).idName.equals(id)  )
            return i;
         
      // name must not be in the table, so add it
      SymbolTableEntry entry = new SymbolTableEntry(id);  
      symbolTableEntries.add(entry);
      return symbolTableEntries.size()-1;
   }
     
   public String getIdentifier(int index)
   {
      /*
         Return the identifier at position index of the symbol table, or
         null if invalid index.
      */
      return ( (SymbolTableEntry) symbolTableEntries.elementAt(index) ).idName;
   }

   class SymbolTableEntry
   {
      String idName;
     
      public SymbolTableEntry(String s)
      {
         idName = s;
      }
   }
}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
someone else is running a loop to populate the entries.  this is where you would query from a database or load each entry from something hardcoded.

this is the loop that you would change to add different things to the vector, nothing here needs to be modified.

randy
Commented:
Why don't you use a java.util.Hashtable for Storing this data

Hashtable symbols = new Hashtable();
SymbolTableEntry getSymbol (String symbolname) {
  SymbolTableEntry entry = (SymbolTableEntry ) symbols.get(symbolname);
  if (entry == null) {
    entry = new SymbolTableEntry (symbolname);
    symbols.put(symbolname,entry);
  }
  return entry;
}

This is faster than scannin your Vector
Irfan_Mohamed:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.

Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Split points between randyd and csar

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venabili
EE Cleanup Volunteer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial