1 20 21 package JFlex; 22 23 import java.io.*; 24 import java.util.*; 25 import JFlex.gui.MainFrame; 26 27 28 36 public class Main { 37 38 39 final public static String version = "1.4.1"; 41 47 public static void generate(File inputFile) { 48 49 Out.resetCounters(); 50 51 Timer totalTime = new Timer(); 52 Timer time = new Timer(); 53 54 LexScan scanner = null; 55 LexParse parser = null; 56 FileReader inputReader = null; 57 58 totalTime.start(); 59 60 try { 61 Out.println(ErrorMessages.READING, inputFile.toString()); 62 inputReader = new FileReader(inputFile); 63 scanner = new LexScan(inputReader); 64 scanner.setFile(inputFile); 65 parser = new LexParse(scanner); 66 } 67 catch (FileNotFoundException e) { 68 Out.error(ErrorMessages.CANNOT_OPEN, inputFile.toString()); 69 throw new GeneratorException(); 70 } 71 72 try { 73 NFA nfa = (NFA) parser.parse().value; 74 75 Out.checkErrors(); 76 77 if (Options.dump) Out.dump(ErrorMessages.get(ErrorMessages.NFA_IS)+ 78 Out.NL+nfa+Out.NL); 79 80 if (Options.dot) 81 nfa.writeDot(Emitter.normalize("nfa.dot", null)); 83 Out.println(ErrorMessages.NFA_STATES, nfa.numStates); 84 85 time.start(); 86 DFA dfa = nfa.getDFA(); 87 time.stop(); 88 Out.time(ErrorMessages.DFA_TOOK, time); 89 90 dfa.checkActions(scanner, parser); 91 92 nfa = null; 93 94 if (Options.dump) Out.dump(ErrorMessages.get(ErrorMessages.DFA_IS)+ 95 Out.NL+dfa+Out.NL); 96 97 if (Options.dot) 98 dfa.writeDot(Emitter.normalize("dfa-big.dot", null)); 100 time.start(); 101 dfa.minimize(); 102 time.stop(); 103 104 Out.time(ErrorMessages.MIN_TOOK, time); 105 106 if (Options.dump) 107 Out.dump(ErrorMessages.get(ErrorMessages.MIN_DFA_IS)+ 108 Out.NL+dfa); 109 110 if (Options.dot) 111 dfa.writeDot(Emitter.normalize("dfa-min.dot", null)); 113 time.start(); 114 115 Emitter e = new Emitter(inputFile, parser, dfa); 116 e.emit(); 117 118 time.stop(); 119 120 Out.time(ErrorMessages.WRITE_TOOK, time); 121 122 totalTime.stop(); 123 124 Out.time(ErrorMessages.TOTAL_TIME, totalTime); 125 } 126 catch (ScannerException e) { 127 Out.error(e.file, e.message, e.line, e.column); 128 throw new GeneratorException(); 129 } 130 catch (MacroException e) { 131 Out.error(e.getMessage()); 132 throw new GeneratorException(); 133 } 134 catch (IOException e) { 135 Out.error(ErrorMessages.IO_ERROR, e.toString()); 136 throw new GeneratorException(); 137 } 138 catch (OutOfMemoryError e) { 139 Out.error(ErrorMessages.OUT_OF_MEMORY); 140 throw new GeneratorException(); 141 } 142 catch (GeneratorException e) { 143 throw new GeneratorException(); 144 } 145 catch (Exception e) { 146 e.printStackTrace(); 147 throw new GeneratorException(); 148 } 149 150 } 151 152 public static Vector parseOptions(String argv[]) throws SilentExit { 153 Vector files = new Vector(); 154 155 for (int i = 0; i < argv.length; i++) { 156 157 if ( argv[i].equals("-d") || argv[i].equals("--outdir") ) { if ( ++i >= argv.length ) { 159 Out.error(ErrorMessages.NO_DIRECTORY); 160 throw new GeneratorException(); 161 } 162 Options.setDir(argv[i]); 163 continue; 164 } 165 166 if ( argv[i].equals("--skel") || argv[i].equals("-skel") ) { if ( ++i >= argv.length ) { 168 Out.error(ErrorMessages.NO_SKEL_FILE); 169 throw new GeneratorException(); 170 } 171 172 Options.setSkeleton(new File(argv[i])); 173 continue; 174 } 175 176 if ( argv[i].equals("-jlex") || argv[i].equals("--jlex") ) { Options.jlex = true; 178 continue; 179 } 180 181 if ( argv[i].equals("-v") || argv[i].equals("--verbose") || argv[i].equals("-verbose") ) { Options.verbose = true; 183 Options.progress = true; 184 continue; 185 } 186 187 if ( argv[i].equals("-q") || argv[i].equals("--quiet") || argv[i].equals("-quiet") ) { Options.verbose = false; 189 Options.progress = false; 190 continue; 191 } 192 193 if ( argv[i].equals("--dump") || argv[i].equals("-dump") ) { Options.dump = true; 195 continue; 196 } 197 198 if ( argv[i].equals("--time") || argv[i].equals("-time") ) { Options.time = true; 200 continue; 201 } 202 203 if ( argv[i].equals("--version") || argv[i].equals("-version") ) { Out.println(ErrorMessages.THIS_IS_JFLEX, version); 205 throw new SilentExit(); 206 } 207 208 if ( argv[i].equals("--dot") || argv[i].equals("-dot") ) { Options.dot = true; 210 continue; 211 } 212 213 if ( argv[i].equals("--help") || argv[i].equals("-h") || argv[i].equals("/h") ) { printUsage(); 215 throw new SilentExit(); 216 } 217 218 if ( argv[i].equals("--info") || argv[i].equals("-info") ) { Out.printSystemInfo(); 220 throw new SilentExit(); 221 } 222 223 if ( argv[i].equals("--nomin") || argv[i].equals("-nomin") ) { Options.no_minimize = true; 225 continue; 226 } 227 228 if ( argv[i].equals("--pack") || argv[i].equals("-pack") ) { Options.gen_method = Options.PACK; 230 continue; 231 } 232 233 if ( argv[i].equals("--table") || argv[i].equals("-table") ) { Options.gen_method = Options.TABLE; 235 continue; 236 } 237 238 if ( argv[i].equals("--switch") || argv[i].equals("-switch") ) { Options.gen_method = Options.SWITCH; 240 continue; 241 } 242 243 if ( argv[i].equals("--nobak") || argv[i].equals("-nobak") ) { Options.no_backup = true; 245 continue; 246 } 247 248 if ( argv[i].startsWith("-") ) { Out.error(ErrorMessages.UNKNOWN_COMMANDLINE, argv[i]); 250 printUsage(); 251 throw new SilentExit(); 252 } 253 254 File f = new File(argv[i]); 256 if ( f.isFile() && f.canRead() ) 257 files.addElement(f); 258 else { 259 Out.error("Sorry, couldn't open \""+f+"\""); throw new GeneratorException(); 261 } 262 } 263 264 return files; 265 } 266 267 268 public static void printUsage() { 269 Out.println(""); Out.println("Usage: jflex <options> <input-files>"); 271 Out.println(""); 272 Out.println("Where <options> can be one or more of"); 273 Out.println("-d <directory> write generated file to <directory>"); 274 Out.println("--skel <file> use external skeleton <file>"); 275 Out.println("--switch"); 276 Out.println("--table"); 277 Out.println("--pack set default code generation method"); 278 Out.println("--jlex strict JLex compatibility"); 279 Out.println("--nomin skip minimization step"); 280 Out.println("--nobak don't create backup files"); 281 Out.println("--dump display transition tables"); 282 Out.println("--dot write graphviz .dot files for the generated automata (alpha)"); 283 Out.println("--verbose"); 284 Out.println("-v display generation progress messages (default)"); 285 Out.println("--quiet"); 286 Out.println("-q display errors only"); 287 Out.println("--time display generation time statistics"); 288 Out.println("--version print the version number of this copy of jflex"); 289 Out.println("--info print system + JDK information"); 290 Out.println("--help"); 291 Out.println("-h print this message"); 292 Out.println(""); 293 Out.println(ErrorMessages.THIS_IS_JFLEX, version); 294 Out.println("Have a nice day!"); 295 } 296 297 298 public static void generate(String argv[]) throws SilentExit { 299 Vector files = parseOptions(argv); 300 301 if (files.size() > 0) { 302 for (int i = 0; i < files.size(); i++) 303 generate((File) files.elementAt(i)); 304 } 305 else { 306 new MainFrame(); 307 } 308 } 309 310 311 318 public static void main(String argv[]) { 319 try { 320 generate(argv); 321 } 322 catch (GeneratorException e) { 323 Out.statistics(); 324 System.exit(1); 325 } 326 catch (SilentExit e) { 327 System.exit(1); 328 } 329 } 330 } 331 | Popular Tags |