开发者

Can't get simple ParseKit example working

I just discovered ParseKit but can't seem to get it working on a simple example.

NSSt开发者_高级运维ring *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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜