home *** CD-ROM | disk | FTP | other *** search
/ ftp.ee.pdx.edu / 2014.02.ftp.ee.pdx.edu.tar / ftp.ee.pdx.edu / pub / users / Harry / compilers / p2 / StringTable.java < prev    next >
Text File  |  2005-10-11  |  3KB  |  115 lines

  1. // -------------------------------- StringTable --------------------------------
  2. //
  3. // This class maintains an collection of entries.  Each entry contains a key and
  4. // a value.  The key is a String and the value is an integer.  Each instance of
  5. // this class is an entry, containing both a key and value.  The entries are all
  6. // kept in a HashMap, which maps keys to the corresponnding entry.  This HashMap
  7. // is kept as a static variable.
  8. //
  9. // Harry Porter -- 01/15/03
  10. //
  11.  
  12. import java.util.*;
  13.  
  14. class StringTable {
  15.  
  16.     //
  17.     // Class Variables
  18.     //
  19.     static private Map map = new HashMap ();
  20.  
  21.     //
  22.     // Fields
  23.     //
  24.     private String str;          // The key
  25.     private int token;           // The value
  26.  
  27.  
  28.     //
  29.     //  Constructor
  30.     //
  31.     StringTable (String s, int t) {
  32.         str = s;
  33.         token = (byte) t;
  34.     }
  35.  
  36.  
  37.     //
  38.     // insert (str, tokenType)
  39.     //
  40.     // This method inserts a new entry mapping String "str" to value "tokenType".
  41.     //
  42.     static void insert (String s, int tokenType) {
  43.         StringTable entry = new StringTable (s, tokenType);
  44.         map.put (s, entry);
  45.     }
  46.  
  47.  
  48.     //
  49.     // lookupToken (str) --> tokenType
  50.     //
  51.     // This method looks string "str" up and returns the corresponding token
  52.     // type.  It returns -1 if not found.
  53.     //
  54.     static int lookupToken (String s) {
  55.         StringTable entry = (StringTable) map.get (s);
  56.         if (entry == null) {
  57.             return -1;
  58.         } else {
  59.             return entry.token;
  60.         }
  61.     }
  62.  
  63.  
  64.     //
  65.     // lookupString (str) --> str
  66.     //
  67.     // This method looks string "str" up and returns the cannonical (unique)
  68.     // string.  It throws NullPointerException if "str" is not found.
  69.     //
  70.     static String lookupString (String s) {
  71.         StringTable entry = (StringTable) map.get (s);
  72.         return entry.str;
  73.     }
  74.  
  75.  
  76.     //
  77.     // init ()
  78.     //
  79.     // This method initialize the string table, by adding the keywords to it.
  80.     //
  81.     static void init () {
  82.         insert ("and",       Token.AND);
  83.         insert ("array",     Token.ARRAY);
  84.         insert ("begin",     Token.BEGIN);
  85.         insert ("by",        Token.BY);
  86.         insert ("div",       Token.DIV);
  87.         insert ("do",        Token.DO);
  88.         insert ("else",      Token.ELSE);
  89.         insert ("elseif",    Token.ELSEIF);
  90.         insert ("end",       Token.END);
  91.         insert ("exit",      Token.EXIT);
  92.         insert ("for",       Token.FOR);
  93.         insert ("if",        Token.IF);
  94.         insert ("is",        Token.IS);
  95.         insert ("loop",      Token.LOOP);
  96.         insert ("mod",       Token.MOD);
  97.         insert ("not",       Token.NOT);
  98.         insert ("of",        Token.OF);
  99.         insert ("or",        Token.OR);
  100.         insert ("procedure", Token.PROCEDURE);
  101.         insert ("program",   Token.PROGRAM);
  102.         insert ("read",      Token.READ);
  103.         insert ("record",    Token.RECORD);
  104.         insert ("return",    Token.RETURN);
  105.         insert ("then",      Token.THEN);
  106.         insert ("to",        Token.TO);
  107.         insert ("type",      Token.TYPE);
  108.         insert ("var",       Token.VAR);
  109.         insert ("while",     Token.WHILE);
  110.         insert ("write",     Token.WRITE);
  111.  
  112.     }
  113.  
  114. }
  115.