Link to home
Start Free TrialLog in
Avatar of Irfan_Mohamed
Irfan_Mohamed

asked on

Java Lexical Analyser

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;
      }
   }
}
ASKER CERTIFIED SOLUTION
Avatar of randyd
randyd

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CleanupPing
CleanupPing

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.
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