1 package ro.infoiasi.donald.compiler.parser; 2 3 import ro.infoiasi.donald.compiler.cfg.*; 4 import java.util.*; 5 6 public class LR1States { 7 private List statesByIndex = new ArrayList(); 8 private Map statesByKernel = new HashMap(); 9 10 public int size() { 11 return statesByIndex.size(); 12 } 13 14 public Iterator iterator() { 15 return Collections.unmodifiableList(statesByIndex).iterator(); 16 } 17 18 public LR1State find(int index) { 19 return (LR1State)statesByIndex.get(index); 20 } 21 22 public LR1State find(Collection kernel) { 23 return (LR1State)statesByKernel.get(kernel); 24 } 25 26 public boolean add(LR1State state) { 27 if (!statesByKernel.containsKey(state.getKernelItems())) { 28 state.setIndex(statesByIndex.size()); 29 statesByIndex.add(state); 30 statesByKernel.put(state.getKernelItems(), state); 31 return false; 32 } else { 33 return true; 34 } 35 } 36 37 public String toString() { 38 StringBuffer sb = new StringBuffer (); 39 for (int i = 0; i<size(); i++) { 40 sb.append(i+": "+find(i)+"\n"); 41 } 42 return new String (sb); 43 } 44 } 45 | Popular Tags |