无法让简单的 ParseKit 示例正常工作

发布于 2024-11-29 16:29:36 字数 543 浏览 1 评论 0原文

我刚刚发现 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白龙吟 2024-12-06 16:29:36

ParseKit 的开发者在这里。上面的例子将不起作用。默认情况下,Tokenizer 会将其标记

FOO:BAR

为三个标记:

Word (FOO)
Symbol (:)
Word (BAR)

问题是您的语法需要像“FOO:”这样的单词,但默认情况下冒号是符号字符,而不是单词字符。如果您希望冒号 (:) 被接受为有效的内部“Word”字符,则必须自定义 Tokenizer 以使其接受这一点。我有点怀疑你真的想要那个。如果您这样做,请阅读此处的文档以了解如何自定义分词器:http://parsekit.com/tokenization.html

我认为更好的“玩具”语法可能是这样的:

@start = pair;
pair = foo colon bar;
foo = 'FOO';
colon = ':';
bar = 'BAR';

您在如何在语法中进行声明方面有很大的灵活性。等效的语法是:

@start = foo ':' bar;
foo = 'FOO';
bar = 'BAR';

Developer of ParseKit here. The example above will not work. By default the Tokenizer will tokenize this:

FOO:BAR

as three tokens:

Word (FOO)
Symbol (:)
Word (BAR)

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:

@start = pair;
pair = foo colon bar;
foo = 'FOO';
colon = ':';
bar = 'BAR';

You have a lot of flexibility in how you do your declarations in your grammar. An equivalent grammar would be:

@start = foo ':' bar;
foo = 'FOO';
bar = 'BAR';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文