我正在 ParseKit 测试的TDArithmeticParser.m之上构建一个解析器。我用测试扩展了TDArithmeticParserTest.mfailing
:
- (void)testMath {
s = @"10+(2*3)-15";
result = [p parse:s];
TDEquals((double)1.0, result); // result == 0.0
}
问题是我不明白为什么语法不适用于这个测试。算术解析器对应的 BNF 语法为:
expr = term (plusTerm | minusTerm)*;
term = factor (timesFactor | divFactor)*;
plusTerm = '+' term;
minusTerm = '-' term;
factor = phrase exponentFactor | phrase;
timesFactor = '*' factor;
divFactor = '/' factor;
exponentFactor = '^' factor;
phrase = '(' expr ')' | Number;
我将非常感谢任何有助于我识别问题的想法。