1 package java_cup; 2 3 import java.util.Enumeration; 4 import java.util.Hashtable; 5 6 /** This class represents a terminal symbol in the grammar. Each terminal 7 * has a textual name, an index, and a string which indicates the type of 8 * object it will be implemented with at runtime (i.e. the class of object 9 * that will be returned by the scanner and pushed on the parse stack to 10 * represent it). 11 * 12 * @version last updated: 11/25/95 13 * @author Scott Hudson 14 */ 15 public class terminal extends symbol { 16 17 /*-----------------------------------------------------------*/ 18 /*--- Constructor(s) ----------------------------------------*/ 19 /*-----------------------------------------------------------*/ 20 21 /** Full constructor. 22 * @param nm the name of the terminal. 23 * @param tp the type of the terminal. 24 */ terminal(String nm, String tp)25 public terminal(String nm, String tp) 26 { 27 /* superclass does most of the work */ 28 super(nm, tp); 29 30 /* add to set of all terminals and check for duplicates */ 31 Object conflict = _all.put(nm,this); 32 if (conflict != null) 33 // can't throw an execption here because this is used in static 34 // initializers, so we do a crash instead 35 // was: 36 // throw new internal_error("Duplicate terminal (" + nm + ") created"); 37 (new internal_error("Duplicate terminal (" + nm + ") created")).crash(); 38 39 /* assign a unique index */ 40 _index = next_index++; 41 42 /* add to by_index set */ 43 _all_by_index.put(new Integer(_index), this); 44 } 45 46 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/ 47 48 /** Constructor with default type. 49 * @param nm the name of the terminal. 50 */ terminal(String nm)51 public terminal(String nm) 52 { 53 this(nm, null); 54 } 55 56 /*-----------------------------------------------------------*/ 57 /*--- (Access to) Static (Class) Variables ------------------*/ 58 /*-----------------------------------------------------------*/ 59 60 /** Table of all terminals. Elements are stored using name strings as 61 * the key 62 */ 63 protected static Hashtable _all = new Hashtable(); 64 65 /** Access to all terminals. */ all()66 public static Enumeration all() {return _all.elements();}; 67 68 /** Lookup a terminal by name string. */ find(String with_name)69 public static terminal find(String with_name) 70 { 71 if (with_name == null) 72 return null; 73 else 74 return (terminal)_all.get(with_name); 75 } 76 77 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/ 78 79 /** Table of all terminals indexed by their index number. */ 80 protected static Hashtable _all_by_index = new Hashtable(); 81 82 /** Lookup a terminal by index. */ find(int indx)83 public static terminal find(int indx) 84 { 85 Integer the_indx = new Integer(indx); 86 87 return (terminal)_all_by_index.get(the_indx); 88 } 89 90 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/ 91 92 /** Total number of terminals. */ number()93 public static int number() {return _all.size();}; 94 95 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/ 96 97 /** Static counter to assign unique index. */ 98 protected static int next_index = 0; 99 100 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/ 101 102 /** Special terminal for end of input. */ 103 public static final terminal EOF = new terminal("EOF"); 104 105 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/ 106 107 /** special terminal used for error recovery */ 108 public static final terminal error = new terminal("error"); 109 110 /*-----------------------------------------------------------*/ 111 /*--- General Methods ---------------------------------------*/ 112 /*-----------------------------------------------------------*/ 113 114 /** Report this symbol as not being a non-terminal. */ is_non_term()115 public boolean is_non_term() 116 { 117 return false; 118 } 119 120 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/ 121 122 /** Convert to a string. */ toString()123 public String toString() 124 { 125 return super.toString() + "[" + index() + "]"; 126 } 127 128 /*-----------------------------------------------------------*/ 129 130 }; 131