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