📄 evalparser.java
字号:
import java.math.BigDecimal;
/**
*
* Course - CS601 OO Programming
* Instructor - Terence Parr
* Assignment - 4
*
* EvalParser implements the grammar specified for the homework as shown below
* <pre>
* expression
: addExpression ( "<" addExpression )?
;
addExpression
: multExpression ( "+" multExpression )*
;
multExpression
: atom ( "*" atom )*
;
atom: INTEGER
| "(" addExpression ")"
;
*
* Essentially, there is one method for each term in the grammar with the
* exception of INTEGER
*
* Note: Ideas represented in this class have been recycled/refitted from Terence Parr's
* http://www.antlr.org/book/byhand.pdf. Thanks Terence
*/
public class EvalParser {
/** used for buffering tokens **/
private TokenBuffer _buffer;
public EvalParser(TokenBuffer buffer) {
_buffer = buffer;
}
/**
* Expression evaluates the entire expression
* @return Returns the value of the expression found
* @throws IllegalArgumentException if invalid expression is parsed
* when the less expression is found, expression() needs to evaluate
* the expressions both before and behind the less expression
*/
public int expression() {
Token result1 = addExpression();
if(isType(1,Token.LESS)){
_buffer.consume();
Token result2=addExpression();
match(Token.EOT);
if(Integer.parseInt(result1.getText())<Integer.parseInt(result2.getText())){
return 1;
}
else return 0;
}
else{
match(Token.EOT);
return Integer.parseInt(result1.getText());
}
}
/**
* Recursive method to evaluate the addExpression grammar
* @return Returns a token which is the computational result of
* evaluating (atom,+addExpression)
*/
private Token addExpression() {
Token atom = multExpression();
// this loop handles matching 0 or more (+,multExpression) per grammar
for (;;) {
if (isType(1, Token.ADDITION)) {
_buffer.consume();
Token token = addExpression();
atom = add(atom, token);
} else {
break;
}
}
return atom;
}
/**
* Recursive method of evaluate the multExpression grammer
* @return Returns a token which is the computational result of
* evaluating (atom,*multExpression)
*
*/
private Token multExpression() {
Token atom=atom();
for(;;){
if(isType(1,Token.MULTIPLICATION)){
_buffer.consume();
Token token=multExpression();
atom=multiply(atom,token);
}
else break;
}
return atom;
}
/**
*
* @return Returns a {@link Token} of type INTEGER
* @throws IllegalArgumentException if the token type is not of right-parenthesis when the expression starts with left-parenthesis
* @throws IllegalArgumentException if the token is not type INTEGER or expression starts with left-parenthesis
*
*/
private Token atom(){
if(isType(1,Token.INTEGER)){
Token atom=_buffer.LA(1);
_buffer.consume();
return atom;
}
if(isType(1,Token.L_PARENTHESIS)){
_buffer.consume();
Token sum=addExpression();
match(Token.R_PARENTHESIS);
_buffer.consume();
return sum;
}
else
throw new IllegalArgumentException("Invalid Input");
}
/**
* @throws IllegalArgumentException if the type of token in position 1
* of the token buffer does not match the specified type
*/
private void match(int type) {
if (!isType(1, type)) {
String s = "Invalid Expression. The token '" + _buffer.LA(1).getText()
+ "' is invalid";
throw new IllegalArgumentException(s);
}
}
private boolean isType(int i, int type) {
Token t = _buffer.LA(i);
return t.getType() == type;
}
/**
*
* @param left Token of type INTEGER
* @param right Token of type INTEGER
* @return Returns the sum of the two tokens
* @throws IllegalArgumentException If the sum of the two numbers is too
* large (> largest int) then throw error
*/
Token add(Token left, Token right) {
BigDecimal l = new BigDecimal(left.getText());
BigDecimal r = new BigDecimal(right.getText());
BigDecimal sum = l.add(r);
validateResult(sum);
return new Token(Token.INTEGER, "" + sum.intValue());
}
/**
* @param left of type INTEGER
* @param right of type INTEGER
* @return Returns the product
* @throws IllegalArgumentException If the product of the two numbers is too
* large (> largest int) then throw error
*/
private Token multiply(Token left, Token right) {
BigDecimal l = new BigDecimal(left.getText());
BigDecimal r = new BigDecimal(right.getText());
BigDecimal product = l.multiply(r);
validateResult(product);
return new Token(Token.INTEGER, product.intValue() + "");
}
/**
*
* @throws IllegalArgumentException if the result of one of our computations
* is no longer an integer
*/
private void validateResult(BigDecimal result) {
try {
result.intValueExact();
} catch (ArithmeticException ex) {
throw new IllegalArgumentException("The result of this expression "
+ "exceeds the maximum integer: "
+ Integer.MAX_VALUE, ex);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -