I have the following very simple (test) grammar file
@start = expression+;
expression = keyword | otherWord;
otherWord = Word;
keyword = a | the;
a = 'a';
the = 'the';
Then I run the following code:
// Grammar contains the contents of the above grammar file.
PKParser *parser = [[PKParserFactory factory] parserFromGrammar:grammar assembler:self];
NSString *s = @"The parrot";
[parser parse:s];
PKReleaseSubparserTree(parser);
And the following methods:
- (void)didMatchA:(PKAssembly *)a{
[self log:a type:@"didMatchA "];
}
- (void)didMatchThe:(PKAssembly *)a{
[self log:a type:@"didMatchThe "];
}
- (void)didMatchKeyword:(PKAssembly *)a{
[self log:a type:@"didMatchKeyword "];
}
- (void)didMatchExpression:(PKAssembly *)a{
[self log:a type:@"didMatchExpression "];
}
- (void)didMatchOtherWord:(PKAssembly *)a{
[self log:a type:@"didMatchOtherWord "];
}
-(void) log:(PKAssembly *) assembly type:(NSString *) type{
PKToken * token = [assembly top];
NSLog(@"Method: [%@], token: %@, assembly: %@", type, token, assembly);
}
And finally I get these messages in the log:
[1] Method: [didMatchThe ], token: The, assembly: [The]The^parrot
[2] Method: [didMatchKeyword ], token: The, assembly: [The]The^parrot
[3] Method: [didMatchOtherWord ], token: The, assembly: [The]The^parrot
[4] Method: [didMatchExpression ], token: The, assembly: [The]The^parrot
[5] Method: [didMatchExpression ], token: The, assembly: [The]The^parrot
[6] Method: [didMatchOtherWord ], token: parrot, assembly: [The, parrot]The/parrot^
[7] Method: [didMatchExpression ], token: parrot, assembly: [The, parrot]The/parrot^
This sort of makes sense, but I cannot see why %5 occurs. I'd really like to be able to remove the double matching so that keywords such as "The" only trigger didMatchThe and not didMatchKeyword.
Unfortunately the doco on parsekit seems to be non-existant on its grammar syntax and how it decides to trigger methods. Yes, I've trolled the source code too :-)
Has anyone got experience with parsekit and can shed some light on this?