KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > languages > parser > AnalyserAnalyser


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.languages.parser;
21
22 import org.netbeans.api.languages.ASTToken;
23 import java.io.PrintWriter JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.LinkedList JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Set JavaDoc;
33 import org.netbeans.modules.languages.parser.LLSyntaxAnalyser.Rule;
34 import org.netbeans.modules.languages.parser.LLSyntaxAnalyser.T;
35
36 /**
37  *
38  * @author Jan Jancura
39  */

40 public class AnalyserAnalyser {
41     
42     public static void printRules (List JavaDoc rules, PrintWriter JavaDoc writer) {
43         if (writer == null)
44             System.out.println ("Rules:");
45         else
46             writer.println ("Rules:");
47         List JavaDoc l = new ArrayList JavaDoc ();
48         Map JavaDoc m = new HashMap JavaDoc ();
49         Map JavaDoc mm = new HashMap JavaDoc ();
50         int i = 0;
51         Iterator JavaDoc it = rules.iterator ();
52         while (it.hasNext ()) {
53             Rule r = (Rule) it.next ();
54             if (!m.containsKey (r.getNT ()))
55                 l.add (r.getNT ());
56             List JavaDoc ll = (List JavaDoc) m.get (r.getNT ());
57             if (ll == null) {
58                 ll = new ArrayList JavaDoc ();
59                 m.put (r.getNT (), ll);
60                 mm.put (r.getNT (), new ArrayList JavaDoc ());
61             }
62             ll.add (r);
63             ((List JavaDoc) mm.get (r.getNT ())).add (new Integer JavaDoc (i++));
64         }
65         Collections.sort (l);
66         it = l.iterator ();
67         while (it.hasNext ()) {
68             String JavaDoc nt = (String JavaDoc) it.next ();
69             List JavaDoc ll = (List JavaDoc) m.get (nt);
70             Iterator JavaDoc it2 = ll.iterator ();
71             Iterator JavaDoc it3 = ((List JavaDoc) mm.get (nt)).iterator ();
72             while (it2.hasNext ())
73                 if (writer == null)
74                     System.out.println (" " + it2.next () + " (" + it3.next () + ")");
75                 else
76                     writer.println (" " + it2.next () + " (" + it3.next () + ")");
77         }
78         if (writer == null)
79             System.out.println ("");
80         else
81             writer.println ("");
82     }
83     
84     public static void printUndefinedNTs (List JavaDoc rules, PrintWriter JavaDoc writer) {
85         Set JavaDoc f = new HashSet JavaDoc ();
86         Iterator JavaDoc it = rules.iterator ();
87         while (it.hasNext ())
88             f.add (((Rule) it.next ()).getNT ());
89         Set JavaDoc result = new HashSet JavaDoc ();
90         it = rules.iterator ();
91         while (it.hasNext ()) {
92             Rule r = (Rule) it.next ();
93             Iterator JavaDoc it2 = r.getRight ().iterator ();
94             while (it2.hasNext ()) {
95                 Object JavaDoc e = it2.next ();
96                 if (e instanceof ASTToken) continue;
97                 if (e instanceof T && !f.contains (e))
98                     result.add (e);
99             }
100         }
101         if (result.isEmpty ()) return;
102         if (writer == null)
103             System.out.println ("Undefined nonterminals:");
104         else
105             writer.println ("Undefined nonterminals:");
106         it = result.iterator ();
107         while (it.hasNext ()) {
108             if (writer == null)
109                 System.out.println (" " + it.next ());
110             else
111                 writer.println (" " + it.next ());
112         }
113         if (writer == null)
114             System.out.println ("");
115         else
116             writer.println ("");
117     }
118     
119     public static boolean hasConflicts (Map JavaDoc f) {
120         boolean[] ff = new boolean[] {true};
121         Iterator JavaDoc it = f.keySet ().iterator ();
122         while (it.hasNext ()) {
123             String JavaDoc mt = (String JavaDoc) it.next ();
124             Map JavaDoc m = (Map JavaDoc) f.get (mt);
125             Iterator JavaDoc it2 = m.keySet ().iterator ();
126             while (it2.hasNext ()) {
127                 String JavaDoc nt = (String JavaDoc) it2.next ();
128                 if (pf2 (mt, nt, (Map JavaDoc) m.get (nt), new LinkedList JavaDoc (), ff))
129                     return true;
130             }
131         }
132         return false;
133     }
134     
135     private static boolean pf2 (String JavaDoc mt, String JavaDoc nt, Map JavaDoc m, LinkedList JavaDoc l, boolean[] f) {
136         if (((Set JavaDoc) m.get ("&")).size () < 2) return false;
137         boolean end = true;
138         Iterator JavaDoc it = m.keySet ().iterator ();
139         while (it.hasNext ()) {
140             Object JavaDoc e = it.next ();
141             if (e instanceof T) {
142                 end = false;
143                 l.addLast (e);
144                 pf2 (mt, nt, (Map JavaDoc) m.get (e), l, f);
145                 l.removeLast ();
146             }
147         }
148         return end;
149     }
150     
151     public static boolean printConflicts (Map JavaDoc f, PrintWriter JavaDoc writer) {
152         boolean[] ff = new boolean[] {true};
153         Iterator JavaDoc it = f.keySet ().iterator ();
154         while (it.hasNext ()) {
155             String JavaDoc nt = (String JavaDoc) it.next ();
156             pf (nt, (Map JavaDoc) f.get (nt), new LinkedList JavaDoc (), ff, writer);
157         }
158         return !ff [0];
159     }
160     
161     private static void pf (String JavaDoc nt, Map JavaDoc m, LinkedList JavaDoc l, boolean[] f, PrintWriter JavaDoc writer) {
162         if (((Set JavaDoc) m.get ("&")).size () < 2) return;
163         boolean end = true;
164         Iterator JavaDoc it = m.keySet ().iterator ();
165         while (it.hasNext ()) {
166             Object JavaDoc e = it.next ();
167             if (e instanceof T) {
168                 end = false;
169                 l.addLast (e);
170                 pf (nt, (Map JavaDoc) m.get (e), l, f, writer);
171                 l.removeLast ();
172             }
173         }
174         if (end) {
175             if (f [0]) {
176                 f [0] = false;
177                 if (writer == null)
178                     System.out.println ("Conflicts:");
179                 else
180                     writer.println ("Conflicts:");
181             }
182             if (writer == null)
183                 System.out.println (" " + nt + ":" + l + " " + m.get ("&"));
184             else
185                 writer.println (" " + nt + ":" + l + " " + m.get ("&"));
186         }
187     }
188     
189     public static void printF (Map JavaDoc m, PrintWriter JavaDoc writer) {
190         if (writer == null)
191             System.out.println ("First:");
192         else
193             writer.println ("First:");
194         Iterator JavaDoc it = m.keySet ().iterator ();
195         while (it.hasNext ()) {
196             String JavaDoc mimeType = (String JavaDoc) it.next ();
197             if (writer == null)
198                 System.out.println (" " + mimeType);
199             else
200                 writer.println (" " + mimeType);
201             Map JavaDoc m1 = (Map JavaDoc) m.get (mimeType);
202             Iterator JavaDoc it2 = m1.keySet ().iterator ();
203             while (it2.hasNext ()) {
204                 String JavaDoc nt = (String JavaDoc) it2.next ();
205                 Map JavaDoc m2 = (Map JavaDoc) m1.get (nt);
206                 String JavaDoc s = m2.containsKey ("#") ? ("#" + m2.get ("#").toString ()) : "";
207 // int d = 1;
208
// if (m2.containsKey ("*"))
209
// d = ((Integer) m2.get ("*")).intValue ();
210
if (writer == null)
211                     System.out.println (" " + nt + " : " + m2.get ("&") + " " + s /*+ " d=" + d*/);
212                 else
213                     writer.println (" " + nt + " : " + m2.get ("&") + " " + s /*+ " d=" + d*/);
214                 p (m2, " ", writer);
215             }
216         }
217     }
218     
219     private static void p (Map JavaDoc m, String JavaDoc i, PrintWriter JavaDoc writer) {
220         Iterator JavaDoc it = m.keySet ().iterator ();
221         while (it.hasNext ()) {
222             Object JavaDoc e = it.next ();
223             if ("&".equals (e)) continue;
224             if ("#".equals (e)) continue;
225             if ("*".equals (e)) continue;
226             Map JavaDoc m1 = (Map JavaDoc) m.get (e);
227             String JavaDoc s = m1.containsKey ("#") ? ("#" + m1.get ("#").toString ()) : "";
228             if (writer == null)
229                 System.out.println (i + e + " " + m1.get ("&") + " " + s);
230             else
231                 writer.println (i + e + " " + m1.get ("&") + " " + s);
232             p (m1, i + " ", writer);
233         }
234     }
235     
236     public static void printDepth (Map JavaDoc f, PrintWriter JavaDoc writer) {
237         if (writer == null)
238             System.out.println ("Depth:");
239         else
240             writer.println ("Depth:");
241         int dd = 0;
242         Iterator JavaDoc it = f.keySet ().iterator ();
243         while (it.hasNext ()) {
244             String JavaDoc mt = (String JavaDoc) it.next ();
245             Map JavaDoc m = (Map JavaDoc) f.get (mt);
246             Iterator JavaDoc it2 = m.keySet ().iterator ();
247             while (it2.hasNext ()) {
248                 String JavaDoc nt = (String JavaDoc) it2.next ();
249                 Map JavaDoc mm = (Map JavaDoc) m.get (nt);
250                 int[] r = pd (mm);
251                 dd += r [1];
252 // int d = 1;
253
// if (mm.containsKey ("*"))
254
// d = ((Integer) mm.get ("*")).intValue ();
255
if (writer == null)
256                     System.out.println (" " + nt + ": " + /*d + ", " +*/ r [0] + ", " + r [1]);
257                 else
258                     writer.println (" " + nt + ": " + /*d + ", " +*/ r [0] + ", " + r [1]);
259             }
260         }
261         if (writer == null)
262             System.out.println ("d = " + dd);
263         else
264             writer.println ("d = " + dd);
265     }
266     
267     private static int[] pd (Map JavaDoc m) {
268         int[] r = new int[] {0, 0};
269         Iterator JavaDoc it = m.keySet ().iterator ();
270         while (it.hasNext ()) {
271             Object JavaDoc e = it.next ();
272             if (e instanceof T) {
273                 int[] rr = pd ((Map JavaDoc) m.get (e));
274                 r[0] = Math.max (r[0], rr[0] + 1);
275                 r[1] += rr[1] + 1;
276             }
277         }
278         return r;
279     }
280 }
281
Popular Tags