我在antlr4中尝试了一个简单的计算器程序。 +,-和*正常工作。但是'/'不能正常工作,实际上我收到如下错误:
输入'/'不匹配,期望{BIN_OPERATOR,')'}
有人可以帮我吗?
// Calculator program
compilationUnit : e EOF{
System.out.println(e_value);
};
e returns [int e_value]:
e1 = e BIN_OPERATOR e2 =e{
int value1 = $e1.e_value;
int value2 = $e2.e_value;
String op = $BIN_OPERATOR.text;
int output = -1;
if (op.equals("+"))
{output = value1+value2;}
else if (op.equals("-"))
{output = value1-value2;}
else if (op.equals("*"))
{output = value1*value2;}
else if (op.equals("/"))
{output = value1/value2;}
$e_value = output;
}
| '(' e1 = e ')' {
$e_value = $e1.e_value;
}
|IntegerLiteral {
String int_val = $IntegerLiteral.text;
int output = Integer.parseInt(int_val);
$e_value = output;
};
// LEXER
BIN_OPERATOR : '+' | '-' | '*' | '/' ;
// §3.10.1 Integer Literals
IntegerLiteral
: [0-9]+;