无法让简单的 ParseKit 示例正常工作
我刚刚发现 ParseKit 但似乎无法让它在一个简单的例子中工作。
NSString *test = @"FOO:BAR";
NSString *grammar = ...//get grammar txt file and read it into a string
PKParser *parser = nil;
parser = [[PKParserFactory factory] parserFromGrammar:grammar assembler:self];
[parser parse:test];
}
- (void)didMatchFoo:(PKAssembly *)a
{
NSLog(@"FOO");
}
- (void)didMatchBar:(PKAssembly *)a
{
NSLog(@"BAR");
}
我的语法文件如下所示:
@start = foo;
foo = 'FOO:' bar;
bar = 'BAR';
但这些方法不会触发。
I just discovered ParseKit but can't seem to get it working on a simple example.
NSString *test = @"FOO:BAR";
NSString *grammar = ...//get grammar txt file and read it into a string
PKParser *parser = nil;
parser = [[PKParserFactory factory] parserFromGrammar:grammar assembler:self];
[parser parse:test];
}
- (void)didMatchFoo:(PKAssembly *)a
{
NSLog(@"FOO");
}
- (void)didMatchBar:(PKAssembly *)a
{
NSLog(@"BAR");
}
My grammar file looks like this:
@start = foo;
foo = 'FOO:' bar;
bar = 'BAR';
But the methods don't fire.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ParseKit 的开发者在这里。上面的例子将不起作用。默认情况下,Tokenizer 会将其标记
为三个标记:
问题是您的语法需要像“FOO:”这样的单词,但默认情况下冒号是符号字符,而不是单词字符。如果您希望冒号 (:) 被接受为有效的内部“Word”字符,则必须自定义 Tokenizer 以使其接受这一点。我有点怀疑你真的想要那个。如果您这样做,请阅读此处的文档以了解如何自定义分词器:http://parsekit.com/tokenization.html
我认为更好的“玩具”语法可能是这样的:
您在如何在语法中进行声明方面有很大的灵活性。等效的语法是:
Developer of ParseKit here. The example above will not work. By default the Tokenizer will tokenize this:
as three tokens:
The problem is your grammar is expecting a word like 'FOO:', but colons are Symbol chars by default, not Word chars. If you want colons (:) to be accepted as valid internal "Word" chars, you'll have to customize the Tokenizer to make it accept that. I kinda doubt you really want that tho. If you do, read the docs here to learn how to customize the Tokenizer: http://parsekit.com/tokenization.html
I think a better 'toy' grammar to start with might be something like:
You have a lot of flexibility in how you do your declarations in your grammar. An equivalent grammar would be: