1 19 20 package org.netbeans.modules.languages.parser; 21 22 import org.netbeans.api.languages.ASTToken; 23 import java.io.PrintWriter ; 24 import java.util.ArrayList ; 25 import java.util.Collections ; 26 import java.util.HashMap ; 27 import java.util.HashSet ; 28 import java.util.Iterator ; 29 import java.util.LinkedList ; 30 import java.util.List ; 31 import java.util.Map ; 32 import java.util.Set ; 33 import org.netbeans.modules.languages.parser.LLSyntaxAnalyser.Rule; 34 import org.netbeans.modules.languages.parser.LLSyntaxAnalyser.T; 35 36 40 public class AnalyserAnalyser { 41 42 public static void printRules (List rules, PrintWriter writer) { 43 if (writer == null) 44 System.out.println ("Rules:"); 45 else 46 writer.println ("Rules:"); 47 List l = new ArrayList (); 48 Map m = new HashMap (); 49 Map mm = new HashMap (); 50 int i = 0; 51 Iterator 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 ll = (List ) m.get (r.getNT ()); 57 if (ll == null) { 58 ll = new ArrayList (); 59 m.put (r.getNT (), ll); 60 mm.put (r.getNT (), new ArrayList ()); 61 } 62 ll.add (r); 63 ((List ) mm.get (r.getNT ())).add (new Integer (i++)); 64 } 65 Collections.sort (l); 66 it = l.iterator (); 67 while (it.hasNext ()) { 68 String nt = (String ) it.next (); 69 List ll = (List ) m.get (nt); 70 Iterator it2 = ll.iterator (); 71 Iterator it3 = ((List ) 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 rules, PrintWriter writer) { 85 Set f = new HashSet (); 86 Iterator it = rules.iterator (); 87 while (it.hasNext ()) 88 f.add (((Rule) it.next ()).getNT ()); 89 Set result = new HashSet (); 90 it = rules.iterator (); 91 while (it.hasNext ()) { 92 Rule r = (Rule) it.next (); 93 Iterator it2 = r.getRight ().iterator (); 94 while (it2.hasNext ()) { 95 Object 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 f) { 120 boolean[] ff = new boolean[] {true}; 121 Iterator it = f.keySet ().iterator (); 122 while (it.hasNext ()) { 123 String mt = (String ) it.next (); 124 Map m = (Map ) f.get (mt); 125 Iterator it2 = m.keySet ().iterator (); 126 while (it2.hasNext ()) { 127 String nt = (String ) it2.next (); 128 if (pf2 (mt, nt, (Map ) m.get (nt), new LinkedList (), ff)) 129 return true; 130 } 131 } 132 return false; 133 } 134 135 private static boolean pf2 (String mt, String nt, Map m, LinkedList l, boolean[] f) { 136 if (((Set ) m.get ("&")).size () < 2) return false; 137 boolean end = true; 138 Iterator it = m.keySet ().iterator (); 139 while (it.hasNext ()) { 140 Object e = it.next (); 141 if (e instanceof T) { 142 end = false; 143 l.addLast (e); 144 pf2 (mt, nt, (Map ) m.get (e), l, f); 145 l.removeLast (); 146 } 147 } 148 return end; 149 } 150 151 public static boolean printConflicts (Map f, PrintWriter writer) { 152 boolean[] ff = new boolean[] {true}; 153 Iterator it = f.keySet ().iterator (); 154 while (it.hasNext ()) { 155 String nt = (String ) it.next (); 156 pf (nt, (Map ) f.get (nt), new LinkedList (), ff, writer); 157 } 158 return !ff [0]; 159 } 160 161 private static void pf (String nt, Map m, LinkedList l, boolean[] f, PrintWriter writer) { 162 if (((Set ) m.get ("&")).size () < 2) return; 163 boolean end = true; 164 Iterator it = m.keySet ().iterator (); 165 while (it.hasNext ()) { 166 Object e = it.next (); 167 if (e instanceof T) { 168 end = false; 169 l.addLast (e); 170 pf (nt, (Map ) 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 m, PrintWriter writer) { 190 if (writer == null) 191 System.out.println ("First:"); 192 else 193 writer.println ("First:"); 194 Iterator it = m.keySet ().iterator (); 195 while (it.hasNext ()) { 196 String mimeType = (String ) it.next (); 197 if (writer == null) 198 System.out.println (" " + mimeType); 199 else 200 writer.println (" " + mimeType); 201 Map m1 = (Map ) m.get (mimeType); 202 Iterator it2 = m1.keySet ().iterator (); 203 while (it2.hasNext ()) { 204 String nt = (String ) it2.next (); 205 Map m2 = (Map ) m1.get (nt); 206 String s = m2.containsKey ("#") ? ("#" + m2.get ("#").toString ()) : ""; 207 if (writer == null) 211 System.out.println (" " + nt + " : " + m2.get ("&") + " " + s ); 212 else 213 writer.println (" " + nt + " : " + m2.get ("&") + " " + s ); 214 p (m2, " ", writer); 215 } 216 } 217 } 218 219 private static void p (Map m, String i, PrintWriter writer) { 220 Iterator it = m.keySet ().iterator (); 221 while (it.hasNext ()) { 222 Object e = it.next (); 223 if ("&".equals (e)) continue; 224 if ("#".equals (e)) continue; 225 if ("*".equals (e)) continue; 226 Map m1 = (Map ) m.get (e); 227 String 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 f, PrintWriter writer) { 237 if (writer == null) 238 System.out.println ("Depth:"); 239 else 240 writer.println ("Depth:"); 241 int dd = 0; 242 Iterator it = f.keySet ().iterator (); 243 while (it.hasNext ()) { 244 String mt = (String ) it.next (); 245 Map m = (Map ) f.get (mt); 246 Iterator it2 = m.keySet ().iterator (); 247 while (it2.hasNext ()) { 248 String nt = (String ) it2.next (); 249 Map mm = (Map ) m.get (nt); 250 int[] r = pd (mm); 251 dd += r [1]; 252 if (writer == null) 256 System.out.println (" " + nt + ": " + r [0] + ", " + r [1]); 257 else 258 writer.println (" " + nt + ": " + 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 m) { 268 int[] r = new int[] {0, 0}; 269 Iterator it = m.keySet ().iterator (); 270 while (it.hasNext ()) { 271 Object e = it.next (); 272 if (e instanceof T) { 273 int[] rr = pd ((Map ) 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 |