KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > CalcLexer


1 /* The following code was generated by JFlex 1.4_5 on 1/13/04 3:11 PM */
2
3       
4
5 /**
6  * This class is a scanner generated by
7  * <a HREF="http://www.jflex.de/">JFlex</a> 1.4_5
8  * on 1/13/04 3:11 PM from the specification file
9  * <tt>lcalc.flex</tt>
10  */

11 class CalcLexer implements parser.runtime.Lexer {
12
13   /** This character denotes the end of file */
14   public static final int YYEOF = -1;
15
16   /** initial size of the lookahead buffer */
17   private static final int YY_BUFFERSIZE = 16384;
18
19   /** lexical states */
20   public static final int YYINITIAL = 0;
21
22   /**
23    * Translates characters to character classes
24    */

25   private static final char [] yycmap = {
26      0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 0, 3, 1, 0, 0,
27      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
28      3, 0, 0, 0, 0, 0, 0, 0, 12, 13, 10, 8, 0, 9, 0, 11,
29      4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0, 7, 0, 0, 0, 0,
30      0, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
31      6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 0, 0, 0, 0, 6,
32      0, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
33      6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 0, 0, 0, 0, 0
34   };
35
36   /**
37    * Translates DFA states to action switch labels.
38    */

39   private static final int [] YY_ACTION = yy_unpack_YY_ACTION();
40
41   private static final String JavaDoc YY_ACTION_packed0 =
42     "\1\0\1\1\2\2\2\3\1\4\1\5\1\6\1\7"+
43     "\1\10\1\11\1\12\1\13";
44
45   private static int [] yy_unpack_YY_ACTION() {
46     int [] result = new int[14];
47     int offset = 0;
48     offset = yy_unpack_YY_ACTION(YY_ACTION_packed0, offset, result);
49     return result;
50   }
51
52   private static int yy_unpack_YY_ACTION(String JavaDoc packed, int offset, int [] result) {
53     int i = 0; /* index in packed string */
54     int j = offset; /* index in unpacked array */
55     int l = packed.length();
56     while (i < l) {
57       int count = packed.charAt(i++);
58       int value = packed.charAt(i++);
59       do result[j++] = value; while (--count > 0);
60     }
61     return j;
62   }
63
64
65   /**
66    * Translates a state to a row index in the transition table
67    */

68   private static final int [] yy_rowMap = yy_unpack_yy_rowMap();
69
70   private static final String JavaDoc yy_rowMap_packed0 =
71     "\0\0\0\16\0\34\0\16\0\16\0\52\0\70\0\16"+
72     "\0\16\0\16\0\16\0\16\0\16\0\16";
73
74   private static int [] yy_unpack_yy_rowMap() {
75     int [] result = new int[14];
76     int offset = 0;
77     offset = yy_unpack_yy_rowMap(yy_rowMap_packed0, offset, result);
78     return result;
79   }
80
81   private static int yy_unpack_yy_rowMap(String JavaDoc packed, int offset, int [] result) {
82     int i = 0; /* index in packed string */
83     int j = offset; /* index in unpacked array */
84     int l = packed.length();
85     while (i < l) {
86       int high = ((int) packed.charAt(i++)) << 16;
87       result[j++] = high | packed.charAt(i++);
88     }
89     return j;
90   }
91
92   /**
93    * The transition table of the DFA
94    */

95   private static final int [] yytrans = yy_unpack_yytrans();
96
97   private static final String JavaDoc yytrans_packed0 =
98     "\1\2\1\3\2\4\1\5\1\6\1\7\1\10\1\11"+
99     "\1\12\1\13\1\14\1\15\1\16\20\0\1\4\17\0"+
100     "\2\6\14\0\3\7\7\0";
101
102   private static int [] yy_unpack_yytrans() {
103     int [] result = new int[70];
104     int offset = 0;
105     offset = yy_unpack_yytrans(yytrans_packed0, offset, result);
106     return result;
107   }
108
109   private static int yy_unpack_yytrans(String JavaDoc packed, int offset, int [] result) {
110     int i = 0; /* index in packed string */
111     int j = offset; /* index in unpacked array */
112     int l = packed.length();
113     while (i < l) {
114       int count = packed.charAt(i++);
115       int value = packed.charAt(i++);
116       value--;
117       do result[j++] = value; while (--count > 0);
118     }
119     return j;
120   }
121
122
123   /* error codes */
124   private static final int YY_UNKNOWN_ERROR = 0;
125   private static final int YY_NO_MATCH = 1;
126   private static final int YY_PUSHBACK_2BIG = 2;
127
128   /* error messages for the codes above */
129   private static final String JavaDoc YY_ERROR_MSG[] = {
130     "Unkown internal scanner error",
131     "Error: could not match input",
132     "Error: pushback value was too large"
133   };
134
135   /**
136    * YY_ATTRIBUTE[aState] contains the attributes of state <code>aState</code>
137    */

138   private static final int [] YY_ATTRIBUTE = yy_unpack_YY_ATTRIBUTE();
139
140   private static final String JavaDoc YY_ATTRIBUTE_packed0 =
141     "\1\0\1\11\1\1\2\11\2\1\7\11";
142
143   private static int [] yy_unpack_YY_ATTRIBUTE() {
144     int [] result = new int[14];
145     int offset = 0;
146     offset = yy_unpack_YY_ATTRIBUTE(YY_ATTRIBUTE_packed0, offset, result);
147     return result;
148   }
149
150   private static int yy_unpack_YY_ATTRIBUTE(String JavaDoc packed, int offset, int [] result) {
151     int i = 0; /* index in packed string */
152     int j = offset; /* index in unpacked array */
153     int l = packed.length();
154     while (i < l) {
155       int count = packed.charAt(i++);
156       int value = packed.charAt(i++);
157       do result[j++] = value; while (--count > 0);
158     }
159     return j;
160   }
161
162   /** the input device */
163   private java.io.Reader JavaDoc yy_reader;
164
165   /** the current state of the DFA */
166   private int yy_state;
167
168   /** the current lexical state */
169   private int yy_lexical_state = YYINITIAL;
170
171   /** this buffer contains the current text to be matched and is
172       the source of the yytext() string */

173   private char yy_buffer[] = new char[YY_BUFFERSIZE];
174
175   /** the textposition at the last accepting state */
176   private int yy_markedPos;
177
178   /** the textposition at the last state to be included in yytext */
179   private int yy_pushbackPos;
180
181   /** the current text position in the buffer */
182   private int yy_currentPos;
183
184   /** startRead marks the beginning of the yytext() string in the buffer */
185   private int yy_startRead;
186
187   /** endRead marks the last character in the buffer, that has been read
188       from input */

189   private int yy_endRead;
190
191   /** number of newlines encountered up to the start of the matched text */
192   private int yyline;
193
194   /** the number of characters up to the start of the matched text */
195   private int yychar;
196
197   /**
198    * the number of characters from the last newline up to the start of the
199    * matched text
200    */

201   private int yycolumn;
202
203   /**
204    * yy_atBOL == true <=> the scanner is currently at the beginning of a line
205    */

206   private boolean yy_atBOL = true;
207
208   /** yy_atEOF == true <=> the scanner is at the EOF */
209   private boolean yy_atEOF;
210
211   /** denotes if the user-EOF-code has already been executed */
212   private boolean yy_eof_done;
213
214   /* user code: */
215     private parser.runtime.StackSymbol symbol(int type) {
216         return new parser.runtime.StackSymbol(type, yyline, yycolumn);
217     }
218     
219     private parser.runtime.StackSymbol symbol(int type, Object JavaDoc value) {
220         return new parser.runtime.StackSymbol(type, yyline, yycolumn, value);
221     }
222
223
224   /**
225    * Creates a new scanner
226    * There is also a java.io.InputStream version of this constructor.
227    *
228    * @param in the java.io.Reader to read input from.
229    */

230   CalcLexer(java.io.Reader JavaDoc in) {
231     this.yy_reader = in;
232   }
233
234   /**
235    * Creates a new scanner.
236    * There is also java.io.Reader version of this constructor.
237    *
238    * @param in the java.io.Inputstream to read input from.
239    */

240   CalcLexer(java.io.InputStream JavaDoc in) {
241     this(new java.io.InputStreamReader JavaDoc(in));
242   }
243
244
245   /**
246    * Refills the input buffer.
247    *
248    * @return <code>false</code>, iff there was new input.
249    *
250    * @exception IOException if any I/O-Error occurs
251    */

252   private boolean yy_refill() throws java.io.IOException JavaDoc {
253
254     /* first: make room (if you can) */
255     if (yy_startRead > 0) {
256       System.arraycopy(yy_buffer, yy_startRead,
257                        yy_buffer, 0,
258                        yy_endRead-yy_startRead);
259
260       /* translate stored positions */
261       yy_endRead-= yy_startRead;
262       yy_currentPos-= yy_startRead;
263       yy_markedPos-= yy_startRead;
264       yy_pushbackPos-= yy_startRead;
265       yy_startRead = 0;
266     }
267
268     /* is the buffer big enough? */
269     if (yy_currentPos >= yy_buffer.length) {
270       /* if not: blow it up */
271       char newBuffer[] = new char[yy_currentPos*2];
272       System.arraycopy(yy_buffer, 0, newBuffer, 0, yy_buffer.length);
273       yy_buffer = newBuffer;
274     }
275
276     /* finally: fill the buffer with new input */
277     int numRead = yy_reader.read(yy_buffer, yy_endRead,
278                                             yy_buffer.length-yy_endRead);
279
280     if (numRead < 0) {
281       return true;
282     }
283     else {
284       yy_endRead+= numRead;
285       return false;
286     }
287   }
288
289     
290   /**
291    * Closes the input stream.
292    */

293   public final void yyclose() throws java.io.IOException JavaDoc {
294     yy_atEOF = true; /* indicate end of file */
295     yy_endRead = yy_startRead; /* invalidate buffer */
296
297     if (yy_reader != null)
298       yy_reader.close();
299   }
300
301
302   /**
303    * Resets the scanner to read from a new input stream.
304    * Does not close the old reader.
305    *
306    * All internal variables are reset, the old input stream
307    * <b>cannot</b> be reused (internal buffer is discarded and lost).
308    * Lexical state is set to <tt>YY_INITIAL</tt>.
309    *
310    * @param reader the new input stream
311    */

312   public final void yyreset(java.io.Reader JavaDoc reader) throws java.io.IOException JavaDoc {
313     yy_reader = reader;
314     yy_atBOL = true;
315     yy_atEOF = false;
316     yy_endRead = yy_startRead = 0;
317     yy_currentPos = yy_markedPos = yy_pushbackPos = 0;
318     yyline = yychar = yycolumn = 0;
319     yy_lexical_state = YYINITIAL;
320   }
321
322
323   /**
324    * Returns the current lexical state.
325    */

326   public final int yystate() {
327     return yy_lexical_state;
328   }
329
330
331   /**
332    * Enters a new lexical state
333    *
334    * @param newState the new lexical state
335    */

336   public final void yybegin(int newState) {
337     yy_lexical_state = newState;
338   }
339
340
341   /**
342    * Returns the text matched by the current regular expression.
343    */

344   public final String JavaDoc yytext() {
345     return new String JavaDoc( yy_buffer, yy_startRead, yy_markedPos-yy_startRead );
346   }
347
348
349   /**
350    * Returns the character at position <tt>pos</tt> from the
351    * matched text.
352    *
353    * It is equivalent to yytext().charAt(pos), but faster
354    *
355    * @param pos the position of the character to fetch.
356    * A value from 0 to yylength()-1.
357    *
358    * @return the character at position pos
359    */

360   public final char yycharat(int pos) {
361     return yy_buffer[yy_startRead+pos];
362   }
363
364
365   /**
366    * Returns the length of the matched text region.
367    */

368   public final int yylength() {
369     return yy_markedPos-yy_startRead;
370   }
371
372
373   /**
374    * Reports an error that occured while scanning.
375    *
376    * In a wellformed scanner (no or only correct usage of
377    * yypushback(int) and a match-all fallback rule) this method
378    * will only be called with things that "Can't Possibly Happen".
379    * If this method is called, something is seriously wrong
380    * (e.g. a JFlex bug producing a faulty scanner etc.).
381    *
382    * Usual syntax/scanner level error handling should be done
383    * in error fallback rules.
384    *
385    * @param errorCode the code of the errormessage to display
386    */

387   private void yy_ScanError(int errorCode) {
388     String JavaDoc message;
389     try {
390       message = YY_ERROR_MSG[errorCode];
391     }
392     catch (ArrayIndexOutOfBoundsException JavaDoc e) {
393       message = YY_ERROR_MSG[YY_UNKNOWN_ERROR];
394     }
395
396     throw new Error JavaDoc(message);
397   }
398
399
400   /**
401    * Pushes the specified amount of characters back into the input stream.
402    *
403    * They will be read again by then next call of the scanning method
404    *
405    * @param number the number of characters to be read again.
406    * This number must not be greater than yylength()!
407    */

408   public void yypushback(int number) {
409     if ( number > yylength() )
410       yy_ScanError(YY_PUSHBACK_2BIG);
411
412     yy_markedPos -= number;
413   }
414
415
416   /**
417    * Contains user EOF-code, which will be executed exactly once,
418    * when the end of file is reached
419    */

420   private void yy_do_eof() throws java.io.IOException JavaDoc {
421     if (!yy_eof_done) {
422       yy_eof_done = true;
423       yyclose();
424     }
425   }
426
427
428   /**
429    * Resumes scanning until the next regular expression is matched,
430    * the end of input is encountered or an I/O-Error occurs.
431    *
432    * @return the next token
433    * @exception IOException if any I/O-Error occurs
434    */

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