KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ro > infoiasi > donald > compiler > parser > LR0State


1 package ro.infoiasi.donald.compiler.parser;
2
3 import ro.infoiasi.donald.compiler.cfg.*;
4 import java.util.*;
5
6 public class LR0State {
7     private int index;
8     private Set kernel = new LinkedHashSet();
9     private Set closure = new LinkedHashSet();
10     private Map itemsByNext = new LinkedHashMap();
11
12
13     public boolean equals(Object JavaDoc o) {
14         return (o instanceof LR0State &&
15             kernel.equals(((LR0State)o).kernel) &&
16             closure.equals(((LR0State)o).closure));
17     }
18
19     public int hashCode() {
20         return 17+kernel.hashCode()*37+closure.hashCode();
21     }
22
23     public boolean isEmpty() {
24         return kernel.isEmpty();
25     }
26
27     private void addToItemsByNext(LR0Item item) {
28         if (!item.isComplete()) {
29             Symbol sym = item.getNextSymbol();
30             LinkedList itemList;
31             if (itemsByNext.containsKey(sym)) {
32                 itemList = (LinkedList)itemsByNext.get(sym);
33             } else {
34                 itemList = new LinkedList();
35             }
36             itemList.add(item);
37             itemsByNext.put(sym, itemList);
38         }
39     }
40
41     public boolean addKernelItem(LR0Item item) {
42         if (kernel.add(item)) {
43             addToItemsByNext(item);
44             return true;
45         } else {
46             return false;
47         }
48     }
49
50     public Iterator iterator() {
51         return new Iterator() {
52             Iterator it = kernel.iterator();
53             boolean inKernel = true;
54             
55             public boolean hasNext() {
56                 if (it.hasNext() || (inKernel && !closure.isEmpty())) {
57                     return true;
58                 } else {
59                     return false;
60                 }
61             }
62             
63             public Object JavaDoc next() {
64                 if (inKernel && !it.hasNext()) {
65                     it = closure.iterator();
66                     inKernel = false;
67                 }
68                 return it.next();
69             }
70             
71             public void remove() {
72                 throw new UnsupportedOperationException JavaDoc();
73             }
74         };
75     }
76
77     public LinkedList find(Symbol x) {
78         return (LinkedList)itemsByNext.get(x);
79     }
80
81     public Iterator iterator(Symbol x) {
82         List itemList = find(x);
83         if (itemList == null) {
84             // iterator over the empty list
85
return new Iterator() {
86                 public boolean hasNext() { return false; }
87                 public Object JavaDoc next() { throw new NoSuchElementException(); }
88                 public void remove() { throw new UnsupportedOperationException JavaDoc(); }
89             };
90         } else {
91             return Collections.unmodifiableList(itemList).iterator();
92         }
93     }
94
95     public boolean contains(LR0Item item) {
96         return kernel.contains(item) || closure.contains(item);
97     }
98
99     public void closure(Productions p) {
100         LinkedList queue = new LinkedList();
101         queue.addAll(kernel);
102
103         while (!queue.isEmpty()) {
104             LR0Item item = (LR0Item)queue.removeFirst();
105             if (!item.isComplete()) {
106                 Symbol sym = (Symbol)item.getNextSymbol();
107                 if (!sym.isTerminal()) {
108                     List prodList = p.find((NonTerminal)sym);
109                     Iterator pIt = prodList.iterator();
110                     while (pIt.hasNext()) {
111                         Production prod = (Production)pIt.next();
112                         LR0Item newItem = new LR0Item(prod);
113                         if (closure.add(newItem)) {
114                             addToItemsByNext(newItem);
115                             queue.addLast(newItem);
116                         }
117                     }
118                 }
119             }
120         }
121     }
122
123     void setIndex(int index) {
124         this.index = index;
125     }
126
127     public int getIndex() {
128         return index;
129     }
130
131     public Collection getKernelItems() {
132         return kernel;
133     }
134
135     public Collection getClosureItems() {
136         return closure;
137     }
138
139     public String JavaDoc toString() {
140         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
141         sb.append("{");
142         Iterator it = kernel.iterator();
143         while (it.hasNext()) {
144             sb.append(it.next());
145             if (it.hasNext()) {
146                 sb.append(", ");
147             }
148         }
149         sb.append("; ");
150         it = closure.iterator();
151         while (it.hasNext()) {
152             sb.append(it.next());
153             if (it.hasNext()) {
154                 sb.append(", ");
155             }
156         }
157         sb.append("}");
158         return new String JavaDoc(sb);
159     }
160 }
161
Popular Tags