3

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.

JPC
  • 8,096
  • 22
  • 77
  • 110
  • NSString *grammar = @"@start = foo;foo = 'FOO:' bar;bar = 'BAR';"; didn't work either – JPC Aug 17 '11 at 00:07

1 Answers1

2

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';
Todd Ditchendorf
  • 11,217
  • 14
  • 69
  • 123