4 /** The "core" of an LR item. This includes a production and the position
5 * of a marker (the "dot") within the production. Typically item cores
6 * are written using a production with an embedded "dot" to indicate their
7 * position. For example: <pre>
10 * This represents a point in a parse where the parser is trying to match
11 * the given production, and has succeeded in matching everything before the
12 * "dot" (and hence is expecting to see the symbols after the dot next). See
13 * lalr_item, lalr_item_set, and lalr_start for full details on the meaning
16 * @see java_cup.lalr_item
17 * @see java_cup.lalr_item_set
18 * @see java_cup.lalr_state
19 * @version last updated: 11/25/95
20 * @author Scott Hudson
23 public class lr_item_core {
25 /*-----------------------------------------------------------*/
26 /*--- Constructor(s) ----------------------------------------*/
27 /*-----------------------------------------------------------*/
30 * @param prod production this item uses.
31 * @param pos position of the "dot" within the item.
33 public lr_item_core(production prod, int pos) throws internal_error
35 symbol after_dot = null;
39 throw new internal_error(
40 "Attempt to create an lr_item_core with a null production");
42 _the_production = prod;
44 if (pos < 0 || pos > _the_production.rhs_length())
45 throw new internal_error(
46 "Attempt to create an lr_item_core with a bad dot position");
50 /* compute and cache hash code now */
51 _core_hash_cache = 13*_the_production.hashCode() + pos;
53 /* cache the symbol after the dot */
54 if (_dot_pos < _the_production.rhs_length())
56 part = _the_production.rhs(_dot_pos);
57 if (!part.is_action())
58 _symbol_after_dot = ((symbol_part)part).the_symbol();
62 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
64 /** Constructor for dot at start of right hand side.
65 * @param prod production this item uses.
67 public lr_item_core(production prod) throws internal_error
72 /*-----------------------------------------------------------*/
73 /*--- (Access to) Instance Variables ------------------------*/
74 /*-----------------------------------------------------------*/
76 /** The production for the item. */
77 protected production _the_production;
79 /** The production for the item. */
80 public production the_production() {return _the_production;}
82 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
84 /** The position of the "dot" -- this indicates the part of the production
85 * that the marker is before, so 0 indicates a dot at the beginning of
88 protected int _dot_pos;
90 /** The position of the "dot" -- this indicates the part of the production
91 * that the marker is before, so 0 indicates a dot at the beginning of
94 public int dot_pos() {return _dot_pos;}
96 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
98 /** Cache of the hash code. */
99 protected int _core_hash_cache;
101 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
103 /** Cache of symbol after the dot. */
104 protected symbol _symbol_after_dot = null;
106 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
108 /** Is the dot at the end of the production? */
109 public boolean dot_at_end()
111 return _dot_pos >= _the_production.rhs_length();
114 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
116 /** Return the symbol after the dot. If there is no symbol after the dot
118 public symbol symbol_after_dot()
120 /* use the cached symbol */
121 return _symbol_after_dot;
124 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
126 /** Determine if we have a dot before a non terminal, and if so which one
127 * (return null or the non terminal).
129 public non_terminal dot_before_nt()
133 /* get the symbol after the dot */
134 sym = symbol_after_dot();
136 /* if it exists and is a non terminal, return it */
137 if (sym != null && sym.is_non_term())
138 return (non_terminal)sym;
143 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
145 /** Produce a new lr_item_core that results from shifting the dot one
146 * position to the right.
148 public lr_item_core shift_core() throws internal_error
151 throw new internal_error(
152 "Attempt to shift past end of an lr_item_core");
154 return new lr_item_core(_the_production, _dot_pos+1);
157 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
159 /** Equality comparison for the core only. This is separate out because we
160 * need separate access in a super class.
162 public boolean core_equals(lr_item_core other)
164 return other != null &&
165 _the_production.equals(other._the_production) &&
166 _dot_pos == other._dot_pos;
169 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
171 /** Equality comparison. */
172 public boolean equals(lr_item_core other) {return core_equals(other);}
174 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
176 /** Generic equality comparison. */
177 public boolean equals(Object other)
179 if (!(other instanceof lr_item_core))
182 return equals((lr_item_core)other);
185 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
187 /** Hash code for the core (separated so we keep non overridden version). */
188 public int core_hashCode()
190 return _core_hash_cache;
193 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
195 /** Hash code for the item. */
196 public int hashCode()
198 return _core_hash_cache;
201 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
203 /** Return the hash code that object would have provided for us so we have
204 * a (nearly) unique id for debugging.
206 protected int obj_hash()
208 return super.hashCode();
211 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
213 /** Convert to a string (separated out from toString() so we can call it
214 * from subclass that overrides toString()).
216 public String to_simple_string() throws internal_error
219 production_part part;
221 if (_the_production.lhs() != null &&
222 _the_production.lhs().the_symbol() != null &&
223 _the_production.lhs().the_symbol().name() != null)
224 result = _the_production.lhs().the_symbol().name();
230 for (int i = 0; i<_the_production.rhs_length(); i++)
232 /* do we need the dot before this one? */
236 /* print the name of the part */
237 if (_the_production.rhs(i) == null)
239 result += "$$NULL$$ ";
243 part = _the_production.rhs(i);
245 result += "$$NULL$$ ";
246 else if (part.is_action())
247 result += "{ACTION} ";
248 else if (((symbol_part)part).the_symbol() != null &&
249 ((symbol_part)part).the_symbol().name() != null)
250 result += ((symbol_part)part).the_symbol().name() + " ";
252 result += "$$NULL$$ ";
256 /* put the dot after if needed */
257 if (_dot_pos == _the_production.rhs_length())
263 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
265 /** Convert to a string */
266 public String toString()
268 /* can't throw here since super class doesn't, so we crash instead */
270 return to_simple_string();
271 } catch(internal_error e) {
277 /*-----------------------------------------------------------*/