exprscanner.java

来自「国外的数据结构与算法分析用书」· Java 代码 · 共 156 行

JAVA
156
字号
import java.io.Reader;

/**	Class for scanning an expression */
public class ExprScanner
{
	/**	The last character scanned */
	protected char lastChar;
    
	/**	The source for the scanner */
	protected Reader reader;

	/**	Constructor */
    
	/**	Construct an expression scanner on the specified source. <br>
		Analysis: Time = O(1) */
	public ExprScanner(Reader newReader)
	{
		reader = newReader;
	}
    
	/**	Initialize the scanner by reading the 1 character look ahead. <br>
		Analysis: Time = O(1) */
	public void initialize()
	{
		readChar();  // needed for the 1 character read ahead
	}
    
	/**	Read the next character from the expression. <br>	
		Analysis: Time = O(1) */
	public void readChar()
	{
		try
		{
			lastChar = (char)reader.read();
		}catch(java.io.IOException e)
		{
			throw new RuntimeException();
		}
	}
    
	/**	The current token from input */
	public Object token;
    
	/**	The integer specification for the current token */
	public int tokenType;
    
	/**	Get the next token from the inputr stream with 1 character read ahead.
		Analysis: Time = O(n), n = number of characters to the end of the token */
	public void getToken()
	{
		/**	scan past blanks, tabs, carriage returns and line feeds */
		while (Character.isWhitespace(lastChar))
	    		readChar();
	
		if (Character.isLetter(lastChar))
		{
			getIdentifier();
			tokenType = 0;
		}
		else if (Character.isDigit(lastChar))
		{
			getNumber();
			tokenType = 1;
		}
		else if (lastChar == '=')
		{
			token = "=";
			tokenType = 2;
			readChar();
		}
		else if (lastChar == ';')
		{
			token = ";";
			tokenType = 3;
			// Don't read ahead as this is the last character of the expression
		}
		else if (lastChar == '+')
		{
			token = "+";
			tokenType = 4;
			readChar();
		}
		else if (lastChar == '-')
		{
			token = "-";
			tokenType = 5;
			readChar();
		}
		else if (lastChar == '*')
		{
			token = "*";
			tokenType = 6;
			readChar();
		}
		else if (lastChar == '/')
		{
			token = "/";
			tokenType = 7;
			readChar();
		}
		else if (lastChar == '(')
		{
			token = "(";
			tokenType = 8;
			readChar();
		}
		else if (lastChar == ')')
		{
			token = ")";
			tokenType = 9;
			readChar();
		}
		else if (lastChar == '.')
		{
			token = ".";
			tokenType = 10;
			readChar();
		}
		else
		{
			System.out.print("*****Illegal character for token start: '");
			System.out.print(lastChar);
			System.out.println("'");
			readChar();
		}
	}
    
	/**	Read in the characters of an identifier. 
		Analysis: Time = O(n), n = length of the identifier */
	public void getIdentifier()
	{
		StringBuffer newToken = new StringBuffer();
		while (Character.isLetterOrDigit(lastChar))
		{
			newToken.append(lastChar);
			readChar();
		}
		token = new String(newToken);
	
	}
    
	/**	Read in the digits of an integer. 
		Analysis: Time = O(n), n = length of the integer */
	public void getNumber()
	{
		String newTokenString = new String();
		while (Character.isDigit(lastChar))
		{
			newTokenString += lastChar;
			readChar();
		}
		token = java.lang.Integer.valueOf(newTokenString); // new String(newToken);
	}    

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?