1 package com.sun.java_cup.internal; 2 3 import java.util.Stack ; 4 import java.util.Enumeration ; 5 6 32 public class lalr_item extends lr_item_core { 33 34 35 36 37 38 43 public lalr_item(production prod, int pos, terminal_set look) 44 throws internal_error 45 { 46 super(prod, pos); 47 _lookahead = look; 48 _propagate_items = new Stack (); 49 needs_propagation = true; 50 } 51 52 53 54 58 public lalr_item(production prod, terminal_set look) throws internal_error 59 { 60 this(prod,0,look); 61 } 62 63 64 65 68 public lalr_item(production prod) throws internal_error 69 { 70 this(prod,0,new terminal_set()); 71 } 72 73 74 75 76 77 78 protected terminal_set _lookahead; 79 80 81 public terminal_set lookahead() {return _lookahead;} 82 83 84 85 86 protected Stack _propagate_items; 87 88 89 public Stack propagate_items() {return _propagate_items;} 90 91 92 93 96 protected boolean needs_propagation; 97 98 99 100 101 public void add_propagate(lalr_item prop_to) 102 { 103 _propagate_items.push(prop_to); 104 needs_propagation = true; 105 } 106 107 108 109 110 111 115 public void propagate_lookaheads(terminal_set incoming) throws internal_error 116 { 117 boolean change = false; 118 119 120 if (!needs_propagation && (incoming == null || incoming.empty())) 121 return; 122 123 124 if (incoming != null) 125 { 126 127 change = lookahead().add(incoming); 128 } 129 130 131 if (change || needs_propagation) 132 { 133 134 needs_propagation = false; 135 136 137 for (int i = 0; i < propagate_items().size(); i++) 138 ((lalr_item)propagate_items().elementAt(i)) 139 .propagate_lookaheads(lookahead()); 140 } 141 } 142 143 144 145 148 public lalr_item shift() throws internal_error 149 { 150 lalr_item result; 151 152 153 if (dot_at_end()) 154 throw new internal_error("Attempt to shift past end of an lalr_item"); 155 156 157 result = new lalr_item(the_production(), dot_pos()+1, 158 new terminal_set(lookahead())); 159 160 161 add_propagate(result); 162 163 return result; 164 } 165 166 167 168 173 public terminal_set calc_lookahead(terminal_set lookahead_after) 174 throws internal_error 175 { 176 terminal_set result; 177 int pos; 178 production_part part; 179 symbol sym; 180 181 182 if (dot_at_end()) 183 throw new internal_error( 184 "Attempt to calculate a lookahead set with a completed item"); 185 186 187 result = new terminal_set(); 188 189 190 for (pos = dot_pos()+1; pos < the_production().rhs_length(); pos++) 191 { 192 part = the_production().rhs(pos); 193 194 195 if (!part.is_action()) 196 { 197 sym = ((symbol_part)part).the_symbol(); 198 199 200 if (!sym.is_non_term()) 201 { 202 result.add((terminal)sym); 203 return result; 204 } 205 else 206 { 207 208 result.add(((non_terminal)sym).first_set()); 209 210 211 if (!((non_terminal)sym).nullable()) 212 return result; 213 } 214 } 215 } 216 217 219 result.add(lookahead_after); 220 return result; 221 } 222 223 224 225 232 public boolean lookahead_visible() throws internal_error 233 { 234 production_part part; 235 symbol sym; 236 237 239 if (dot_at_end()) return true; 240 241 242 for (int pos = dot_pos() + 1; pos < the_production().rhs_length(); pos++) 243 { 244 part = the_production().rhs(pos); 245 246 247 if (!part.is_action()) 248 { 249 sym = ((symbol_part)part).the_symbol(); 250 251 252 if (!sym.is_non_term()) return false; 253 254 255 if (!((non_terminal)sym).nullable()) return false; 256 } 257 } 258 259 260 return true; 261 } 262 263 264 265 269 public boolean equals(lalr_item other) 270 { 271 if (other == null) return false; 272 return super.equals(other); 273 } 274 275 276 277 278 public boolean equals(Object other) 279 { 280 if (!(other instanceof lalr_item)) 281 return false; 282 else 283 return equals((lalr_item)other); 284 } 285 286 287 288 291 public int hashCode() 292 { 293 return super.hashCode(); 294 } 295 296 297 298 299 public String toString() 300 { 301 String result = ""; 302 303 result += "["; 306 result += super.toString(); 307 result += ", "; 308 if (lookahead() != null) 309 { 310 result += "{"; 311 for (int t = 0; t < terminal.number(); t++) 312 if (lookahead().contains(t)) 313 result += terminal.find(t).name() + " "; 314 result += "}"; 315 } 316 else 317 result += "NULL LOOKAHEAD!!"; 318 result += "]"; 319 320 327 return result; 328 } 329 330 } 331 | Popular Tags |