我正在尝试为向量构建一个解析器(并最终将其用于矩阵)
在我的测试课中,我有这个:
Parser parser = new Parser();
try {
VectorExpressionNode expression = parser.parse("{1.2, 4.2, 5.2}+{1.0, 2.0,3.0}");
System.out.println("The value of the expression is "+expression.getValue()[0]);
}
catch (ParserException e) {
System.out.println("Parser Exception" + e.getMessage());
}
catch (EvaluationException e) {
System.out.println("Evaluation Exception " + e.getMessage());
}
但是它不是解析行。我确定问题出在我用于数组的正则表达式上。这是我使用的方法(仅供参考:Tokenize-对输入令牌进行令牌化的类)
private static Tokenizer createExpressionTokenizer()
{
Tokenizer tokenizer = new Tokenizer();
tokenizer.add("[+-]", Token.PLUSMINUS);
tokenizer.add("[*]", Token.MULT);
tokenizer.add("\\{\\s*(\\d+(\\s*,\\s*\\d+)*\\s*)?\\}", Token.ARRAY);//I think the program doesn't get this
return tokenizer;
}
运行程序时,我注意到它无法通过“ if(m.find())”行。这是从输入中读取令牌的方法:
public void tokenize(String str) throws Exception {
String s = new String(str);
int totalLength = s.length();
tokens.clear();
while(!s.equals("")) {
int remaining = s.length();
boolean match = false;
for (TokenInfo info: tokenInfos) {
Matcher m = info.regex.matcher(s);
System.out.println(m.toString());
if (m.find()) { //the program is not entering this block
match = true;
String tok = m.group().trim();
tokens.add(new Token(info.token, tok, totalLength - remaining));
s = m.replaceFirst("");
break;
}
}
if (!match) throw new Exception(); //as a result, throws this exception
}
}