package oop.ex2.expression;
import java.io.IOException;
import java.util.HashMap;
import oop.ex2.exception.*;
import oop.ex2.main.Tokenizer;
/**
* This class contains 3 public static methods. All 3 methods are used
* to parse text into a mathematical expression. The information is "thrown"
* back and forth from one method to another.
*/
public class ExpressionParser {
/**
* This method uses expression() method to parse the text into mathematical
* expressions, and returns an expression which is the sum of all
* expressions returned from expression() [the sum is calculated according
* to the right operator]
*
* @param st - the Tokenizer parsing the text
* @return - Expression, the sum of all expressions from expression()
* @throws InputException
* @throws IOException
*/
public static Expression sumExpressions(Tokenizer st)
throws InputException, IOException {
boolean endOfLine = false;
Expression temp = expression(st);
int token = Tokenizer.TT_NOTHING;
while (!endOfLine) {
token = st.nextToken();
if ((token == Tokenizer.TT_OPERATOR)
|| (token == Tokenizer.TT_OVERLOADED_OP))
temp = new FatherExpression(st.op, temp, expression(st));
else
endOfLine = true;
}
return temp;
}
public static Expression expression(Tokenizer st) throws InputException, IOException {
Expression result = null;
switch (st.nextToken()) {
case Tokenizer.TT_NUMBER:
result = new NumberExpression(st.nval);
break;
case Tokenizer.TT_VARIABLE:
result = new VariableExpression(st.sval);
break;
case Tokenizer.TT_FUNC:
result = createFunction(st);
break;
case '[':
result = sumExpressions(st);
if (st.ttype != ']')
throw new BracketException("BracketException: "
+ "one too many ']'");
break;
default:
throw new UnexpectedTokenException("Unexpected token on" +
"ExpressionParser.elements(st)");
}
return result;
}
private static Expression createFunction(Tokenizer st)
throws IOException, InputException {
if (InlineManager.getAllInlineFunctions().containsKey(st.sval)) {
InlineFunction temp = InlineManager.getInlineFunction(st.sval);
temp.setArguments(st);
return temp;
}
if (st.sval.equals("MAX"))
return new Max(st);
if (st.sval.equals("MIN"))
return new Min(st);
if (st.sval.equals("POW"))
return new Pow(st);
if (st.sval.equals("MOD"))
return new Mod(st);
if (st.sval.equals("ABS"))
return new Abs(st);
throw new FunctionNameException("Wrong funcion entred " + st.sval);
}
public static HashMap<String, Expression> parseArguments(Tokenizer st)
throws IOException, InputException {
HashMap<String, Expression> result = new HashMap<String, Expression>();
if (st.nextToken() != '{')
throw new UnexpectedTokenException("Missing {");
int argument = 0;
while (true) {
st.ignoreToken(',', true);
switch (st.nextToken()) {
case '}':
st.ignoreToken(',', false);
return result;
case '[':
result.put(String.valueOf(argument++), sumExpressions(st));
break;
case Tokenizer.TT_NUMBER:
result.put(String.valueOf(argument++), new NumberExpression(st.nval));
break;
case Tokenizer.TT_VARIABLE:
result.put(String.valueOf(argument++), new VariableExpression(st.sval));
break;
case Tokenizer.TT_FUNC:
result.put(String.valueOf(argument++), createFunction(st));
break;
default:
throw new UnexpectedTokenException("Unexpected token on function arguments");
}
}
}
}
我知道很久了。表达式对象可以是常量,变量或函数,如MAX {3,2},即3.表达式()使用我构建的标记生成器将文本解析为表达式,sumExpression()使用expression()创建一个新的Expression,它是根据右边运算符组合的两个Expression对象。
我希望它清楚。正如我之前所说,我无法弄清楚如何识别一元减号( - [4]将是-4)的事情。我没有放置我的tokenizer代码,认为没必要。
谢谢!
P.S。 计算顺序定义为从左到右,不考虑运算符的类型。
答案 0 :(得分:3)
(前缀)一元和(中缀)二元运算符之间的区别在于它们出现的上下文。二元运算符总是跟在表达式之后,而一元运算符出现在期望表达式的位置,即在开始时,在运算符之后或在左括号之后。
答案 1 :(得分:1)
“我可以识别出一元减去”错误的情况吗?
看起来如果你在parseArguments
中点击“+”,“ - ”,“*”或“/”,你就会立即创建InlineFunction
类的实例并传递tokenizer作为构造函数的参数。构造函数假定当前标记两侧的东西是该运算符的参数,并且不知道“ - ”实际上应该是什么时候是一元的。那是对的吗?你能告诉我们那个构造函数吗?
我认为很容易判断“ - ”应该被解析为一元减号 - 这将是二元运算符不合法的地方:在一行或括号的开头/etc.-delimited group,紧跟在另一个运算符之后,或者在逗号分隔列表中的表达式的开头(即你的max,min等函数的参数的开头)。