How to debug an invalid ParseKit Grammar?

2019-05-18 08:32发布

I am trying to write a grammar for ParseKit so that it matches basic propositional logic sentences in an iphone app. Can someone please tell me where im going wrong.

@start = wff;
wff = disjunction (implies disjunction)?;
disjunction = conjuction (or conjuction)*;
conjunction = notExpression (and notExpression)*;
notExpression = (not | not primaryExpression);
primaryExpression = variable | lbracket wff rbracket;
variable = p | q | r;

p = 'P';
q = 'Q';
r = 'R';

implies = '→';
and = '∧';
or = '∨';
not = '¬';
lbracket = '(';
rbracket = ')';

Also how would i go about adding some extra call backs so that I can create a parse tree from the grammar.

1条回答
Summer. ? 凉城
2楼-- · 2019-05-18 08:52

Developer of ParseKit here.

I see one obvious problem: conjunction is misspelled in a couple of places in your grammar.


ParseKit's Grammar Parser's error messages are not the greatest. Ideally, you would receive a nice error message leading you to the problem (but, hey it's open source so anyone is welcome to contribute a fix of this nature).

However, I can tell you how I find these issues:

  1. Update your ParseKit working copy to head of the google code trunk.
  2. In Xcode, turn on breakpoints and add a breakpoint for all exceptions
  3. Place your ParseKit grammar in this file: ~/Desktop/grammar.txt
  4. (Optional) Place any example input in this file: ~/Desktop/input.txt
  5. Run the ParseKit DebugApp target in Debug configuration.
  6. Click the big Run button in the window that appears when the app runs.
  7. Wait for an exception to be thrown. Breakpoint is hit.

If there is a bug in your grammar, you will find the exception has most likely occurred in a ParseKit Assembler callback like:

- (void)parser:(PKParser *)p didMatchExpression:(PKAssembly *)a

print a to the debug console. You'll see something like:

(lldb) po a
(PKAssembly *) $1 = 0x00000001075a9010 [] /conjuction/ ^(/or/ /conjuction/)/*

This is a printout of the PKAssembly object passed into your callback. Here you can see where ParseKit's grammar parser failed to parse your grammar. It appears that the ParseKit Grammar Parser failed while parsing this expression:

conjuction (or conjuction)*;

And from the position of the ^ (caret), you can tell the parser choked right after seeing the first conjuction.

This expression is located in just one line in your grammar:

disjunction = conjuction (or conjuction)*;

That's how I noticed that there was a misspelling in this line.

This approach will invariably lead you to the problematic line in your grammar. Hope that helps.

查看更多
登录 后发表回答