Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get simple ParseKit example working

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.

like image 531
JPC Avatar asked Sep 28 '26 22:09

JPC


1 Answers

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';
like image 166
Todd Ditchendorf Avatar answered Sep 30 '26 13:09

Todd Ditchendorf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!