1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
// $Id: Scanner.java,v 1.3 1999/11/04 14:02:18 shields Exp $
// This software is subject to the terms of the IBM Jikes Compiler
// License Agreement available at the following URL:
// http://www.ibm.com/research/jikes.
// Copyright (C) 1983, 1999, International Business Machines Corporation
// and others. All Rights Reserved.
// You must accept the terms of that agreement to use this software.
//
// The Scanner object
//
class Scanner implements exprsym
{
int next_byte;
Option option;
LexStream lex_stream;
Scanner(Option option, LexStream lex_stream)
{
this.lex_stream = lex_stream;
this.option = option;
}
//
//
//
void skip_spaces() throws java.io.IOException
{
while (next_byte != '\n' && Character.isSpace((char) next_byte))
next_byte = System.in.read();
return;
}
//
//
//
String scan_symbol() throws java.io.IOException
{
StringBuffer buffer = new StringBuffer();
while (next_byte != '\n' && (! Character.isSpace((char) next_byte)))
{
buffer.append((char) next_byte);
}
return buffer.toString();
}
//
//
//
void scan() throws java.io.IOException
{
//
// Do not use token indexed at location 0.
//
Token start_token = new Token();
start_token.kind = 0;
start_token.name = "";
lex_stream.tokens.addElement(start_token);
next_byte = System.in.read();
for (skip_spaces(); next_byte != '\n'; skip_spaces())
{
Token token = new Token();
switch(next_byte)
{
case '+':
token.kind = TK_PLUS;
token.name = "+";
break;
case '-':
token.kind = TK_MINUS;
token.name = "-";
break;
case '*':
token.kind = TK_STAR;
token.name = "*";
break;
case '/':
token.kind = TK_SLASH;
token.name = "/";
break;
case '(':
token.kind = TK_LPAREN;
token.name = "(";
break;
case ')':
token.kind = TK_RPAREN;
token.name = ")";
break;
default:
StringBuffer buffer = new StringBuffer();
if (! Character.isDigit((char) next_byte))
{
token.kind = TK_ERROR;
while (next_byte != '\n' && (! Character.isSpace((char) next_byte)))
{
buffer.append((char) next_byte);
next_byte = System.in.read();
}
System.out.println("The token \"" + buffer.toString() + "\" is illegal");
}
else
{
token.kind = TK_NUMBER;
do
{
buffer.append((char) next_byte);
next_byte = System.in.read();
} while (next_byte != '\n' && Character.isDigit((char) next_byte));
}
token.name = buffer.toString();
}
if (token.kind != TK_ERROR && token.kind != TK_NUMBER)
next_byte = System.in.read();
lex_stream.tokens.addElement(token);
}
Token end_token = new Token();
end_token.kind = TK_EOF;
end_token.name = "";
lex_stream.tokens.addElement(end_token);
return;
}
}
|