4 import java.util.Hashtable;
5 import java.util.Enumeration;
7 /** This class represents a terminal symbol in the grammar. Each terminal
8 * has a textual name, an index, and a string which indicates the type of
9 * object it will be implemented with at runtime (i.e. the class of object
10 * that will be returned by the scanner and pushed on the parse stack to
13 * @version last updated: 7/3/96
14 * @author Frank Flannery
16 public class terminal extends symbol {
18 /*-----------------------------------------------------------*/
19 /*--- Constructor(s) ----------------------------------------*/
20 /*-----------------------------------------------------------*/
23 * @param nm the name of the terminal.
24 * @param tp the type of the terminal.
26 public terminal(String nm, String tp, int precedence_side, int precedence_num)
28 /* superclass does most of the work */
31 /* add to set of all terminals and check for duplicates */
32 Object conflict = _all.put(nm,this);
34 // can't throw an execption here because this is used in static
35 // initializers, so we do a crash instead
37 // throw new internal_error("Duplicate terminal (" + nm + ") created");
38 (new internal_error("Duplicate terminal (" + nm + ") created")).crash();
40 /* assign a unique index */
41 _index = next_index++;
43 /* set the precedence */
44 _precedence_num = precedence_num;
45 _precedence_side = precedence_side;
47 /* add to by_index set */
48 _all_by_index.put(new Integer(_index), this);
51 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
53 /** Constructor for non-precedented terminal
56 public terminal(String nm, String tp)
58 this(nm, tp, assoc.no_prec, -1);
61 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
63 /** Constructor with default type.
64 * @param nm the name of the terminal.
66 public terminal(String nm)
71 /*-----------------------------------------------------------*/
72 /*------------------- Class Variables ---------------------*/
73 /*-----------------------------------------------------------*/
75 private int _precedence_num;
76 private int _precedence_side;
78 /*-----------------------------------------------------------*/
79 /*--- (Access to) Static (Class) Variables ------------------*/
80 /*-----------------------------------------------------------*/
82 /** Table of all terminals. Elements are stored using name strings as
85 protected static Hashtable _all = new Hashtable();
87 /** Access to all terminals. */
88 public static Enumeration all() {return _all.elements();}
90 /** Lookup a terminal by name string. */
91 public static terminal find(String with_name)
93 if (with_name == null)
96 return (terminal)_all.get(with_name);
100 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
102 /** Table of all terminals indexed by their index number. */
103 protected static Hashtable _all_by_index = new Hashtable();
105 /** Lookup a terminal by index. */
106 public static terminal find(int indx)
108 Integer the_indx = new Integer(indx);
110 return (terminal)_all_by_index.get(the_indx);
113 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
115 /** Total number of terminals. */
116 public static int number() {return _all.size();}
118 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
120 /** Static counter to assign unique index. */
121 protected static int next_index = 0;
123 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
125 /** Special terminal for end of input. */
126 public static final terminal EOF = new terminal("EOF");
128 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
130 /** special terminal used for error recovery */
131 public static final terminal error = new terminal("error");
133 /*-----------------------------------------------------------*/
134 /*--- General Methods ---------------------------------------*/
135 /*-----------------------------------------------------------*/
137 /** Report this symbol as not being a non-terminal. */
138 public boolean is_non_term()
143 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
145 /** Convert to a string. */
146 public String toString()
148 return super.toString() + "[" + index() + "]";
151 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
153 /** get the precedence of a terminal */
154 public int precedence_num() {
155 return _precedence_num;
157 public int precedence_side() {
158 return _precedence_side;
161 /** set the precedence of a terminal */
162 public void set_precedence(int p, int new_prec) {
163 _precedence_side = p;
164 _precedence_num = new_prec;
167 /*-----------------------------------------------------------*/