KickJava   Java API By Example, From Geeks To Geeks.

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


1 package ro.infoiasi.donald.compiler.parser;
2
3 import ro.infoiasi.donald.compiler.cfg.*;
4 import ro.infoiasi.donald.compiler.simple.*;
5 import java.util.*;
6
7 // a relation on the symbols of a CFG grammar
8
public class SymbolRelation {
9     public interface SymbolPair {
10         Symbol getFirst();
11         Symbol getSecond();
12     }
13
14     private CFG g;
15     private Relation r;
16
17     private class SymbolPairP implements SymbolPair {
18         private Symbol first;
19         private Symbol second;
20         public SymbolPairP(Symbol first, Symbol second) {
21             this.first = first;
22             this.second = second;
23         }
24         public SymbolPairP(Relation.IntPair ip) {
25             this.first = g.symbol(ip.getFirst());
26             this.second = g.symbol(ip.getSecond());
27             
28         }
29         public Symbol getFirst() {
30             return first;
31         }
32         
33         public Symbol getSecond() {
34             return second;
35         }
36         public String JavaDoc toString() {
37             return "("+first+","+second+")";
38         }
39     }
40     
41     SymbolRelation(CFG g) {
42         this.g = g;
43         r = new Relation(g.getNonTerminals().count()+g.getTerminals().count());
44     }
45
46     private SymbolRelation(CFG g, Relation r) {
47         this.g = g;
48         this.r = r;
49     }
50
51     public boolean contains(Symbol x, Symbol y) {
52         return r.contains(g.getSID(x), g.getSID(y));
53     }
54
55     public boolean add(Symbol x, Symbol y) {
56         return r.add(g.getSID(x), g.getSID(y));
57     }
58
59     public SymbolRelation inverse() {
60         return new SymbolRelation(g, r.inverse());
61     }
62
63     public SymbolRelation plus() {
64         return new SymbolRelation(g, r.transitiveClosure());
65     }
66
67     public SymbolRelation star() {
68         return new SymbolRelation(g, r.reflexiveTransitiveClosure());
69     }
70
71     public SymbolRelation product(SymbolRelation sr) {
72         return new SymbolRelation(g, Relation.product(r, sr.r));
73     }
74     
75     private class IteratorP implements Iterator {
76         private Iterator it = r.iterator();
77         public boolean hasNext() {
78             return it.hasNext();
79         }
80         public Object JavaDoc next() {
81             return new SymbolPairP((Relation.IntPair)it.next());
82         }
83         public void remove() {
84             it.remove();
85         }
86     }
87     
88     public Iterator iteratorSP() {
89         return new IteratorP();
90     }
91
92     public Iterator iteratorIP() {
93         return r.iterator();
94     }
95
96     public String JavaDoc toString() {
97         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
98         sb.append("{");
99         Iterator it = iteratorSP();
100         while (it.hasNext()) {
101             SymbolPair sp = (SymbolPair)it.next();
102             sb.append(sp.toString());
103         }
104         sb.append("}");
105         return sb.toString();
106     }
107 }
108
Popular Tags