01: package ro.infoiasi.donald.compiler.parser;
02:
03: import ro.infoiasi.donald.compiler.cfg.*;
04: import java.util.*;
05:
06: public class LALR1States {
07: private List statesByIndex = new ArrayList();
08: private Map statesByLR0Kernel = new HashMap();
09:
10: public int size() {
11: return statesByIndex.size();
12: }
13:
14: public Iterator iterator() {
15: return Collections.unmodifiableList(statesByIndex).iterator();
16: }
17:
18: public LALR1State find(int index) {
19: return (LALR1State) statesByIndex.get(index);
20: }
21:
22: public LALR1State find(Collection lr0kernel) {
23: return (LALR1State) statesByLR0Kernel.get(lr0kernel);
24: }
25:
26: public boolean add(LALR1State state) {
27: if (!statesByLR0Kernel.containsKey(state.getLR0KernelItems())) {
28: state.setIndex(statesByIndex.size());
29: statesByIndex.add(state);
30: statesByLR0Kernel.put(state.getLR0KernelItems(), 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: }
|