1 8 9 package net.sourceforge.chaperon.build; 10 11 import net.sourceforge.chaperon.common.IntegerList; 12 import net.sourceforge.chaperon.model.grammar.Grammar; 13 import net.sourceforge.chaperon.model.symbol.SymbolSet; 14 15 import org.apache.commons.logging.Log; 16 17 import java.util.ArrayList ; 18 19 25 public class ItemSetCollection 26 { 27 private static final EndOfFile EOF = new EndOfFile(); 28 private static final int NOTCHANGED = 0; 29 private static final int CHANGED = 1; 30 private ArrayList itemsets = new ArrayList (); 31 private Grammar grammar; 32 private FirstSetCollection firstsets; 33 private SymbolSet tsymbols; 34 private SymbolSet ntsymbols; 35 private Log log; 36 37 44 public ItemSetCollection(Grammar grammar, FirstSetCollection firstsets) 45 { 46 this(grammar, firstsets, null); 47 } 48 49 57 public ItemSetCollection(Grammar grammar, FirstSetCollection firstsets, Log log) 58 { 59 this.grammar = grammar; 60 this.firstsets = firstsets; 61 this.log = log; 62 63 SymbolSet symbols = grammar.getSymbols(); 64 65 tsymbols = symbols.getTerminals(); 66 ntsymbols = symbols.getNonterminals(); 67 68 IntegerList changedState = new IntegerList(); 71 addItemSet(getStartItemSet()); 72 changedState.add(CHANGED); 73 74 boolean mustrepeat = false; 75 for (int i = 0; i<getItemSetCount(); i++) 76 { 77 changedState.set(i, NOTCHANGED); 78 79 ItemSet I = getItemSet(i); 80 81 SymbolSet nextnonterminals = I.getNextNonterminals(); 84 for (int j = 0; j<nextnonterminals.getSymbolCount(); j++) 85 { 86 ItemSet J = I.jump(nextnonterminals.getSymbol(j)); 87 88 if (!J.isEmpty()) 89 { 90 int index = indexOfCore(J); 91 if (index<0) { 93 index = addItemSet(J); 94 95 changedState.add(CHANGED); 96 } 97 else { 99 if (getItemSet(index).addItemSet(J)) { 101 if (index<changedState.getCount()) 102 changedState.set(index, CHANGED); 103 else 104 changedState.add(CHANGED); 105 106 if (index<=i) 108 109 mustrepeat = true; 111 } 112 } 113 114 I.setTransition(nextnonterminals.getSymbol(j), index); } 116 } 117 118 SymbolSet nextterminals = I.getNextTerminals(); 120 for (int j = 0; j<nextterminals.getSymbolCount(); j++) 121 { 122 ItemSet J = I.jump(nextterminals.getSymbol(j)); 123 124 if (!J.isEmpty()) 125 { 126 int index = indexOfCore(J); 127 if (index<0) { 129 index = addItemSet(J); 130 131 changedState.add(CHANGED); 132 } 133 else { 135 if (getItemSet(index).addItemSet(J)) { 137 if (index<changedState.getCount()) 138 changedState.set(index, CHANGED); 139 else 140 changedState.add(CHANGED); 141 142 if (index<=i) 144 145 mustrepeat = true; 147 } 148 } 149 150 I.setTransition(nextterminals.getSymbol(j), index); } 152 } 153 } 154 155 do 156 { 157 mustrepeat = false; 158 159 for (int i = 0; i<getItemSetCount(); i++) 160 if (changedState.get(i)==CHANGED) 161 { 162 changedState.set(i, NOTCHANGED); 163 164 ItemSet I = getItemSet(i); 165 166 symbols = I.getShiftSymbols(); 167 168 for (int j = 0; j<symbols.getSymbolCount(); j++) 169 { 170 ItemSet J = I.jump(symbols.getSymbol(j)); 171 int index = I.getTransition(symbols.getSymbol(j)); 172 173 if (getItemSet(index).addItemSet(J)) { 175 if (index<changedState.getCount()) 176 changedState.set(index, CHANGED); 177 else 178 changedState.add(CHANGED); 179 180 if (index<=i) 182 183 mustrepeat = true; 185 } 186 } 187 } 188 } 189 while (mustrepeat); } 191 192 197 private ItemSet getStartItemSet() 198 { 199 ItemSet I = new ItemSet(grammar, firstsets); 200 IntegerList startlist = grammar.getProductionList(grammar.getStartSymbol()); 201 202 for (int i = 0; i<startlist.getCount(); i++) 203 I.addItem(startlist.get(i), 0, EOF); 204 205 return I.closure(); 206 } 207 208 215 public int addItemSet(ItemSet itemset) 216 { 217 int index = indexOf(itemset); 218 219 if (index==-1) 220 { 221 itemsets.add(itemset); 222 index = itemsets.size()-1; 223 } 224 225 return index; 226 } 227 228 235 public ItemSet getItemSet(int index) 236 { 237 return (ItemSet)itemsets.get(index); 238 } 239 240 245 public int getItemSetCount() 246 { 247 return itemsets.size(); 248 } 249 250 258 public int indexOf(ItemSet itemset) 259 { 260 for (int i = 0; i<itemsets.size(); i++) 261 if (((ItemSet)itemsets.get(i)).equals(itemset)) 262 return i; 263 264 return -1; 265 } 266 267 274 public boolean contains(ItemSet itemset) 275 { 276 for (int i = 0; i<itemsets.size(); i++) 277 if (((ItemSet)itemsets.get(i)).equals(itemset)) 278 return true; 279 280 return false; 281 } 282 283 291 public int indexOfCore(ItemSet itemset) 292 { 293 for (int i = 0; i<itemsets.size(); i++) 294 if (((ItemSet)itemsets.get(i)).equalsCore(itemset)) 295 return i; 296 297 return -1; 298 } 299 300 307 public boolean containsCore(ItemSet itemset) 308 { 309 for (int i = 0; i<itemsets.size(); i++) 310 if (((ItemSet)itemsets.get(i)).equalsCore(itemset)) 311 return true; 312 313 return false; 314 } 315 316 321 public String toString() 322 { 323 StringBuffer buffer = new StringBuffer (); 324 325 for (int i = 0; i<getItemSetCount(); i++) 326 { 327 buffer.append("State "); 328 buffer.append(String.valueOf(i)); 329 buffer.append(":\n"); 330 buffer.append(getItemSet(i).toString()); 331 buffer.append("\n"); 332 } 333 334 return buffer.toString(); 335 } 336 } 337 | Popular Tags |