KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > java_cup > internal > terminal


1 package com.sun.java_cup.internal;
2
3 import com.sun.java_cup.internal.assoc;
4 import java.util.Hashtable JavaDoc;
5 import java.util.Enumeration JavaDoc;
6
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
11  * represent it).
12  *
13  * @version last updated: 7/3/96
14  * @author Frank Flannery
15  */

16 public class terminal extends symbol {
17
18   /*-----------------------------------------------------------*/
19   /*--- Constructor(s) ----------------------------------------*/
20   /*-----------------------------------------------------------*/
21
22   /** Full constructor.
23    * @param nm the name of the terminal.
24    * @param tp the type of the terminal.
25    */

26   public terminal(String JavaDoc nm, String JavaDoc tp, int precedence_side, int precedence_num)
27     {
28       /* superclass does most of the work */
29       super(nm, tp);
30
31       /* add to set of all terminals and check for duplicates */
32       Object JavaDoc conflict = _all.put(nm,this);
33       if (conflict != null)
34     // can't throw an execption here because this is used in static
35
// initializers, so we do a crash instead
36
// was:
37
// throw new internal_error("Duplicate terminal (" + nm + ") created");
38
(new internal_error("Duplicate terminal (" + nm + ") created")).crash();
39
40       /* assign a unique index */
41       _index = next_index++;
42
43       /* set the precedence */
44       _precedence_num = precedence_num;
45       _precedence_side = precedence_side;
46
47       /* add to by_index set */
48       _all_by_index.put(new Integer JavaDoc(_index), this);
49     }
50
51   /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
52
53   /** Constructor for non-precedented terminal
54     */

55
56   public terminal(String JavaDoc nm, String JavaDoc tp)
57     {
58       this(nm, tp, assoc.no_prec, -1);
59     }
60
61   /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
62
63   /** Constructor with default type.
64    * @param nm the name of the terminal.
65    */

66   public terminal(String JavaDoc nm)
67     {
68       this(nm, null);
69     }
70
71   /*-----------------------------------------------------------*/
72   /*------------------- Class Variables ---------------------*/
73   /*-----------------------------------------------------------*/
74
75   private int _precedence_num;
76   private int _precedence_side;
77
78   /*-----------------------------------------------------------*/
79   /*--- (Access to) Static (Class) Variables ------------------*/
80   /*-----------------------------------------------------------*/
81
82   /** Table of all terminals. Elements are stored using name strings as
83    * the key
84    */

85   protected static Hashtable JavaDoc _all = new Hashtable JavaDoc();
86
87   /** Access to all terminals. */
88   public static Enumeration JavaDoc all() {return _all.elements();}
89
90   /** Lookup a terminal by name string. */
91   public static terminal find(String JavaDoc with_name)
92     {
93       if (with_name == null)
94     return null;
95       else
96     return (terminal)_all.get(with_name);
97     }
98
99
100   /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
101
102   /** Table of all terminals indexed by their index number. */
103   protected static Hashtable JavaDoc _all_by_index = new Hashtable JavaDoc();
104
105   /** Lookup a terminal by index. */
106   public static terminal find(int indx)
107     {
108       Integer JavaDoc the_indx = new Integer JavaDoc(indx);
109
110       return (terminal)_all_by_index.get(the_indx);
111     }
112
113   /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
114
115   /** Total number of terminals. */
116   public static int number() {return _all.size();}
117
118   /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
119  
120   /** Static counter to assign unique index. */
121   protected static int next_index = 0;
122
123   /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
124
125   /** Special terminal for end of input. */
126   public static final terminal EOF = new terminal("EOF");
127
128   /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
129
130   /** special terminal used for error recovery */
131   public static final terminal error = new terminal("error");
132
133   /*-----------------------------------------------------------*/
134   /*--- General Methods ---------------------------------------*/
135   /*-----------------------------------------------------------*/
136
137   /** Report this symbol as not being a non-terminal. */
138   public boolean is_non_term()
139     {
140       return false;
141     }
142
143   /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
144
145   /** Convert to a string. */
146   public String JavaDoc toString()
147     {
148       return super.toString() + "[" + index() + "]";
149     }
150
151   /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
152
153   /** get the precedence of a terminal */
154   public int precedence_num() {
155     return _precedence_num;
156   }
157   public int precedence_side() {
158     return _precedence_side;
159   }
160
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;
165   }
166
167   /*-----------------------------------------------------------*/
168
169 }
170
Popular Tags