1 package com.sun.java_cup.internal; 2 3 import java.util.Hashtable ; 4 import java.util.Enumeration ; 5 6 14 15 public class non_terminal extends symbol { 16 17 18 19 20 21 25 public non_terminal(String nm, String tp) 26 { 27 28 super(nm, tp); 29 30 31 Object conflict = _all.put(nm,this); 32 if (conflict != null) 33 (new internal_error("Duplicate non-terminal ("+nm+") created")).crash(); 38 39 40 _index = next_index++; 41 42 43 _all_by_index.put(new Integer (_index), this); 44 } 45 46 47 48 51 public non_terminal(String nm) 52 { 53 this(nm, null); 54 } 55 56 57 58 59 60 63 protected static Hashtable _all = new Hashtable (); 64 65 66 public static Enumeration all() {return _all.elements();} 67 68 69 public static non_terminal find(String with_name) 70 { 71 if (with_name == null) 72 return null; 73 else 74 return (non_terminal)_all.get(with_name); 75 } 76 77 78 79 80 protected static Hashtable _all_by_index = new Hashtable (); 81 82 83 public static non_terminal find(int indx) 84 { 85 Integer the_indx = new Integer (indx); 86 87 return (non_terminal)_all_by_index.get(the_indx); 88 } 89 90 91 92 93 public static int number() {return _all.size();} 94 95 96 97 98 protected static int next_index = 0; 99 100 101 102 103 static protected int next_nt = 0; 104 105 106 107 108 public static final non_terminal START_nt = new non_terminal("$START"); 109 110 111 112 113 public boolean is_embedded_action = false; 114 115 116 117 118 119 123 static non_terminal create_new(String prefix) throws internal_error 124 { 125 if (prefix == null) prefix = "NT$"; 126 return new non_terminal(prefix + next_nt++); 127 } 128 129 130 131 132 static non_terminal create_new() throws internal_error 133 { 134 return create_new(null); 135 } 136 137 138 139 140 public static void compute_nullability() throws internal_error 141 { 142 boolean change = true; 143 non_terminal nt; 144 Enumeration e; 145 production prod; 146 147 148 while (change) 149 { 150 151 change = false; 152 153 154 for (e=all(); e.hasMoreElements(); ) 155 { 156 nt = (non_terminal)e.nextElement(); 157 158 159 if (!nt.nullable()) 160 { 161 if (nt.looks_nullable()) 162 { 163 nt._nullable = true; 164 change = true; 165 } 166 } 167 } 168 } 169 170 171 for (e=production.all(); e.hasMoreElements(); ) 172 { 173 prod = (production)e.nextElement(); 174 prod.set_nullable(prod.check_nullable()); 175 } 176 } 177 178 179 180 183 public static void compute_first_sets() throws internal_error 184 { 185 boolean change = true; 186 Enumeration n; 187 Enumeration p; 188 non_terminal nt; 189 production prod; 190 terminal_set prod_first; 191 192 193 while (change) 194 { 195 196 change = false; 197 198 199 for (n = all(); n.hasMoreElements(); ) 200 { 201 nt = (non_terminal)n.nextElement(); 202 203 204 for (p = nt.productions(); p.hasMoreElements(); ) 205 { 206 prod = (production)p.nextElement(); 207 208 209 prod_first = prod.check_first_set(); 210 211 212 if (!prod_first.is_subset_of(nt._first_set)) 213 { 214 change = true; 215 nt._first_set.add(prod_first); 216 } 217 } 218 } 219 } 220 } 221 222 223 224 225 226 227 protected Hashtable _productions = new Hashtable (11); 228 229 230 public Enumeration productions() {return _productions.elements();} 231 232 233 234 235 public int num_productions() {return _productions.size();} 236 237 238 239 240 public void add_production(production prod) throws internal_error 241 { 242 243 if (prod == null || prod.lhs() == null || prod.lhs().the_symbol() != this) 244 throw new internal_error( 245 "Attempt to add invalid production to non terminal production table"); 246 247 248 _productions.put(prod,prod); 249 } 250 251 252 253 254 protected boolean _nullable; 255 256 257 public boolean nullable() {return _nullable;} 258 259 260 261 262 protected terminal_set _first_set = new terminal_set(); 263 264 265 public terminal_set first_set() {return _first_set;} 266 267 268 269 270 271 272 public boolean is_non_term() 273 { 274 return true; 275 } 276 277 278 279 280 protected boolean looks_nullable() throws internal_error 281 { 282 283 for (Enumeration e = productions(); e.hasMoreElements(); ) 284 285 if (((production)e.nextElement()).check_nullable()) 286 return true; 287 288 289 return false; 290 } 291 292 293 294 295 public String toString() 296 { 297 return super.toString() + "[" + index() + "]" + (nullable() ? "*" : ""); 298 } 299 300 301 } 302 | Popular Tags |