001: package java_cup;
002:
003: import java_cup.assoc;
004: import java.util.Hashtable;
005: import java.util.Enumeration;
006:
007: /** This class represents a terminal symbol in the grammar. Each terminal
008: * has a textual name, an index, and a string which indicates the type of
009: * object it will be implemented with at runtime (i.e. the class of object
010: * that will be returned by the scanner and pushed on the parse stack to
011: * represent it).
012: *
013: * @version last updated: 7/3/96
014: * @author Frank Flannery
015: */
016: public class terminal extends symbol {
017:
018: /*-----------------------------------------------------------*/
019: /*--- Constructor(s) ----------------------------------------*/
020: /*-----------------------------------------------------------*/
021:
022: /** Full constructor.
023: * @param nm the name of the terminal.
024: * @param tp the type of the terminal.
025: */
026: public terminal(String nm, String tp, int precedence_side,
027: int precedence_num) {
028: /* superclass does most of the work */
029: super (nm, tp);
030:
031: /* add to set of all terminals and check for duplicates */
032: Object conflict = _all.put(nm, this );
033: if (conflict != null)
034: // can't throw an execption here because this is used in static
035: // initializers, so we do a crash instead
036: // was:
037: // throw new internal_error("Duplicate terminal (" + nm + ") created");
038: (new internal_error("Duplicate terminal (" + nm
039: + ") created")).crash();
040:
041: /* assign a unique index */
042: _index = next_index++;
043:
044: /* set the precedence */
045: _precedence_num = precedence_num;
046: _precedence_side = precedence_side;
047:
048: /* add to by_index set */
049: _all_by_index.put(new Integer(_index), this );
050: }
051:
052: /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
053:
054: /** Constructor for non-precedented terminal
055: */
056:
057: public terminal(String nm, String tp) {
058: this (nm, tp, assoc.no_prec, -1);
059: }
060:
061: /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
062:
063: /** Constructor with default type.
064: * @param nm the name of the terminal.
065: */
066: public terminal(String nm) {
067: this (nm, null);
068: }
069:
070: /*-----------------------------------------------------------*/
071: /*------------------- Class Variables ---------------------*/
072: /*-----------------------------------------------------------*/
073:
074: private int _precedence_num;
075: private int _precedence_side;
076:
077: /*-----------------------------------------------------------*/
078: /*--- (Access to) Static (Class) Variables ------------------*/
079: /*-----------------------------------------------------------*/
080:
081: /** Table of all terminals. Elements are stored using name strings as
082: * the key
083: */
084: protected static Hashtable _all = new Hashtable();
085:
086: //Hm Added clear to clear all static fields
087: public static void clear() {
088: _all.clear();
089: _all_by_index.clear();
090: next_index = 0;
091: EOF = new terminal("EOF");
092: error = new terminal("error");
093: }
094:
095: /** Access to all terminals. */
096: public static Enumeration all() {
097: return _all.elements();
098: }
099:
100: /** Lookup a terminal by name string. */
101: public static terminal find(String with_name) {
102: if (with_name == null)
103: return null;
104: else
105: return (terminal) _all.get(with_name);
106: }
107:
108: /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
109:
110: /** Table of all terminals indexed by their index number. */
111: protected static Hashtable _all_by_index = new Hashtable();
112:
113: /** Lookup a terminal by index. */
114: public static terminal find(int indx) {
115: Integer the_indx = new Integer(indx);
116:
117: return (terminal) _all_by_index.get(the_indx);
118: }
119:
120: /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
121:
122: /** Total number of terminals. */
123: public static int number() {
124: return _all.size();
125: }
126:
127: /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
128:
129: /** Static counter to assign unique index. */
130: protected static int next_index = 0;
131:
132: /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
133:
134: /** Special terminal for end of input. */
135: public static terminal EOF = new terminal("EOF");
136:
137: /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
138:
139: /** special terminal used for error recovery */
140: public static terminal error = new terminal("error");
141:
142: /*-----------------------------------------------------------*/
143: /*--- General Methods ---------------------------------------*/
144: /*-----------------------------------------------------------*/
145:
146: /** Report this symbol as not being a non-terminal. */
147: public boolean is_non_term() {
148: return false;
149: }
150:
151: /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
152:
153: /** Convert to a string. */
154: public String toString() {
155: return super .toString() + "[" + index() + "]";
156: }
157:
158: /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
159:
160: /** get the precedence of a terminal */
161: public int precedence_num() {
162: return _precedence_num;
163: }
164:
165: public int precedence_side() {
166: return _precedence_side;
167: }
168:
169: /** set the precedence of a terminal */
170: public void set_precedence(int p, int new_prec) {
171: _precedence_side = p;
172: _precedence_num = new_prec;
173: }
174:
175: /*-----------------------------------------------------------*/
176:
177: }
|