Can't get simple ParseKit example working

无人久伴 提交于 2019-12-13 14:35:16

问题


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.


回答1:


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';


来源:https://stackoverflow.com/questions/7086586/cant-get-simple-parsekit-example-working

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