Question

I am building a parser on top of the TDArithmeticParser.m of ParseKit Tests. I extended the TDArithmeticParserTest.m with the failing test:

- (void)testMath {
    s = @"10+(2*3)-15";
    result = [p parse:s];
    TDEquals((double)1.0, result); // result == 0.0
}

The problem is that I don't understand why the grammar is not working with this test. The corresponding BNF-grammar of the arithmetic parser is:

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;

I would be very thankful for any ideas that helps me identifying the problem.

Was it helpful?

Solution

Developer of ParseKit here.

First, note: TDArithmeticParser is just some (not-terribly-robust) sample code included in the test bundle for ParseKit. It is not part of ParseKit itself.

Looks like this is a bug/deficiency in the TDArithmeticParser class where -15 is recognized as negative fifteen, not subtract fifteen.

If you add whitespace, this issue is resolved:

s = @"10+(2*3)- 15";

The opposite behavior is also possible with a small change, but then a stand-alone -15 would not be recognized as valid input (which may or may not be a problem for you).

Both could be supported simultaneously with some changes to the Grammar and the Assembler callbacks.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top