4

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.

Muscovado
  • 53
  • 5

1 Answers1

3

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.

Todd Ditchendorf
  • 11,217
  • 14
  • 69
  • 123
  • But is it really a problem of the TDArithmeticParser? Sadly for me changing the text that I want to parse is not much of a good option. I search for a better solution then to do some kind of "pre-parsing" to prevent this problem to occur. The BNF-grammar looks fine to me and the code corresponds to the grammar… Does the parser needs a further setup? I am just using what comes out of [PKParser parser] like the TDArithmeticParser does… – Muscovado Nov 13 '13 at 10:12