⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tokenbuffer.java

📁 用编译原理方法计算表达式的值,例如(2+3)*84+3这样的复杂表达式 深入体现了面向对象的方法,代码可以很容易进行overwrite
💻 JAVA
字号:
/**
 * Course - CS601 OO Programming
 * Instructor - Terence Parr
 * Assignment - 4
 *
 * Ideas represented in this class have been recycled from Terence Parr's
 * http://www.antlr.org/book/byhand.pdf. Thanks Terence
 */
public class TokenBuffer {
	private Lexer _lexer;
	private int _lookahead;
	private Token[] _tokens;
	
	/**
	 * Constructs a buffer of tokens from the lexer.
	 * @param lexer Lexer is the stream of tokens used for this token buffer
	 * @param lookAhead lookAhead is how far in advance to grab tokens from the 
	 * lexer
	 */
	public TokenBuffer(Lexer lexer, int lookAhead) {
		this._lexer = lexer;
		this._lookahead = lookAhead;
		_tokens = new Token[lookAhead];
		
		for (int i = 0; i < lookAhead; i++) {
			_tokens[i] = lexer.getToken();
			if (_tokens[i].getType() == Token.EOT) break;
		}
		
	}
	
	/**
	 * 
	 * @param i I is the 1 based token index.
	 * @return Returns the token at the specified (1 based) position in this buffer
	 */
	public Token LA(int i) {
		return _tokens[i-1];
	}

	/**
	 * removes token at position 1 and advances the buffer by 1
	 *
	 */
	public void consume() {
		for (int i = 1; i < _lookahead; i++) {
			_tokens[i-1] = _tokens[i];
		}
		_tokens[_lookahead-1]= _lexer.getToken();
	}
}

⌨️ 快捷键说明

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