lin
2025-08-21 57113df3a0e2be01232281fad9a5f2c060567981
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
 
package java_cup;
 
import java.util.BitSet;
 
/** A set of terminals implemented as a bitset. 
 * @version last updated: 11/25/95
 * @author  Scott Hudson
 */
public class terminal_set {
 
  /*-----------------------------------------------------------*/
  /*--- Constructor(s) ----------------------------------------*/
  /*-----------------------------------------------------------*/
 
  /** Constructor for an empty set. */
  public terminal_set() 
    { 
      /* allocate the bitset at what is probably the right size */
      _elements = new BitSet(terminal.number());
    };
 
  /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
 
  /** Constructor for cloning from another set. 
   * @param other the set we are cloning from.
   */
  public terminal_set(terminal_set other) 
    throws internal_error
    {
      not_null(other);
      _elements = (BitSet)other._elements.clone();
    };
 
  /*-----------------------------------------------------------*/
  /*--- (Access to) Static (Class) Variables ------------------*/
  /*-----------------------------------------------------------*/
 
  /** Constant for the empty set. */
  public static final terminal_set EMPTY = new terminal_set();
 
  /*-----------------------------------------------------------*/
  /*--- (Access to) Instance Variables ------------------------*/
  /*-----------------------------------------------------------*/
 
  /** Bitset to implement the actual set. */
  protected BitSet _elements;
 
  /*-----------------------------------------------------------*/
  /*--- General Methods ----------------------------------------*/
  /*-----------------------------------------------------------*/
 
  /** Helper function to test for a null object and throw an exception if
   *  one is found. 
   * @param obj the object we are testing.
   */
  protected void not_null(Object obj) throws internal_error
    {
      if (obj == null) 
    throw new internal_error("Null object used in set operation");
    }
 
  /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
 
  /** Determine if the set is empty. */
  public boolean empty()
    {
      return equals(EMPTY);
    }
 
  /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
 
  /** Determine if the set contains a particular terminal. 
   * @param sym the terminal symbol we are looking for.
   */
  public boolean contains(terminal sym) 
    throws internal_error
    {
      not_null(sym); 
      return _elements.get(sym.index());
    };
 
  /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
 
  /** Given its index determine if the set contains a particular terminal. 
   * @param indx the index of the terminal in question.
   */
  public boolean contains(int indx) 
    {
      return _elements.get(indx);
    };
 
  /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
 
  /** Determine if this set is an (improper) subset of another.
   * @param other the set we are testing against.
   */
  public boolean is_subset_of(terminal_set other)
    throws internal_error
    {
      not_null(other);
 
      /* make a copy of the other set */
      BitSet copy_other = (BitSet)other._elements.clone();
 
      /* and or in */
      copy_other.or(_elements);
 
      /* if it hasn't changed, we were a subset */
      return copy_other.equals(other._elements);
    }
 
  /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
 
  /** Determine if this set is an (improper) superset of another.
   * @param other the set we are testing against.
   */
  public boolean is_superset_of(terminal_set other)
    throws internal_error
    {
      not_null(other);
      return other.is_subset_of(this);
    }
 
  /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
 
  /** Add a single terminal to the set.  
   * @param sym the terminal being added.
   * @return true if this changes the set.
   */
  public boolean add(terminal sym) 
    throws internal_error
    {
      boolean result;
 
      not_null(sym); 
 
      /* see if we already have this */ 
      result = _elements.get(sym.index());
 
      /* if not we add it */
      if (!result)
    _elements.set(sym.index());
 
      return result;
    };
 
  /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
 
  /** Remove a terminal if it is in the set.
   * @param sym the terminal being removed.
   */
  public void remove(terminal sym) 
    throws internal_error
    {
      not_null(sym); 
      _elements.clear(sym.index());
    };
 
  /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
 
  /** Add (union) in a complete set.  
   * @param other the set being added.
   * @return true if this changes the set.
   */
  public boolean add(terminal_set other)
    throws internal_error
    {
      not_null(other);
 
      /* make a copy */
      BitSet copy = (BitSet)_elements.clone();
 
      /* or in the other set */
      _elements.or(other._elements);
 
      /* changed if we are not the same as the copy */
      return !_elements.equals(copy);
    };
 
  /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
 
  /** Determine if this set intersects another.
   * @param other the other set in question.
   */
   public boolean intersects(terminal_set other)
     throws internal_error
     {
       not_null(other);
 
       /* make a copy of the other set */
       BitSet copy = (BitSet)other._elements.clone();
 
       /* xor out our values */
       copy.xor(this._elements);
 
       /* see if its different */
       return !copy.equals(other._elements);
     }
 
  /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
 
  /** Equality comparison. */
  public boolean equals(terminal_set other)
    {
      if (other == null) 
    return false;
      else
    return _elements.equals(other._elements);
    }
 
  /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
 
  /** Generic equality comparison. */
  public boolean equals(Object other)
    {
      if (!(other instanceof terminal_set))
    return false;
      else
    return equals((terminal_set)other);
    }
 
  /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
 
  /** Convert to string. */
  public String toString()
    {
      String result;
      boolean comma_flag;
      
      result = "{";
      comma_flag = false;
      for (int t = 0; t < terminal.number(); t++)
    {
      if (_elements.get(t))
        {
          if (comma_flag)
            result += ", ";
          else
            comma_flag = true;
 
          result += terminal.find(t).name();
        }
    }
      result += "}";
 
      return result;
    }
 
  /*-----------------------------------------------------------*/
 
};