0

我刚刚遇到 ParseKit,并使用http://parsekit.com/iphone.html中的说明将其合并到我的 IOS 项目中。我从 Google Code 得到它,签出的版本是 1543。一切编译和构建都很好。

然后我尝试使用其中一篇文章中的(最终工作)简单示例 - Can't get simple ParseKit example working

NSString *test = @"FOO : BAR";

NSString *grammar = @"@start = foo ':' bar; foo = 'FOO'; bar = 'BAR';";

PKParser *parser = nil;
NSError *err = nil;
parser = [[PKParserFactory factory] parserFromGrammar:grammar assembler:self error:&err];
NSLog(@"parser: error1: %@", err);

[parser parse:test error:&err];
NSLog(@"parser: error2: %@", err);


- (void)didMatchFoo:(PKAssembly *)a
{
   NSLog(@"FOO");
}

- (void)didMatchBar:(PKAssembly *)a
{
   NSLog(@"BAR");
} 

当执行到达该行时

    parser = [[PKParserFactory factory] parserFromGrammar:grammar assembler:self error:&err];

我在控制台中看到很多错误消息。didMatchFoo 和 didMatchBar 不会触发。

以下是来自控制台的消息:

objc[53244]: EXCEPTIONS: throwing 0xb47f040 (object 0xa17b7b0, a PEGRecognitionException)
objc[53244]: EXCEPTIONS: searching through frame [ip=0xf23ac sp=0xbfffd2e0] for exception 0xb47f020
objc[53244]: EXCEPTIONS: catch(PEGRecognitionException)
objc[53244]: EXCEPTIONS: unwinding through frame [ip=0xf23ac sp=0xbfffd2e0] for exception 0xb47f020
objc[53244]: EXCEPTIONS: handling exception 0xb47f020 at 0xf2406
objc[53244]: EXCEPTIONS: finishing handler
objc[53244]: EXCEPTIONS: throwing 0xb47f5e0 (object 0xa17b7b0, a PEGRecognitionException)
objc[53244]: EXCEPTIONS: searching through frame [ip=0xf23ac sp=0xbfffd170] for exception 0xb47f5c0
objc[53244]: EXCEPTIONS: catch(PEGRecognitionException)
objc[53244]: EXCEPTIONS: unwinding through frame [ip=0xf23ac sp=0xbfffd170] for exception 0xb47f5c0
objc[53244]: EXCEPTIONS: handling exception 0xb47f5c0 at 0xf2406
objc[53244]: EXCEPTIONS: finishing handler
objc[53244]: EXCEPTIONS: throwing 0xa589360 (object 0xa17b7b0, a PEGRecognitionException)
objc[53244]: EXCEPTIONS: searching through frame [ip=0xf23ac sp=0xbfffd230] for exception 0xa589340
objc[53244]: EXCEPTIONS: catch(PEGRecognitionException)
objc[53244]: EXCEPTIONS: unwinding through frame [ip=0xf23ac sp=0xbfffd230] for exception 0xa589340
objc[53244]: EXCEPTIONS: handling exception 0xa589340 at 0xf2406
objc[53244]: EXCEPTIONS: finishing handler
objc[53244]: EXCEPTIONS: throwing 0xa589470 (object 0xa17b7b0, a PEGRecognitionException)
objc[53244]: EXCEPTIONS: searching through frame [ip=0xf23ac sp=0xbfffd290] for exception 0xa589450
objc[53244]: EXCEPTIONS: catch(PEGRecognitionException)
objc[53244]: EXCEPTIONS: unwinding through frame [ip=0xf23ac sp=0xbfffd290] for exception 0xa589450
objc[53244]: EXCEPTIONS: handling exception 0xa589450 at 0xf2406
objc[53244]: EXCEPTIONS: finishing handler
objc[53244]: EXCEPTIONS: throwing 0xa58bd80 (object 0xa17b7b0, a PEGRecognitionException)
objc[53244]: EXCEPTIONS: searching through frame [ip=0xf23ac sp=0xbfffd380] for exception 0xa58bd60
objc[53244]: EXCEPTIONS: catch(PEGRecognitionException)
objc[53244]: EXCEPTIONS: unwinding through frame [ip=0xf23ac sp=0xbfffd380] for exception 0xa58bd60
objc[53244]: EXCEPTIONS: handling exception 0xa58bd60 at 0xf2406
objc[53244]: EXCEPTIONS: finishing handler
objc[53244]: EXCEPTIONS: throwing 0x8b848e0 (object 0xa17b7b0, a PEGRecognitionException)
objc[53244]: EXCEPTIONS: searching through frame [ip=0xf23ac sp=0xbfffd210] for exception 0x8b848c0
objc[53244]: EXCEPTIONS: catch(PEGRecognitionException)
objc[53244]: EXCEPTIONS: unwinding through frame [ip=0xf23ac sp=0xbfffd210] for exception 0x8b848c0
objc[53244]: EXCEPTIONS: handling exception 0x8b848c0 at 0xf2406
objc[53244]: EXCEPTIONS: finishing handler
objc[53244]: EXCEPTIONS: throwing 0x8ba5610 (object 0xa17b7b0, a PEGRecognitionException)
objc[53244]: EXCEPTIONS: searching through frame [ip=0xf23ac sp=0xbfffd2d0] for exception  0x8ba55f0
objc[53244]: EXCEPTIONS: catch(PEGRecognitionException)
objc[53244]: EXCEPTIONS: unwinding through frame [ip=0xf23ac sp=0xbfffd2d0] for exception 0x8ba55f0
objc[53244]: EXCEPTIONS: handling exception 0x8ba55f0 at 0xf2406
objc[53244]: EXCEPTIONS: finishing handler
objc[53244]: EXCEPTIONS: throwing 0xb57a160 (object 0xa17b7b0, a PEGRecognitionException)
objc[53244]: EXCEPTIONS: searching through frame [ip=0xf23ac sp=0xbfffd330] for exception 0xb57a140
objc[53244]: EXCEPTIONS: catch(PEGRecognitionException)
objc[53244]: EXCEPTIONS: unwinding through frame [ip=0xf23ac sp=0xbfffd330] for exception 0xb57a140
objc[53244]: EXCEPTIONS: handling exception 0xb57a140 at 0xf2406
objc[53244]: EXCEPTIONS: finishing handler
objc[53244]: EXCEPTIONS: throwing 0xb57a730 (object 0xa17b7b0, a PEGRecognitionException)
objc[53244]: EXCEPTIONS: searching through frame [ip=0xf23ac sp=0xbfffd2e0] for exception 0xb57a710
objc[53244]: EXCEPTIONS: catch(PEGRecognitionException)
objc[53244]: EXCEPTIONS: unwinding through frame [ip=0xf23ac sp=0xbfffd2e0] for exception 0xb57a710
objc[53244]: EXCEPTIONS: handling exception 0xb57a710 at 0xf2406
objc[53244]: EXCEPTIONS: finishing handler
objc[53244]: EXCEPTIONS: throwing 0xb57ac90 (object 0xa17b7b0, a PEGRecognitionException)
objc[53244]: EXCEPTIONS: searching through frame [ip=0xf23ac sp=0xbfffd170] for exception 0xb57ac70
objc[53244]: EXCEPTIONS: catch(PEGRecognitionException)
objc[53244]: EXCEPTIONS: unwinding through frame [ip=0xf23ac sp=0xbfffd170] for exception 0xb57ac70
objc[53244]: EXCEPTIONS: handling exception 0xb57ac70 at 0xf2406
objc[53244]: EXCEPTIONS: finishing handler
objc[53244]: EXCEPTIONS: throwing 0xb57af30 (object 0xa17b7b0, a PEGRecognitionException)
objc[53244]: EXCEPTIONS: searching through frame [ip=0xf23ac sp=0xbfffd230] for exception 0xb57af10
objc[53244]: EXCEPTIONS: catch(PEGRecognitionException)
objc[53244]: EXCEPTIONS: unwinding through frame [ip=0xf23ac sp=0xbfffd230] for exception 0xb57af10
objc[53244]: EXCEPTIONS: handling exception 0xb57af10 at 0xf2406
objc[53244]: EXCEPTIONS: finishing handler 
objc[53244]: EXCEPTIONS: throwing 0x8ba5c40 (object 0xa17b7b0, a PEGRecognitionException)
objc[53244]: EXCEPTIONS: searching through frame [ip=0xf23ac sp=0xbfffd290] for exception 0x8ba5c20
objc[53244]: EXCEPTIONS: catch(PEGRecognitionException)
objc[53244]: EXCEPTIONS: unwinding through frame [ip=0xf23ac sp=0xbfffd290] for exception 0x8ba5c20
objc[53244]: EXCEPTIONS: handling exception 0x8ba5c20 at 0xf2406
objc[53244]: EXCEPTIONS: finishing handler
objc[53244]: EXCEPTIONS: throwing 0x8ba5f40 (object 0xa17b7b0, a PEGRecognitionException)
objc[53244]: EXCEPTIONS: searching through frame [ip=0xf23ac sp=0xbfffd380] for exception 0x8ba5f20
objc[53244]: EXCEPTIONS: catch(PEGRecognitionException)
objc[53244]: EXCEPTIONS: unwinding through frame [ip=0xf23ac sp=0xbfffd380] for exception 0x8ba5f20
objc[53244]: EXCEPTIONS: handling exception 0x8ba5f20 at 0xf2406
objc[53244]: EXCEPTIONS: finishing handler
objc[53244]: EXCEPTIONS: throwing 0x8e80b40 (object 0xa17b7b0, a PEGRecognitionException)
objc[53244]: EXCEPTIONS: searching through frame [ip=0xf23ac sp=0xbfffd210] for exception 0x8e80b20
objc[53244]: EXCEPTIONS: catch(PEGRecognitionException)
objc[53244]: EXCEPTIONS: unwinding through frame [ip=0xf23ac sp=0xbfffd210] for exception 0x8e80b20
objc[53244]: EXCEPTIONS: handling exception 0x8e80b20 at 0xf2406
objc[53244]: EXCEPTIONS: finishing handler
objc[53244]: EXCEPTIONS: throwing 0x8e4ac80 (object 0xa17b7b0, a PEGRecognitionException)
objc[53244]: EXCEPTIONS: searching through frame [ip=0xf23ac sp=0xbfffd2d0] for exception 0x8e4ac60
objc[53244]: EXCEPTIONS: catch(PEGRecognitionException)
objc[53244]: EXCEPTIONS: unwinding through frame [ip=0xf23ac sp=0xbfffd2d0] for exception 0x8e4ac60
objc[53244]: EXCEPTIONS: handling exception 0x8e4ac60 at 0xf2406
objc[53244]: EXCEPTIONS: finishing handler
objc[53244]: EXCEPTIONS: throwing 0x8ba6200 (object 0xa17b7b0, a PEGRecognitionException)
objc[53244]: EXCEPTIONS: searching through frame [ip=0xf23ac sp=0xbfffd330] for exception 0x8ba61e0
objc[53244]: EXCEPTIONS: catch(PEGRecognitionException)
objc[53244]: EXCEPTIONS: unwinding through frame [ip=0xf23ac sp=0xbfffd330] for exception 0x8ba61e0
objc[53244]: EXCEPTIONS: handling exception 0x8ba61e0 at 0xf2406
objc[53244]: EXCEPTIONS: finishing handler
objc[53244]: EXCEPTIONS: throwing 0x8ba62c0 (object 0xa17b7b0, a PEGRecognitionException)
objc[53244]: EXCEPTIONS: searching through frame [ip=0xf23ac sp=0xbfffd420] for exception 0x8ba62a0
objc[53244]: EXCEPTIONS: catch(PEGRecognitionException)
objc[53244]: EXCEPTIONS: unwinding through frame [ip=0xf23ac sp=0xbfffd420] for exception 0x8ba62a0
objc[53244]: EXCEPTIONS: handling exception 0x8ba62a0 at 0xf2406
objc[53244]: EXCEPTIONS: finishing handler

[parser parse:test error:&err];

不调用回调,也不返回任何错误。

任何指示或帮助将不胜感激!

PS - 我在 ParseKit 中取消了一些 NSLOG 语句的注释,以查看上述异常是什么:

对于一个语法

    NSString *grammar = @"@start = expr; expr = abc col def; abc = 'ABC'; col = ':'; def = 'DEF';";

我有

Expected : @
Line : 1
Near : ; expr = abc 
Found : =

No viable alternative found in rule 'predicate'.
Line : 1
Near : ; expr = abc col def 
Found : col

No viable alternative found in rule 'predicate'.
Line : 1
Near : ; expr = abc col def 
Found : def

No viable alternative found in rule 'predicate'.
Line : 1
Near : ; expr = abc col def 
Found : def

No viable alternative found in rule 'predicate'.
Line : 1
Near : ; expr = abc col def ; abc 
Found : ;

No viable alternative found in rule 'predicate'.
Line : 1
Near : ; expr = abc col def ; abc 
Found : ;

No viable alternative found in rule 'primaryExpr'.
Line : 1
Near : ; expr = abc col def ; abc 
Found : ;

Expected : |
Line : 1
Near : ; expr = abc col def ; abc 
Found : ;

Expected : @
Line : 1
Near : ; expr = abc col def ; abc 
Found : =

No viable alternative found in rule 'predicate'.
Line : 1
Near : ; expr = abc col def ; abc 
Found : col

No viable alternative found in rule 'predicate'.
Line : 1
Near : ; expr = abc col def ; abc 
Found : def

No viable alternative found in rule 'predicate'.
Line : 1
Near : ; expr = abc col def ; abc 
Found : def

No viable alternative found in rule 'predicate'.
Line : 1
Near : ; expr = abc col def ; abc 
Found : ;

No viable alternative found in rule 'predicate'.
Line : 1
Near : ; expr = abc col def ; abc 
Found : ;

No viable alternative found in rule 'primaryExpr'.
Line : 1
Near : ; expr = abc col def ; abc 
Found : ;

Expected : |
Line : 1
Near : ; expr = abc col def ; abc 
Found : ;

Expected : @
Line : 1
Near : ; expr = abc col def ; abc = 'ABC' 
Found : =

No viable alternative found in rule 'predicate'.
Line : 1
Near : ; expr = abc col def ; abc = 'ABC' ; col 
Found : ;

No viable alternative found in rule 'primaryExpr'.
Line : 1
Near : ; expr = abc col def ; abc = 'ABC' ; col 
Found : ;

Expected : |
Line : 1
Near : ; expr = abc col def ; abc = 'ABC' ; col 
Found : ;

Expected : @
Line : 1
Near : ; expr = abc col def ; abc = 'ABC' ; col 
Found : =

No viable alternative found in rule 'predicate'.
Line : 1
Near : ; expr = abc col def ; abc = 'ABC' ; col 
Found : ;

No viable alternative found in rule 'primaryExpr'.
Line : 1
Near : ; expr = abc col def ; abc = 'ABC' ; col 
Found : ;

Expected : |
Line : 1
Near : ; expr = abc col def ; abc = 'ABC' ; col 
Found : ;

Expected : @
Line : 1
Near : ; expr = abc col def ; abc = 'ABC' ; col = ':' 
Found : =

No viable alternative found in rule 'predicate'.
Line : 1
Near : ; expr = abc col def ; abc = 'ABC' ; col = ':' ; def 
Found : ;

No viable alternative found in rule 'primaryExpr'.
Line : 1
Near : ; expr = abc col def ; abc = 'ABC' ; col = ':' ; def 
Found : ;

Expected : |
Line : 1
Near : ; expr = abc col def ; abc = 'ABC' ; col = ':' ; def 
Found : ;

Expected : @
Line : 1
Near : ; expr = abc col def ; abc = 'ABC' ; col = ':' ; def 
Found : =

No viable alternative found in rule 'predicate'.
Line : 1
Near : ; expr = abc col def ; abc = 'ABC' ; col = ':' ; def 
Found : ;

No viable alternative found in rule 'primaryExpr'.
Line : 1
Near : ; expr = abc col def ; abc = 'ABC' ; col = ':' ; def 
Found : ;

Expected : |
Line : 1
Near : ; expr = abc col def ; abc = 'ABC' ; col = ':' ; def 
Found : ;

Expected : @
Line : 1
Near : ; expr = abc col def ; abc = 'ABC' ; col = ':' ; def = 'DEF' 
Found : =

No viable alternative found in rule 'predicate'.
Line : 1
Near : ; expr = abc col def ; abc = 'ABC' ; col = ':' ; def = 'DEF' ; 
Found : ;

No viable alternative found in rule 'primaryExpr'.
Line : 1
Near : ; expr = abc col def ; abc = 'ABC' ; col = ':' ; def = 'DEF' ; 
Found : ;

Expected : |
Line : 1
Near : ; expr = abc col def ; abc = 'ABC' ; col = ':' ; def = 'DEF' ; 
Found : ;

Expected : @
Line : 1
Near : ; expr = abc col def ; abc = 'ABC' ; col = ':' ; def = 'DEF' ; 
Found : =

No viable alternative found in rule 'predicate'.
Line : 1
Near : ; expr = abc col def ; abc = 'ABC' ; col = ':' ; def = 'DEF' ; 
Found : ;

No viable alternative found in rule 'primaryExpr'.
Line : 1
Near : ; expr = abc col def ; abc = 'ABC' ; col = ':' ; def = 'DEF' ; 
Found : ;

Expected : |
Line : 1
Near : ; expr = abc col def ; abc = 'ABC' ; col = ':' ; def = 'DEF' ; 
Found : ;

No viable alternative found in rule 'statement'.
Line : 2147483647
Near : «EOF» 
Found : «EOF»
4

1 回答 1

0

我让它工作......有点!

与文档不同的地方......

我与 libParseKitMobile.a 链接......这很明显,因为它是 iOS 的唯一目标。

我使用 ParserGenApp 首先为我的语法生成 .h 和 .m

我将这些生成的文件包含在我的项目中。我的项目不是用 ARC 构建的,ParseKit 是用 ARC 构建的,所以我在生成的 .m 的编译标志中添加了 -fobjc-arc

然后我的代码中的用法如下:

NSString *test = @"ABC : DEF";

MyParser *parser = [[MyParser alloc] init];
NSError *err = nil;
PKAssembly *result = [parser parseString:test
                               assembler:self
                                   error:&err];

if (!result) {
    if (err) NSLog(@"%@", err);
} else {
    NSLog(@"%@", [result description]);
}

回调需要采用以下形式:

- (void)parser:(PKParser *)p didMatchAbc:(PKAssembly *)a;

(ParseKit 文档提到 -(void)didMatchAbc:(PKAssembly *a); 但这不起作用,因为所需的签名不同)

从 Github 下载并尝试完整的 MiniMath 项目帮助我解决了一些问题 - https://github.com/itod/ParseKitMiniMathExample

MiniMath 示例也可以作为 zip 文件下载,但它似乎没有嵌入的 ParseKit 文件,我遇到了一些问题。

希望这可以帮助其他希望在他们的 iOS 项目中使用 ParseKit 的人!

Todd Ditchendorf,非常感谢您在 ParseKit 上所做的工作!不幸的是,它有点支离破碎——Github 版本、Google Code 版本,并且网站上的文档很稀疏,而且很有趣,同时有 Github 和 Google 代码的链接,增加了混乱。

parser = [[PKParserFactory factory] parserFromGrammar:grammar assembler:self error:&err];

似乎不起作用。但是,首先生成解析器代码然后使用它似乎可以工作。

如果 ParseKit 可以被清理并更新,那就太好了!这是一个非常有用的工具,尤其是因为我没有看到任何其他替代方案被提及。如果您在清理 ParseKit 及其文档方面需要帮助,我很乐意提供帮助!

干杯,斯里达尔

于 2013-09-04T00:56:59.970 回答