KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JFlex > Main


1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * JFlex 1.4.1 *
3  * Copyright (C) 1998-2004 Gerwin Klein <lsf@jflex.de> *
4  * All rights reserved. *
5  * *
6  * This program is free software; you can redistribute it and/or modify *
7  * it under the terms of the GNU General Public License. See the file *
8  * COPYRIGHT for more information. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License along *
16  * with this program; if not, write to the Free Software Foundation, Inc., *
17  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
18  * *
19  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

20
21 package JFlex;
22  
23 import java.io.*;
24 import java.util.*;
25 import JFlex.gui.MainFrame;
26
27
28 /**
29  * This is the main class of JFlex controlling the scanner generation process.
30  * It is responsible for parsing the commandline, getting input files,
31  * starting up the GUI if necessary, etc.
32  *
33  * @author Gerwin Klein
34  * @version JFlex 1.4.1, $Revision: 2.20 $, $Date: 2004/11/06 23:03:32 $
35  */

36 public class Main {
37   
38   /** JFlex version */
39   final public static String JavaDoc version = "1.4.1"; //$NON-NLS-1$
40

41   /**
42    * Generates a scanner for the specified input file.
43    *
44    * @param inputFile a file containing a lexical specification
45    * to generate a scanner for.
46    */

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)); //$NON-NLS-1$
82

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)); //$NON-NLS-1$
99

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)); //$NON-NLS-1$
112

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 JavaDoc 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 JavaDoc e) {
146       e.printStackTrace();
147       throw new GeneratorException();
148     }
149
150   }
151
152   public static Vector parseOptions(String JavaDoc 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") ) { //$NON-NLS-1$ //$NON-NLS-2$
158
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") ) { //$NON-NLS-1$ //$NON-NLS-2$
167
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") ) { //$NON-NLS-1$ //$NON-NLS-2$
177
Options.jlex = true;
178         continue;
179       }
180
181       if ( argv[i].equals("-v") || argv[i].equals("--verbose") || argv[i].equals("-verbose") ) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
182
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") ) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
188
Options.verbose = false;
189         Options.progress = false;
190         continue;
191       }
192
193       if ( argv[i].equals("--dump") || argv[i].equals("-dump") ) { //$NON-NLS-1$ //$NON-NLS-2$
194
Options.dump = true;
195         continue;
196       }
197
198       if ( argv[i].equals("--time") || argv[i].equals("-time") ) { //$NON-NLS-1$ //$NON-NLS-2$
199
Options.time = true;
200         continue;
201       }
202
203       if ( argv[i].equals("--version") || argv[i].equals("-version") ) { //$NON-NLS-1$ //$NON-NLS-2$
204
Out.println(ErrorMessages.THIS_IS_JFLEX, version);
205         throw new SilentExit();
206       }
207
208       if ( argv[i].equals("--dot") || argv[i].equals("-dot") ) { //$NON-NLS-1$ //$NON-NLS-2$
209
Options.dot = true;
210         continue;
211       }
212
213       if ( argv[i].equals("--help") || argv[i].equals("-h") || argv[i].equals("/h") ) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
214
printUsage();
215         throw new SilentExit();
216       }
217
218       if ( argv[i].equals("--info") || argv[i].equals("-info") ) { //$NON-NLS-1$ //$NON-NLS-2$
219
Out.printSystemInfo();
220         throw new SilentExit();
221       }
222       
223       if ( argv[i].equals("--nomin") || argv[i].equals("-nomin") ) { //$NON-NLS-1$ //$NON-NLS-2$
224
Options.no_minimize = true;
225         continue;
226       }
227
228       if ( argv[i].equals("--pack") || argv[i].equals("-pack") ) { //$NON-NLS-1$ //$NON-NLS-2$
229
Options.gen_method = Options.PACK;
230         continue;
231       }
232
233       if ( argv[i].equals("--table") || argv[i].equals("-table") ) { //$NON-NLS-1$ //$NON-NLS-2$
234
Options.gen_method = Options.TABLE;
235         continue;
236       }
237
238       if ( argv[i].equals("--switch") || argv[i].equals("-switch") ) { //$NON-NLS-1$ //$NON-NLS-2$
239
Options.gen_method = Options.SWITCH;
240         continue;
241       }
242       
243       if ( argv[i].equals("--nobak") || argv[i].equals("-nobak") ) { //$NON-NLS-1$ //$NON-NLS-2$
244
Options.no_backup = true;
245         continue;
246       }
247       
248       if ( argv[i].startsWith("-") ) { //$NON-NLS-1$
249
Out.error(ErrorMessages.UNKNOWN_COMMANDLINE, argv[i]);
250         printUsage();
251         throw new SilentExit();
252       }
253
254       // if argv[i] is not an option, try to read it as file
255
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+"\""); //$NON-NLS-2$
260
throw new GeneratorException();
261       }
262     }
263
264     return files;
265   }
266
267
268   public static void printUsage() {
269     Out.println(""); //$NON-NLS-1$
270
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 JavaDoc 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   /**
312    * Starts the generation process with the files in <code>argv</code> or
313    * pops up a window to choose a file, when <code>argv</code> doesn't have
314    * any file entries.
315    *
316    * @param argv the commandline.
317    */

318   public static void main(String JavaDoc 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