KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Lexer


1 /* The following code was generated by JFlex 1.4_5 on 1/13/04 8:30 AM */
2
3 /*
4   This example comes from a short article series in the Linux
5   Gazette by Richard A. Sevenich and Christopher Lopes, titled
6   "Compiler Construction Tools". The article series starts at
7
8   http://www.linuxgazette.com/issue39/sevenich.html
9
10   Small changes and updates to newest JFlex+Cup versions
11   by Gerwin Klein
12 */

13
14 /*
15   Commented By: Christopher Lopes
16   File Name: lcalc.flex
17   To Create: > jflex lcalc.flex
18
19   and then after the parser is created
20   > javac Lexer.java
21 */

22    
23 /* --------------------------Usercode Section------------------------ */
24    
25 import java_cup.runtime.*;
26       
27
28 /**
29  * This class is a scanner generated by
30  * <a HREF="http://www.jflex.de/">JFlex</a> 1.4_5
31  * on 1/13/04 8:30 AM from the specification file
32  * <tt>lcalc.flex</tt>
33  */

34 class Lexer implements java_cup.runtime.Scanner {
35
36   /** This character denotes the end of file */
37   public static final int YYEOF = -1;
38
39   /** initial size of the lookahead buffer */
40   private static final int YY_BUFFERSIZE = 16384;
41
42   /** lexical states */
43   public static final int YYINITIAL = 0;
44
45   /**
46    * Translates characters to character classes
47    */

48   private static final char [] yycmap = {
49      0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 0, 3, 1, 0, 0,
50      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
51      3, 0, 0, 0, 0, 0, 0, 0, 12, 13, 10, 8, 0, 9, 0, 11,
52      4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0, 7, 0, 0, 0, 0,
53      0, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
54      6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 0, 0, 0, 0, 6,
55      0, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
56      6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 0, 0, 0, 0, 0
57   };
58
59   /**
60    * Translates DFA states to action switch labels.
61    */

62   private static final int [] YY_ACTION = yy_unpack_YY_ACTION();
63
64   private static final String JavaDoc YY_ACTION_packed0 =
65     "\1\0\1\1\2\2\2\3\1\4\1\5\1\6\1\7"+
66     "\1\10\1\11\1\12\1\13";
67
68   private static int [] yy_unpack_YY_ACTION() {
69     int [] result = new int[14];
70     int offset = 0;
71     offset = yy_unpack_YY_ACTION(YY_ACTION_packed0, offset, result);
72     return result;
73   }
74
75   private static int yy_unpack_YY_ACTION(String JavaDoc packed, int offset, int [] result) {
76     int i = 0; /* index in packed string */
77     int j = offset; /* index in unpacked array */
78     int l = packed.length();
79     while (i < l) {
80       int count = packed.charAt(i++);
81       int value = packed.charAt(i++);
82       do result[j++] = value; while (--count > 0);
83     }
84     return j;
85   }
86
87
88   /**
89    * Translates a state to a row index in the transition table
90    */

91   private static final int [] yy_rowMap = yy_unpack_yy_rowMap();
92
93   private static final String JavaDoc yy_rowMap_packed0 =
94     "\0\0\0\16\0\34\0\16\0\16\0\52\0\70\0\16"+
95     "\0\16\0\16\0\16\0\16\0\16\0\16";
96
97   private static int [] yy_unpack_yy_rowMap() {
98     int [] result = new int[14];
99     int offset = 0;
100     offset = yy_unpack_yy_rowMap(yy_rowMap_packed0, offset, result);
101     return result;
102   }
103
104   private static int yy_unpack_yy_rowMap(String JavaDoc packed, int offset, int [] result) {
105     int i = 0; /* index in packed string */
106     int j = offset; /* index in unpacked array */
107     int l = packed.length();
108     while (i < l) {
109       int high = ((int) packed.charAt(i++)) << 16;
110       result[j++] = high | packed.charAt(i++);
111     }
112     return j;
113   }
114
115   /**
116    * The transition table of the DFA
117    */

118   private static final int [] yytrans = yy_unpack_yytrans();
119
120   private static final String JavaDoc yytrans_packed0 =
121     "\1\2\1\3\2\4\1\5\1\6\1\7\1\10\1\11"+
122     "\1\12\1\13\1\14\1\15\1\16\20\0\1\4\17\0"+
123     "\2\6\14\0\3\7\7\0";
124
125   private static int [] yy_unpack_yytrans() {
126     int [] result = new int[70];
127     int offset = 0;
128     offset = yy_unpack_yytrans(yytrans_packed0, offset, result);
129     return result;
130   }
131
132   private static int yy_unpack_yytrans(String JavaDoc packed, int offset, int [] result) {
133     int i = 0; /* index in packed string */
134     int j = offset; /* index in unpacked array */
135     int l = packed.length();
136     while (i < l) {
137       int count = packed.charAt(i++);
138       int value = packed.charAt(i++);
139       value--;
140       do result[j++] = value; while (--count > 0);
141     }
142     return j;
143   }
144
145
146   /* error codes */
147   private static final int YY_UNKNOWN_ERROR = 0;
148   private static final int YY_NO_MATCH = 1;
149   private static final int YY_PUSHBACK_2BIG = 2;
150
151   /* error messages for the codes above */
152   private static final String JavaDoc YY_ERROR_MSG[] = {
153     "Unkown internal scanner error",
154     "Error: could not match input",
155     "Error: pushback value was too large"
156   };
157
158   /**
159    * YY_ATTRIBUTE[aState] contains the attributes of state <code>aState</code>
160    */

161   private static final int [] YY_ATTRIBUTE = yy_unpack_YY_ATTRIBUTE();
162
163   private static final String JavaDoc YY_ATTRIBUTE_packed0 =
164     "\1\0\1\11\1\1\2\11\2\1\7\11";
165
166   private static int [] yy_unpack_YY_ATTRIBUTE() {
167     int [] result = new int[14];
168     int offset = 0;
169     offset = yy_unpack_YY_ATTRIBUTE(YY_ATTRIBUTE_packed0, offset, result);
170     return result;
171   }
172
173   private static int yy_unpack_YY_ATTRIBUTE(String JavaDoc packed, int offset, int [] result) {
174     int i = 0; /* index in packed string */
175     int j = offset; /* index in unpacked array */
176     int l = packed.length();
177     while (i < l) {
178       int count = packed.charAt(i++);
179       int value = packed.charAt(i++);
180       do result[j++] = value; while (--count > 0);
181     }
182     return j;
183   }
184
185   /** the input device */
186   private java.io.Reader JavaDoc yy_reader;
187
188   /** the current state of the DFA */
189   private int yy_state;
190
191   /** the current lexical state */
192   private int yy_lexical_state = YYINITIAL;
193
194   /** this buffer contains the current text to be matched and is
195       the source of the yytext() string */

196   private char yy_buffer[] = new char[YY_BUFFERSIZE];
197
198   /** the textposition at the last accepting state */
199   private int yy_markedPos;
200
201   /** the textposition at the last state to be included in yytext */
202   private int yy_pushbackPos;
203
204   /** the current text position in the buffer */
205   private int yy_currentPos;
206
207   /** startRead marks the beginning of the yytext() string in the buffer */
208   private int yy_startRead;
209
210   /** endRead marks the last character in the buffer, that has been read
211       from input */

212   private int yy_endRead;
213
214   /** number of newlines encountered up to the start of the matched text */
215   private int yyline;
216
217   /** the number of characters up to the start of the matched text */
218   private int yychar;
219
220   /**
221    * the number of characters from the last newline up to the start of the
222    * matched text
223    */

224   private int yycolumn;
225
226   /**
227    * yy_atBOL == true <=> the scanner is currently at the beginning of a line
228    */

229   private boolean yy_atBOL = true;
230
231   /** yy_atEOF == true <=> the scanner is at the EOF */
232   private boolean yy_atEOF;
233
234   /** denotes if the user-EOF-code has already been executed */
235   private boolean yy_eof_done;
236
237   /* user code: */
238     /* To create a new java_cup.runtime.Symbol with information about
239        the current token, the token will have no value in this
240        case. */

241     private Symbol symbol(int type) {
242         return new Symbol(type, yyline, yycolumn);
243     }
244     
245     /* Also creates a new java_cup.runtime.Symbol with information
246        about the current token, but this object has a value. */

247     private Symbol symbol(int type, Object JavaDoc value) {
248         return new Symbol(type, yyline, yycolumn, value);
249     }
250
251
252   /**
253    * Creates a new scanner
254    * There is also a java.io.InputStream version of this constructor.
255    *
256    * @param in the java.io.Reader to read input from.
257    */

258   Lexer(java.io.Reader JavaDoc in) {
259     this.yy_reader = in;
260   }
261
262   /**
263    * Creates a new scanner.
264    * There is also java.io.Reader version of this constructor.
265    *
266    * @param in the java.io.Inputstream to read input from.
267    */

268   Lexer(java.io.InputStream JavaDoc in) {
269     this(new java.io.InputStreamReader JavaDoc(in));
270   }
271
272
273   /**
274    * Refills the input buffer.
275    *
276    * @return <code>false</code>, iff there was new input.
277    *
278    * @exception IOException if any I/O-Error occurs
279    */

280   private boolean yy_refill() throws java.io.IOException JavaDoc {
281
282     /* first: make room (if you can) */
283     if (yy_startRead > 0) {
284       System.arraycopy(yy_buffer, yy_startRead,
285                        yy_buffer, 0,
286                        yy_endRead-yy_startRead);
287
288       /* translate stored positions */
289       yy_endRead-= yy_startRead;
290       yy_currentPos-= yy_startRead;
291       yy_markedPos-= yy_startRead;
292       yy_pushbackPos-= yy_startRead;
293       yy_startRead = 0;
294     }
295
296     /* is the buffer big enough? */
297     if (yy_currentPos >= yy_buffer.length) {
298       /* if not: blow it up */
299       char newBuffer[] = new char[yy_currentPos*2];
300       System.arraycopy(yy_buffer, 0, newBuffer, 0, yy_buffer.length);
301       yy_buffer = newBuffer;
302     }
303
304     /* finally: fill the buffer with new input */
305     int numRead = yy_reader.read(yy_buffer, yy_endRead,
306                                             yy_buffer.length-yy_endRead);
307
308     if (numRead < 0) {
309       return true;
310     }
311     else {
312       yy_endRead+= numRead;
313       return false;
314     }
315   }
316
317     
318   /**
319    * Closes the input stream.
320    */

321   public final void yyclose() throws java.io.IOException JavaDoc {
322     yy_atEOF = true; /* indicate end of file */
323     yy_endRead = yy_startRead; /* invalidate buffer */
324
325     if (yy_reader != null)
326       yy_reader.close();
327   }
328
329
330   /**
331    * Resets the scanner to read from a new input stream.
332    * Does not close the old reader.
333    *
334    * All internal variables are reset, the old input stream
335    * <b>cannot</b> be reused (internal buffer is discarded and lost).
336    * Lexical state is set to <tt>YY_INITIAL</tt>.
337    *
338    * @param reader the new input stream
339    */

340   public final void yyreset(java.io.Reader JavaDoc reader) throws java.io.IOException JavaDoc {
341     yy_reader = reader;
342     yy_atBOL = true;
343     yy_atEOF = false;
344     yy_endRead = yy_startRead = 0;
345     yy_currentPos = yy_markedPos = yy_pushbackPos = 0;
346     yyline = yychar = yycolumn = 0;
347     yy_lexical_state = YYINITIAL;
348   }
349
350
351   /**
352    * Returns the current lexical state.
353    */

354   public final int yystate() {
355     return yy_lexical_state;
356   }
357
358
359   /**
360    * Enters a new lexical state
361    *
362    * @param newState the new lexical state
363    */

364   public final void yybegin(int newState) {
365     yy_lexical_state = newState;
366   }
367
368
369   /**
370    * Returns the text matched by the current regular expression.
371    */

372   public final String JavaDoc yytext() {
373     return new String JavaDoc( yy_buffer, yy_startRead, yy_markedPos-yy_startRead );
374   }
375
376
377   /**
378    * Returns the character at position <tt>pos</tt> from the
379    * matched text.
380    *
381    * It is equivalent to yytext().charAt(pos), but faster
382    *
383    * @param pos the position of the character to fetch.
384    * A value from 0 to yylength()-1.
385    *
386    * @return the character at position pos
387    */

388   public final char yycharat(int pos) {
389     return yy_buffer[yy_startRead+pos];
390   }
391
392
393   /**
394    * Returns the length of the matched text region.
395    */

396   public final int yylength() {
397     return yy_markedPos-yy_startRead;
398   }
399
400
401   /**
402    * Reports an error that occured while scanning.
403    *
404    * In a wellformed scanner (no or only correct usage of
405    * yypushback(int) and a match-all fallback rule) this method
406    * will only be called with things that "Can't Possibly Happen".
407    * If this method is called, something is seriously wrong
408    * (e.g. a JFlex bug producing a faulty scanner etc.).
409    *
410    * Usual syntax/scanner level error handling should be done
411    * in error fallback rules.
412    *
413    * @param errorCode the code of the errormessage to display
414    */

415   private void yy_ScanError(int errorCode) {
416     String JavaDoc message;
417     try {
418       message = YY_ERROR_MSG[errorCode];
419     }
420     catch (ArrayIndexOutOfBoundsException JavaDoc e) {
421       message = YY_ERROR_MSG[YY_UNKNOWN_ERROR];
422     }
423
424     throw new Error JavaDoc(message);
425   }
426
427
428   /**
429    * Pushes the specified amount of characters back into the input stream.
430    *
431    * They will be read again by then next call of the scanning method
432    *
433    * @param number the number of characters to be read again.
434    * This number must not be greater than yylength()!
435    */

436   public void yypushback(int number) {
437     if ( number > yylength() )
438       yy_ScanError(YY_PUSHBACK_2BIG);
439
440     yy_markedPos -= number;
441   }
442
443
444   /**
445    * Contains user EOF-code, which will be executed exactly once,
446    * when the end of file is reached
447    */

448   private void yy_do_eof() throws java.io.IOException JavaDoc {
449     if (!yy_eof_done) {
450       yy_eof_done = true;
451       yyclose();
452     }
453   }
454
455
456   /**
457    * Resumes scanning until the next regular expression is matched,
458    * the end of input is encountered or an I/O-Error occurs.
459    *
460    * @return the next token
461    * @exception IOException if any I/O-Error occurs
462    */

463   public java_cup.runtime.Symbol next_token() throws java.io.IOException JavaDoc {
464     int yy_input;
465     int yy_action;
466
467     // cached fields:
468
int yy_currentPos_l;
469     int yy_markedPos_l;
470     int yy_endRead_l = yy_endRead;
471     char [] yy_buffer_l = yy_buffer;
472     char [] yycmap_l = yycmap;
473
474     int [] yytrans_l = yytrans;
475     int [] yy_rowMap_l = yy_rowMap;
476     int [] yy_attr_l = YY_ATTRIBUTE;
477
478     while (true) {
479       yy_markedPos_l = yy_markedPos;
480
481       boolean yy_r = false;
482       for (yy_currentPos_l = yy_startRead; yy_currentPos_l < yy_markedPos_l;
483                                                              yy_currentPos_l++) {
484         switch (yy_buffer_l[yy_currentPos_l]) {
485         case '\u000B':
486         case '\u000C':
487         case '\u0085':
488         case '\u2028':
489         case '\u2029':
490           yyline++;
491           yycolumn = 0;
492           yy_r = false;
493           break;
494         case '\r':
495           yyline++;
496           yycolumn = 0;
497           yy_r = true;
498           break;
499         case '\n':
500           if (yy_r)
501             yy_r = false;
502           else {
503             yyline++;
504             yycolumn = 0;
505           }
506           break;
507         default:
508           yy_r = false;
509           yycolumn++;
510         }
511       }
512
513       if (yy_r) {
514         // peek one character ahead if it is \n (if we have counted one line too much)
515
boolean yy_peek;
516         if (yy_markedPos_l < yy_endRead_l)
517           yy_peek = yy_buffer_l[yy_markedPos_l] == '\n';
518         else if (yy_atEOF)
519           yy_peek = false;
520         else {
521           boolean eof = yy_refill();
522           yy_markedPos_l = yy_markedPos;
523           yy_buffer_l = yy_buffer;
524           if (eof)
525             yy_peek = false;
526           else
527             yy_peek = yy_buffer_l[yy_markedPos_l] == '\n';
528         }
529         if (yy_peek) yyline--;
530       }
531       yy_action = -1;
532
533       yy_currentPos_l = yy_currentPos = yy_startRead = yy_markedPos_l;
534   
535       yy_state = yy_lexical_state;
536
537
538       yy_forAction: {
539         while (true) {
540     
541           if (yy_currentPos_l < yy_endRead_l)
542             yy_input = yy_buffer_l[yy_currentPos_l++];
543           else if (yy_atEOF) {
544             yy_input = YYEOF;
545             break yy_forAction;
546           }
547           else {
548             // store back cached positions
549
yy_currentPos = yy_currentPos_l;
550             yy_markedPos = yy_markedPos_l;
551             boolean eof = yy_refill();
552             // get translated positions and possibly new buffer
553
yy_currentPos_l = yy_currentPos;
554             yy_markedPos_l = yy_markedPos;
555             yy_buffer_l = yy_buffer;
556             yy_endRead_l = yy_endRead;
557             if (eof) {
558               yy_input = YYEOF;
559               break yy_forAction;
560             }
561             else {
562               yy_input = yy_buffer_l[yy_currentPos_l++];
563             }
564           }
565           int yy_next = yytrans_l[ yy_rowMap_l[yy_state] + yycmap_l[yy_input] ];
566           if (yy_next == -1) break yy_forAction;
567           yy_state = yy_next;
568
569           int yy_attributes = yy_attr_l[yy_state];
570           if ( (yy_attributes & 1) == 1 ) {
571             yy_action = yy_state;
572             yy_markedPos_l = yy_currentPos_l;
573             if ( (yy_attributes & 8) == 8 ) break yy_forAction;
574           }
575
576         }
577       }
578
579       // store back cached position
580
yy_markedPos = yy_markedPos_l;
581
582       switch (yy_action < 0 ? yy_action : YY_ACTION[yy_action]) {
583         case 6:
584           { System.out.print(" + "); return symbol(sym.PLUS); }
585         case 12: break;
586         case 9:
587           { System.out.print(" / "); return symbol(sym.DIVIDE); }
588         case 13: break;
589         case 5:
590           { return symbol(sym.SEMI); }
591         case 14: break;
592         case 8:
593           { System.out.print(" * "); return symbol(sym.TIMES); }
594         case 15: break;
595         case 3:
596           { System.out.print(yytext());
597                          return symbol(sym.NUMBER, new Integer JavaDoc(yytext())); }
598         case 16: break;
599         case 11:
600           { System.out.print(" ) "); return symbol(sym.RPAREN); }
601         case 17: break;
602         case 4:
603           { System.out.print(yytext());
604                          return symbol(sym.ID, new Integer JavaDoc(1)); }
605         case 18: break;
606         case 2:
607           { /* just skip what was found, do nothing */ }
608         case 19: break;
609         case 10:
610           { System.out.print(" ( "); return symbol(sym.LPAREN); }
611         case 20: break;
612         case 7:
613           { System.out.print(" - "); return symbol(sym.MINUS); }
614         case 21: break;
615         case 1:
616           { throw new Error JavaDoc("Illegal character <"+yytext()+">"); }
617         case 22: break;
618         default:
619           if (yy_input == YYEOF && yy_startRead == yy_currentPos) {
620             yy_atEOF = true;
621             yy_do_eof();
622               { return new java_cup.runtime.Symbol(sym.EOF); }
623           }
624           else {
625             yy_ScanError(YY_NO_MATCH);
626           }
627       }
628     }
629   }
630
631
632 }
633
Popular Tags