1 package java_cup; 2 3 import java_cup.runtime.Symbol; 4 import java.util.Hashtable ; 5 import java.io.InputStream ; 6 7 44 public class lexer { 45 46 47 48 49 50 51 private lexer() { } 52 53 54 55 56 57 58 protected static int next_char; 59 60 61 protected static int next_char2; 62 63 64 protected static int next_char3; 65 66 67 protected static int next_char4; 68 69 70 71 72 protected static final int EOF_CHAR = -1; 73 74 75 76 81 protected static Hashtable keywords = new Hashtable (23); 82 83 84 85 91 protected static Hashtable char_symbols = new Hashtable (11); 92 93 94 95 96 protected static int current_line = 1; 97 98 99 100 101 protected static int current_position = 1; 102 103 104 105 106 protected static int absolute_position = 1; 107 108 109 110 111 public static int error_count = 0; 112 113 114 115 116 public static int warning_count = 0; 117 118 119 120 121 122 125 public static void init() throws java.io.IOException 126 { 127 128 keywords.put("package", new Integer (sym.PACKAGE)); 129 keywords.put("import", new Integer (sym.IMPORT)); 130 keywords.put("code", new Integer (sym.CODE)); 131 keywords.put("action", new Integer (sym.ACTION)); 132 keywords.put("parser", new Integer (sym.PARSER)); 133 keywords.put("terminal", new Integer (sym.TERMINAL)); 134 keywords.put("non", new Integer (sym.NON)); 135 keywords.put("nonterminal",new Integer (sym.NONTERMINAL)); keywords.put("init", new Integer (sym.INIT)); 137 keywords.put("scan", new Integer (sym.SCAN)); 138 keywords.put("with", new Integer (sym.WITH)); 139 keywords.put("start", new Integer (sym.START)); 140 keywords.put("precedence", new Integer (sym.PRECEDENCE)); 141 keywords.put("left", new Integer (sym.LEFT)); 142 keywords.put("right", new Integer (sym.RIGHT)); 143 keywords.put("nonassoc", new Integer (sym.NONASSOC)); 144 145 146 char_symbols.put(new Integer (';'), new Integer (sym.SEMI)); 147 char_symbols.put(new Integer (','), new Integer (sym.COMMA)); 148 char_symbols.put(new Integer ('*'), new Integer (sym.STAR)); 149 char_symbols.put(new Integer ('.'), new Integer (sym.DOT)); 150 char_symbols.put(new Integer ('|'), new Integer (sym.BAR)); 151 char_symbols.put(new Integer ('['), new Integer (sym.LBRACK)); 152 char_symbols.put(new Integer (']'), new Integer (sym.RBRACK)); 153 154 155 next_char = Main.in.read(); 156 if (next_char == EOF_CHAR) { 157 next_char2 = EOF_CHAR; 158 next_char3 = EOF_CHAR; 159 next_char4 = EOF_CHAR; 160 } else { 161 next_char2 = Main.in.read(); 162 if (next_char2 == EOF_CHAR) { 163 next_char3 = EOF_CHAR; 164 next_char4 = EOF_CHAR; 165 } else { 166 next_char3 = Main.in.read(); 167 if (next_char3 == EOF_CHAR) { 168 next_char4 = EOF_CHAR; 169 } else { 170 next_char4 = Main.in.read(); 171 } 172 } 173 } 174 } 175 176 177 178 181 protected static void advance() throws java.io.IOException 182 { 183 int old_char; 184 185 old_char = next_char; 186 next_char = next_char2; 187 if (next_char == EOF_CHAR) { 188 next_char2 = EOF_CHAR; 189 next_char3 = EOF_CHAR; 190 next_char4 = EOF_CHAR; 191 } else { 192 next_char2 = next_char3; 193 if (next_char2 == EOF_CHAR) { 194 next_char3 = EOF_CHAR; 195 next_char4 = EOF_CHAR; 196 } else { 197 next_char3 = next_char4; 198 if (next_char3 == EOF_CHAR) { 199 next_char4 = EOF_CHAR; 200 } else { 201 next_char4 = Main.in.read(); 202 } 203 } 204 } 205 206 207 absolute_position++; 208 current_position++; 209 if (old_char == '\n' || (old_char == '\r' && next_char!='\n')) 210 { 211 current_line++; 212 current_position = 1; 213 } 214 } 215 216 217 218 223 public static void emit_error(String message) 224 { 225 System.err.println(Main.inFileName + ":" + current_line + ":" + current_position + 226 ": Error: JavaCUP: " + message); 227 error_count++; 228 } 229 230 231 232 237 public static void emit_warn(String message) 238 { 239 System.err.println(Main.inFileName + ":" + current_line + ":" + current_position + 240 ": Warning: JavaCUP: " + message); 241 warning_count++; 242 } 243 244 245 246 249 protected static boolean id_start_char(int ch) 250 { 251 254 return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || 255 (ch == '_'); 256 257 } 259 260 261 262 265 protected static boolean id_char(int ch) 266 { 267 return id_start_char(ch) || (ch >= '0' && ch <= '9'); 268 } 269 270 271 272 275 protected static int find_single_char(int ch) 276 { 277 Integer result; 278 279 result = (Integer )char_symbols.get(new Integer ((char)ch)); 280 if (result == null) 281 return -1; 282 else 283 return result.intValue(); 284 } 285 286 287 288 291 protected static void swallow_comment() throws java.io.IOException 292 { 293 294 295 296 if (next_char2 == '*') 297 { 298 299 advance(); advance(); 300 301 302 for (;;) 303 { 304 305 if (next_char == EOF_CHAR) 306 { 307 emit_error("Specification file ends inside a comment"); 308 return; 309 } 310 311 312 if (next_char == '*' && next_char2 == '/') 313 { 314 advance(); 315 advance(); 316 return; 317 } 318 319 320 advance(); 321 } 322 } 323 324 325 if (next_char2 == '/') 326 { 327 328 advance(); advance(); 329 330 331 while (next_char != '\n' && next_char != '\r' && 332 next_char != '\f' && next_char!=EOF_CHAR) 333 advance(); 334 335 return; 336 337 } 338 339 340 emit_error("Malformed comment in specification -- ignored"); 341 advance(); 342 } 343 344 345 346 351 protected static Symbol do_code_string() throws java.io.IOException 352 { 353 StringBuffer result = new StringBuffer (); 354 355 356 advance(); advance(); 357 358 359 while (!(next_char == ':' && next_char2 == '}')) 360 { 361 362 if (next_char == EOF_CHAR) 363 { 364 emit_error("Specification file ends inside a code string"); 365 break; 366 } 367 368 369 result.append(new Character ((char)next_char)); 370 advance(); 371 } 372 373 374 advance(); advance(); 375 return new Symbol(sym.CODE_STRING, result.toString()); 376 } 377 378 379 380 385 protected static Symbol do_id() throws java.io.IOException 386 { 387 StringBuffer result = new StringBuffer (); 388 String result_str; 389 Integer keyword_num; 390 char buffer[] = new char[1]; 391 392 393 buffer[0] = (char)next_char; 394 result.append(buffer,0,1); 395 advance(); 396 397 398 while(id_char(next_char)) 399 { 400 buffer[0] = (char)next_char; 401 result.append(buffer,0,1); 402 advance(); 403 } 404 405 406 result_str = result.toString(); 407 keyword_num = (Integer )keywords.get(result_str); 408 409 410 if (keyword_num != null) 411 return new Symbol(keyword_num.intValue()); 412 413 414 return new Symbol(sym.ID, result_str); 415 } 416 417 418 419 426 public static Symbol next_token() throws java.io.IOException 427 { 428 return real_next_token(); 429 } 430 431 432 433 437 public static Symbol debug_next_token() throws java.io.IOException 438 { 439 Symbol result = real_next_token(); 440 System.out.println("# next_Symbol() => " + result.sym); 441 return result; 442 } 443 444 445 446 450 protected static Symbol real_next_token() throws java.io.IOException 451 { 452 int sym_num; 453 454 for (;;) 455 { 456 457 if (next_char == ' ' || next_char == '\t' || next_char == '\n' || 458 next_char == '\f' || next_char == '\r') 459 { 460 461 advance(); 462 continue; 463 } 464 465 466 sym_num = find_single_char(next_char); 467 if (sym_num != -1) 468 { 469 470 advance(); 471 return new Symbol(sym_num); 472 } 473 474 475 if (next_char == ':') 476 { 477 478 if (next_char2 != ':') 479 { 480 advance(); 481 return new Symbol(sym.COLON); 482 } 483 484 485 advance(); 486 if (next_char2 == '=') 487 { 488 advance(); advance(); 489 return new Symbol(sym.COLON_COLON_EQUALS); 490 } 491 else 492 { 493 494 return new Symbol(sym.COLON); 495 } 496 } 497 498 500 if (next_char == '%') { 501 advance(); 502 if ((next_char == 'p') && (next_char2 == 'r') && (next_char3 == 'e') && 503 (next_char4 == 'c')) { 504 advance(); 505 advance(); 506 advance(); 507 advance(); 508 return new Symbol(sym.PERCENT_PREC); 509 } else { 510 emit_error("Found extraneous percent sign"); 511 } 512 } 513 514 515 if (next_char == '/' && (next_char2 == '*' || next_char2 == '/')) 516 { 517 518 swallow_comment(); 519 continue; 520 } 521 522 523 if (next_char == '{' && next_char2 == ':') 524 return do_code_string(); 525 526 527 if (id_start_char(next_char)) return do_id(); 528 529 530 if (next_char == EOF_CHAR) return new Symbol(sym.EOF); 531 532 533 emit_warn("Unrecognized character '" + 534 new Character ((char)next_char) + "'(" + next_char + 535 ") -- ignored"); 536 537 538 advance(); 539 } 540 } 541 542 543 } 544 545 | Popular Tags |