KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ppg > atoms > Production


1 package ppg.atoms;
2
3 import java.util.*;
4 import ppg.parse.*;
5 import ppg.util.*;
6
7 public class Production implements Unparse
8 {
9     private Nonterminal lhs;
10     private Vector rhs;
11     private static String JavaDoc HEADER = "ppg [nterm]: ";
12     
13     public Production (Nonterminal lhs, Vector rhs) {
14         this.lhs = lhs;
15         this.rhs = rhs;
16     }
17
18     public Nonterminal getLHS() { return lhs; }
19     public void setLHS(Nonterminal nt) { lhs = nt; }
20     public Vector getRHS() { return rhs; }
21     
22     public Object JavaDoc clone() {
23         return new Production((Nonterminal) lhs.clone(),
24                               (Vector) rhs.clone());
25     }
26     
27     public void drop (Production prod) {
28         //assertSameLHS(prod, "drop");
29
Vector toDrop = prod.getRHS();
30         // this is O(n^2)
31
Vector target, source;
32         for (int i=0; i < toDrop.size(); i++) {
33             target = (Vector) toDrop.elementAt(i);
34             for (int j=0; j < rhs.size(); j++) {
35                 source = (Vector) rhs.elementAt(j);
36                 if (isSameProduction(target, source)) {
37                     rhs.removeElementAt(j);
38                     break;
39                 }
40                 // production match not found
41
if (j == rhs.size() - 1) {
42                     System.err.println(HEADER+"no match found for production:");
43                     System.err.print(prod.getLHS() + " ::= ");
44                     for (int k=0; k < target.size(); k++) {
45                         System.err.print(target.elementAt(k)+" ");
46                     }
47                     System.exit(1);
48                 }
49             }
50         }
51     }
52     
53     public static boolean isSameProduction (Vector u, Vector v) {
54         int uIdx = 0, vIdx = 0;
55         GrammarPart ug = null, vg = null;
56
57         while (uIdx < u.size() && vIdx < v.size()) {
58             ug = (GrammarPart) u.elementAt(uIdx);
59             if (ug instanceof SemanticAction) {
60                 uIdx++;
61                 continue;
62             }
63
64             vg = (GrammarPart) v.elementAt(vIdx);
65             if (vg instanceof SemanticAction) {
66                 vIdx++;
67                 continue;
68             }
69             
70             if (! ug.equals(vg))
71                 return false;
72             else {
73                 uIdx++;
74                 vIdx++;
75             }
76         }
77         
78         if (uIdx == u.size() && vIdx == v.size()) {
79             // got through all the way, they are the same
80
return true;
81         } else {
82             // one of the lists was not seen all the way,
83
// must check that only semantic actions are left
84
if (uIdx < u.size()) {
85                 for (; uIdx < u.size(); uIdx++) {
86                     ug = (GrammarPart) u.elementAt(uIdx);
87                     if (! (ug instanceof SemanticAction))
88                         return false;
89                 }
90                 return true;
91             } else { // vIdx < v.size()
92
for (; vIdx < v.size(); vIdx++) {
93                     vg = (GrammarPart) v.elementAt(vIdx);
94                     if (! (vg instanceof SemanticAction))
95                         return false;
96                 }
97                 return true;
98             }
99         }
100     }
101     
102     public void union (Production prod) {
103         Vector additional = prod.getRHS();
104         union(additional);
105     }
106     
107     public void union (Vector prodList) {
108         Vector toAdd, current;
109         for (int i=0; i < prodList.size(); i++) {
110             toAdd = (Vector) prodList.elementAt(i);
111             for (int j=0; j < rhs.size(); j++) {
112                 current = (Vector) rhs.elementAt(i);
113                 if (isSameProduction(toAdd, current))
114                     break;
115                 if (j == rhs.size() - 1)
116                     rhs.addElement(toAdd);
117             }
118         }
119     }
120     
121     public void add (Production prod) {
122         //assertSameLHS(prod, "add");
123
Vector additional = prod.getRHS();
124         for (int i=0; i < additional.size(); i++) {
125             rhs.addElement( additional.elementAt(i) );
126         }
127     }
128     public void addToRHS (Vector rhsPart) {
129         rhs.addElement(rhsPart);
130     }
131     
132     private void assertSameLHS(Production prod, String JavaDoc function) {
133         if (! (prod.getLHS().equals(lhs)) ) {
134             System.err.println(HEADER + "nonterminals do not match in Production."+
135                                function + "(): current is "+lhs+", given: "+
136                                prod.getLHS());
137             System.exit(1);
138         }
139     }
140     public void unparse (CodeWriter cw) {
141         cw.begin(0);
142         cw.write(lhs.toString() + " ::=");
143         cw.allowBreak(3);
144         Vector rhs_part;
145         for (int i=0; i < rhs.size(); i++) {
146             rhs_part = (Vector) rhs.elementAt(i);
147             for (int j=0; j < rhs_part.size(); j++) {
148                 cw.write(" ");
149                 ((GrammarPart) rhs_part.elementAt(j)).unparse(cw);
150             }
151             if (i < rhs.size() - 1) {
152                 cw.allowBreak(0);
153                 cw.write(" | ");
154             }
155         }
156         cw.write(";");
157         cw.newline(); cw.newline();
158     
159         cw.end();
160     }
161     
162     public String JavaDoc toString() {
163         String JavaDoc result = lhs.toString();
164         Vector rhs_part;
165         result += " ::=";
166         for (int i=0; i < rhs.size(); i++) {
167             rhs_part = (Vector) rhs.elementAt(i);
168             for (int j=0; j < rhs_part.size(); j++) {
169                 result += " " + rhs_part.elementAt(j).toString();
170             }
171             if (i < rhs.size() - 1)
172                 result += " | ";
173         }
174         return result + ";";
175     }
176 }
177
Popular Tags