001         package com.croftsoft.apps.compiler;
002         import java.io.*;
003         import java.text.*;
004         import java.util.*;
005         import java_cup.runtime.*;
006         /*********************************************************************
007         * Token scanner for Calc.
008         *
009         * <B>Reference:</B>
010         *
011         * <P>
012         *
013         * "JLex: A Lexical Analyzer Generator for Java"<BR>
014         * <A HREF="http://www.cs.princeton.edu/~appel/modern/java/JLex/">
015         * http://www.cs.princeton.edu/~appel/modern/java/JLex/</A>
016         *
017         * @author
018         *   <A HREF="http://www.alumni.caltech.edu/~croft/">David W. Croft</A>
019         * @version
020         *   1999-03-15
021         *********************************************************************/
022         //////////////////////////////////////////////////////////////////////
023         // Portions of this code machine-generated by JLex.
024         //////////////////////////////////////////////////////////////////////
025    
026    
027    public class CalcScanner implements CalcSymbols {
028            private final int YY_BUFFER_SIZE = 512;
029            private final int YY_F = -1;
030            private final int YY_NO_STATE = -1;
031            private final int YY_NOT_ACCEPT = 0;
032            private final int YY_START = 1;
033            private final int YY_END = 2;
034            private final int YY_NO_ANCHOR = 4;
035            private final char YYEOF = '\uFFFF';
036    
037         //////////////////////////////////////////////////////////////////////
038         //////////////////////////////////////////////////////////////////////
039         private static final String [ ]  TEST_DATA = {
040             "1",
041             "22",
042             "333",
043             ";",
044             "*",
045             "write(a);",
046             "bill_123" };
047         //////////////////////////////////////////////////////////////////////
048         //////////////////////////////////////////////////////////////////////
049         public static void  main ( String [ ]  args )
050           throws Exception
051         //////////////////////////////////////////////////////////////////////
052         {
053           for ( int  i = 0; i < TEST_DATA.length; i++ )
054           {
055             test ( TEST_DATA [ i ] );
056           }
057         }
058         public static void  test ( String  testText )
059           throws Exception
060         //////////////////////////////////////////////////////////////////////
061         {
062           Reader  reader = new StringReader ( testText );
063           CalcScanner  calcScanner = new CalcScanner ( reader );
064           Symbol  symbol = null;
065           loop:
066           while ( ( symbol = calcScanner.nextToken ( ) ).sym != EOF )
067           {
068             System.out.println (
069               "Symbol:  " + symbol + "  Value:  " + symbol.value );
070           }
071         }
072         public static Symbol  scanInteger ( String  text )
073         //////////////////////////////////////////////////////////////////////
074         {
075           try
076           {
077             return new Symbol ( INTEGER, new Integer ( text ) );
078           }
079           catch ( NumberFormatException  ex )
080           {
081             return null;
082           }
083         }
084         //////////////////////////////////////////////////////////////////////
085         //////////////////////////////////////////////////////////////////////
086            private java.io.BufferedReader yy_reader;
087            private int yy_buffer_index;
088            private int yy_buffer_read;
089            private int yy_buffer_start;
090            private int yy_buffer_end;
091            private char yy_buffer[];
092            private int yychar;
093            private int yyline;
094            private int yy_lexical_state;
095    
096            public CalcScanner (java.io.Reader reader) {
097                    this ();
098                    if (null == reader) {
099                            throw (new Error("Error: Bad input stream initializer."));
100                    }
101                    yy_reader = new java.io.BufferedReader(reader);
102            }
103    
104            public CalcScanner (java.io.InputStream instream) {
105                    this ();
106                    if (null == instream) {
107                            throw (new Error("Error: Bad input stream initializer."));
108                    }
109                    yy_reader = new java.io.BufferedReader(new java.io.InputStreamReader(instream));
110            }
111    
112            private CalcScanner () {
113                    yy_buffer = new char[YY_BUFFER_SIZE];
114                    yy_buffer_read = 0;
115                    yy_buffer_index = 0;
116                    yy_buffer_start = 0;
117                    yy_buffer_end = 0;
118                    yychar = 0;
119                    yyline = 0;
120                    yy_lexical_state = YYINITIAL;
121            }
122    
123            private boolean yy_eof_done = false;
124            private final int YYINITIAL = 0;
125            private final int yy_state_dtrans[] = {
126                    0
127            };
128            private void yybegin (int state) {
129                    yy_lexical_state = state;
130            }
131            private char yy_advance ()
132                    throws java.io.IOException {
133                    int next_read;
134                    int i;
135                    int j;
136    
137                    if (yy_buffer_index < yy_buffer_read) {
138                            return yy_buffer[yy_buffer_index++];
139                    }
140    
141                    if (0 != yy_buffer_start) {
142                            i = yy_buffer_start;
143                            j = 0;
144                            while (i < yy_buffer_read) {
145                                    yy_buffer[j] = yy_buffer[i];
146                                    ++i;
147                                    ++j;
148                            }
149                            yy_buffer_end = yy_buffer_end - yy_buffer_start;
150                            yy_buffer_start = 0;
151                            yy_buffer_read = j;
152                            yy_buffer_index = j;
153                            next_read = yy_reader.read(yy_buffer,
154                                            yy_buffer_read,
155                                            yy_buffer.length - yy_buffer_read);
156                            if (-1 == next_read) {
157                                    return YYEOF;
158                            }
159                            yy_buffer_read = yy_buffer_read + next_read;
160                    }
161    
162                    while (yy_buffer_index >= yy_buffer_read) {
163                            if (yy_buffer_index >= yy_buffer.length) {
164                                    yy_buffer = yy_double(yy_buffer);
165                            }
166                            next_read = yy_reader.read(yy_buffer,
167                                            yy_buffer_read,
168                                            yy_buffer.length - yy_buffer_read);
169                            if (-1 == next_read) {
170                                    return YYEOF;
171                            }
172                            yy_buffer_read = yy_buffer_read + next_read;
173                    }
174                    return yy_buffer[yy_buffer_index++];
175            }
176            private void yy_move_start () {
177                    if ((byte) '\n' == yy_buffer[yy_buffer_start]) {
178                            ++yyline;
179                    }
180                    ++yychar;
181                    ++yy_buffer_start;
182            }
183            private void yy_pushback () {
184                    --yy_buffer_end;
185            }
186            private void yy_mark_start () {
187                    int i;
188                    for (i = yy_buffer_start; i < yy_buffer_index; ++i) {
189                            if ((byte) '\n' == yy_buffer[i]) {
190                                    ++yyline;
191                            }
192                    }
193                    yychar = yychar
194                            + yy_buffer_index - yy_buffer_start;
195                    yy_buffer_start = yy_buffer_index;
196            }
197            private void yy_mark_end () {
198                    yy_buffer_end = yy_buffer_index;
199            }
200            private void yy_to_mark () {
201                    yy_buffer_index = yy_buffer_end;
202            }
203            private java.lang.String yytext () {
204                    return (new java.lang.String(yy_buffer,
205                            yy_buffer_start,
206                            yy_buffer_end - yy_buffer_start));
207            }
208            private int yylength () {
209                    return yy_buffer_end - yy_buffer_start;
210            }
211            private char[] yy_double (char buf[]) {
212                    int i;
213                    char newbuf[];
214                    newbuf = new char[2*buf.length];
215                    for (i = 0; i < buf.length; ++i) {
216                            newbuf[i] = buf[i];
217                    }
218                    return newbuf;
219            }
220            private final int YY_E_INTERNAL = 0;
221            private final int YY_E_MATCH = 1;
222            private java.lang.String yy_error_string[] = {
223                    "Error: Internal error.\n",
224                    "Error: Unmatched input.\n"
225            };
226            private void yy_error (int code,boolean fatal) {
227                    java.lang.System.out.print(yy_error_string[code]);
228                    java.lang.System.out.flush();
229                    if (fatal) {
230                            throw new Error("Fatal Error.\n");
231                    }
232            }
233    private int [][] unpackFromString(int size1, int size2, String st)
234        {
235          int colonIndex = -1;
236          String lengthString;
237          int sequenceLength = 0;
238          int sequenceInteger = 0;
239          int commaIndex;
240          String workString;
241          int res[][] = new int[size1][size2];
242          for (int i= 0; i < size1; i++)
243            for (int j= 0; j < size2; j++)
244              {
245                if (sequenceLength == 0) 
246                  { 
247                    commaIndex = st.indexOf(',');
248                    if (commaIndex == -1)
249                      workString = st;
250                    else
251                      workString = st.substring(0, commaIndex);
252                    st = st.substring(commaIndex+1);
253                    colonIndex = workString.indexOf(':');
254                    if (colonIndex == -1)
255                      {
256                        res[i][j] = Integer.parseInt(workString);
257                      }
258                    else 
259                      {
260                        lengthString = workString.substring(colonIndex+1);  
261                        sequenceLength = Integer.parseInt(lengthString);
262                        workString = workString.substring(0,colonIndex);
263                        sequenceInteger = Integer.parseInt(workString);
264                        res[i][j] = sequenceInteger;
265                        sequenceLength--;
266                      }
267                  }
268                else 
269                  {
270                    res[i][j] = sequenceInteger;
271                    sequenceLength--;
272                  }
273              }
274          return res;
275        }
276            private int yy_acpt[] = {
277                    YY_NOT_ACCEPT,
278                    YY_NO_ANCHOR,
279                    YY_NO_ANCHOR,
280                    YY_NO_ANCHOR,
281                    YY_NO_ANCHOR,
282                    YY_NO_ANCHOR,
283                    YY_NO_ANCHOR,
284                    YY_NO_ANCHOR,
285                    YY_NO_ANCHOR,
286                    YY_NO_ANCHOR,
287                    YY_NO_ANCHOR,
288                    YY_NO_ANCHOR,
289                    YY_NO_ANCHOR,
290                    YY_NO_ANCHOR,
291                    YY_NO_ANCHOR,
292                    YY_NO_ANCHOR,
293                    YY_NO_ANCHOR,
294                    YY_NO_ANCHOR,
295                    YY_NO_ANCHOR,
296                    YY_NO_ANCHOR,
297                    YY_NO_ANCHOR,
298                    YY_NO_ANCHOR,
299                    YY_NO_ANCHOR,
300                    YY_NO_ANCHOR,
301                    YY_NO_ANCHOR,
302                    YY_NO_ANCHOR
303            };
304            private int yy_cmap[] = {
305                    0, 0, 0, 0, 0, 0, 0, 0,
306                    0, 0, 0, 0, 0, 0, 0, 0,
307                    0, 0, 0, 0, 0, 0, 0, 0,
308                    0, 0, 0, 0, 0, 0, 0, 0,
309                    0, 1, 0, 0, 0, 2, 0, 0,
310                    3, 4, 5, 6, 0, 7, 0, 8,
311                    9, 9, 9, 9, 9, 9, 9, 9,
312                    9, 9, 10, 11, 12, 13, 14, 0,
313                    0, 15, 15, 15, 15, 15, 15, 15,
314                    15, 15, 15, 15, 15, 15, 15, 15,
315                    15, 15, 15, 15, 15, 15, 15, 15,
316                    15, 15, 15, 0, 0, 0, 16, 17,
317                    0, 15, 15, 15, 15, 18, 15, 15,
318                    15, 19, 15, 15, 15, 15, 15, 15,
319                    15, 15, 20, 15, 21, 15, 15, 22,
320                    15, 15, 15, 0, 0, 0, 0, 0
321                    
322            };
323            private int yy_rmap[] = {
324                    0, 1, 1, 1, 1, 1, 1, 1,
325                    1, 2, 1, 1, 1, 3, 1, 1,
326                    1, 1, 3, 4, 5, 6, 7, 8,
327                    9, 10 
328            };
329            private int yy_nxt[][] = unpackFromString(11,23,
330    "1,19,2,3,4,5,6,7,8,9,21,10,11,22,12,13,14,1,13:4,25,-1:32,9,-1:22,13,-1:5,13,-1,13:6,-1:13,15,-1:18,13,-1:5,13,-1,13,18,13:4,-1:13,16,-1:22,17,-1:18,13,-1:5,13,-1,13:4,20,13,-1:9,13,-1:5,13,-1,13:2,23,13:3,-1:9,13,-1:5,13,-1,13:3,24,13:2");
331            public Symbol nextToken ()
332                    throws java.io.IOException {
333                    char yy_lookahead;
334                    int yy_anchor = YY_NO_ANCHOR;
335                    int yy_state = yy_state_dtrans[yy_lexical_state];
336                    int yy_next_state = YY_NO_STATE;
337                    int yy_last_accept_state = YY_NO_STATE;
338                    boolean yy_initial = true;
339                    int yy_this_accept;
340    
341                    yy_mark_start();
342                    yy_this_accept = yy_acpt[yy_state];
343                    if (YY_NOT_ACCEPT != yy_this_accept) {
344                            yy_last_accept_state = yy_state;
345                            yy_mark_end();
346                    }
347                    while (true) {
348                            yy_lookahead = yy_advance();
349                            yy_next_state = YY_F;
350                            if (YYEOF != yy_lookahead) {
351                                    yy_next_state = yy_nxt[yy_rmap[yy_state]][yy_cmap[yy_lookahead]];
352                            }
353                            if (YY_F != yy_next_state) {
354                                    yy_state = yy_next_state;
355                                    yy_initial = false;
356                                    yy_this_accept = yy_acpt[yy_state];
357                                    if (YY_NOT_ACCEPT != yy_this_accept) {
358                                            yy_last_accept_state = yy_state;
359                                            yy_mark_end();
360                                    }
361                            }
362                            else {
363                                    if (YYEOF == yy_lookahead && true == yy_initial) {
364    
365      return new Symbol ( EOF );
366                                    }
367                                    else if (YY_NO_STATE == yy_last_accept_state) {
368                                            throw (new Error("Lexical Error: Unmatched Input."));
369                                    }
370                                    else {
371                                            yy_to_mark();
372                                            yy_anchor = yy_acpt[yy_last_accept_state];
373                                            if (0 != (YY_END & yy_anchor)) {
374                                                    yy_pushback();
375                                            }
376                                            if (0 != (YY_START & yy_anchor)) {
377                                                    yy_move_start();
378                                            }
379                                            switch (yy_last_accept_state) {
380                                            case 1:
381                                                    { yybegin ( YYINITIAL ); }
382                                            case -2:
383                                                    break;
384                                            case 2:
385                                                    { return new Symbol ( MOD       ); }
386                                            case -3:
387                                                    break;
388                                            case 3:
389                                                    { return new Symbol ( LPAREN    ); }
390                                            case -4:
391                                                    break;
392                                            case 4:
393                                                    { return new Symbol ( RPAREN    ); }
394                                            case -5:
395                                                    break;
396                                            case 5:
397                                                    { return new Symbol ( TIMES     ); }
398                                            case -6:
399                                                    break;
400                                            case 6:
401                                                    { return new Symbol ( PLUS      ); }
402                                            case -7:
403                                                    break;
404                                            case 7:
405                                                    { return new Symbol ( MINUS     ); }
406                                            case -8:
407                                                    break;
408                                            case 8:
409                                                    { return new Symbol ( DIVIDE    ); }
410                                            case -9:
411                                                    break;
412                                            case 9:
413                                                    { return scanInteger ( yytext ( ) ); }
414                                            case -10:
415                                                    break;
416                                            case 10:
417                                                    { return new Symbol ( SEMICOLON ); }
418                                            case -11:
419                                                    break;
420                                            case 11:
421                                                    { return new Symbol ( LT        ); }
422                                            case -12:
423                                                    break;
424                                            case 12:
425                                                    { return new Symbol ( GT        ); }
426                                            case -13:
427                                                    break;
428                                            case 13:
429                                                    { return new Symbol ( VARIABLE, yytext ( ) );  }
430                                            case -14:
431                                                    break;
432                                            case 14:
433                                                    { return new Symbol ( EXP       ); }
434                                            case -15:
435                                                    break;
436                                            case 15:
437                                                    { return new Symbol ( NEQ       ); }
438                                            case -16:
439                                                    break;
440                                            case 16:
441                                                    { return new Symbol ( ASSIGN    ); }
442                                            case -17:
443                                                    break;
444                                            case 17:
445                                                    { return new Symbol ( EQ        ); }
446                                            case -18:
447                                                    break;
448                                            case 18:
449                                                    { return new Symbol ( WRITE     ); }
450                                            case -19:
451                                                    break;
452                                            case 19:
453                                                    { yybegin ( YYINITIAL ); }
454                                            case -20:
455                                                    break;
456                                            case 20:
457                                                    { return new Symbol ( VARIABLE, yytext ( ) );  }
458                                            case -21:
459                                                    break;
460                                            case 21:
461                                                    { yybegin ( YYINITIAL ); }
462                                            case -22:
463                                                    break;
464                                            case 22:
465                                                    { yybegin ( YYINITIAL ); }
466                                            case -23:
467                                                    break;
468                                            case 23:
469                                                    { return new Symbol ( VARIABLE, yytext ( ) );  }
470                                            case -24:
471                                                    break;
472                                            case 24:
473                                                    { return new Symbol ( VARIABLE, yytext ( ) );  }
474                                            case -25:
475                                                    break;
476                                            case 25:
477                                                    { return new Symbol ( VARIABLE, yytext ( ) );  }
478                                            case -26:
479                                                    break;
480                                            default:
481                                                    yy_error(YY_E_INTERNAL,false);
482                                            case -1:
483                                            }
484                                            yy_initial = true;
485                                            yy_state = yy_state_dtrans[yy_lexical_state];
486                                            yy_next_state = YY_NO_STATE;
487                                            yy_last_accept_state = YY_NO_STATE;
488                                            yy_mark_start();
489                                            yy_this_accept = yy_acpt[yy_state];
490                                            if (YY_NOT_ACCEPT != yy_this_accept) {
491                                                    yy_last_accept_state = yy_state;
492                                            }
493                                    }
494                            }
495                    }
496            }
497    }