How to debug an invalid ParseKit Grammar?

烂漫一生 提交于 2019-12-06 04:12:36

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!