1 2 3 13 14 22 23 24 25 import java_cup.runtime.*; 26 27 28 34 class Lexer implements java_cup.runtime.Scanner { 35 36 37 public static final int YYEOF = -1; 38 39 40 private static final int YY_BUFFERSIZE = 16384; 41 42 43 public static final int YYINITIAL = 0; 44 45 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 62 private static final int [] YY_ACTION = yy_unpack_YY_ACTION(); 63 64 private static final String 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 packed, int offset, int [] result) { 76 int i = 0; 77 int j = offset; 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 91 private static final int [] yy_rowMap = yy_unpack_yy_rowMap(); 92 93 private static final String 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 packed, int offset, int [] result) { 105 int i = 0; 106 int j = offset; 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 118 private static final int [] yytrans = yy_unpack_yytrans(); 119 120 private static final String 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 packed, int offset, int [] result) { 133 int i = 0; 134 int j = offset; 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 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 152 private static final String YY_ERROR_MSG[] = { 153 "Unkown internal scanner error", 154 "Error: could not match input", 155 "Error: pushback value was too large" 156 }; 157 158 161 private static final int [] YY_ATTRIBUTE = yy_unpack_YY_ATTRIBUTE(); 162 163 private static final String 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 packed, int offset, int [] result) { 174 int i = 0; 175 int j = offset; 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 186 private java.io.Reader yy_reader; 187 188 189 private int yy_state; 190 191 192 private int yy_lexical_state = YYINITIAL; 193 194 196 private char yy_buffer[] = new char[YY_BUFFERSIZE]; 197 198 199 private int yy_markedPos; 200 201 202 private int yy_pushbackPos; 203 204 205 private int yy_currentPos; 206 207 208 private int yy_startRead; 209 210 212 private int yy_endRead; 213 214 215 private int yyline; 216 217 218 private int yychar; 219 220 224 private int yycolumn; 225 226 229 private boolean yy_atBOL = true; 230 231 232 private boolean yy_atEOF; 233 234 235 private boolean yy_eof_done; 236 237 238 241 private Symbol symbol(int type) { 242 return new Symbol(type, yyline, yycolumn); 243 } 244 245 247 private Symbol symbol(int type, Object value) { 248 return new Symbol(type, yyline, yycolumn, value); 249 } 250 251 252 258 Lexer(java.io.Reader in) { 259 this.yy_reader = in; 260 } 261 262 268 Lexer(java.io.InputStream in) { 269 this(new java.io.InputStreamReader (in)); 270 } 271 272 273 280 private boolean yy_refill() throws java.io.IOException { 281 282 283 if (yy_startRead > 0) { 284 System.arraycopy(yy_buffer, yy_startRead, 285 yy_buffer, 0, 286 yy_endRead-yy_startRead); 287 288 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 297 if (yy_currentPos >= yy_buffer.length) { 298 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 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 321 public final void yyclose() throws java.io.IOException { 322 yy_atEOF = true; 323 yy_endRead = yy_startRead; 324 325 if (yy_reader != null) 326 yy_reader.close(); 327 } 328 329 330 340 public final void yyreset(java.io.Reader reader) throws java.io.IOException { 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 354 public final int yystate() { 355 return yy_lexical_state; 356 } 357 358 359 364 public final void yybegin(int newState) { 365 yy_lexical_state = newState; 366 } 367 368 369 372 public final String yytext() { 373 return new String ( yy_buffer, yy_startRead, yy_markedPos-yy_startRead ); 374 } 375 376 377 388 public final char yycharat(int pos) { 389 return yy_buffer[yy_startRead+pos]; 390 } 391 392 393 396 public final int yylength() { 397 return yy_markedPos-yy_startRead; 398 } 399 400 401 415 private void yy_ScanError(int errorCode) { 416 String message; 417 try { 418 message = YY_ERROR_MSG[errorCode]; 419 } 420 catch (ArrayIndexOutOfBoundsException e) { 421 message = YY_ERROR_MSG[YY_UNKNOWN_ERROR]; 422 } 423 424 throw new Error (message); 425 } 426 427 428 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 448 private void yy_do_eof() throws java.io.IOException { 449 if (!yy_eof_done) { 450 yy_eof_done = true; 451 yyclose(); 452 } 453 } 454 455 456 463 public java_cup.runtime.Symbol next_token() throws java.io.IOException { 464 int yy_input; 465 int yy_action; 466 467 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 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 yy_currentPos = yy_currentPos_l; 550 yy_markedPos = yy_markedPos_l; 551 boolean eof = yy_refill(); 552 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 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 (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 (1)); } 605 case 18: break; 606 case 2: 607 { } 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 ("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 |