【发布时间】:2011-06-04 14:05:21
【问题描述】:
我有以下非常简单的(测试)语法文件
@start = expression+;
expression = keyword | otherWord;
otherWord = Word;
keyword = a | the;
a = 'a';
the = 'the';
然后我运行以下代码:
// 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);
还有以下方法:
- (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);
}
最后我在日志中得到了这些消息:
[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^
这种说法是有道理的,但我不明白为什么会出现 %5。我真的很希望能够删除双重匹配,以便诸如“The”之类的关键字仅触发didMatchThe 而不是didMatchKeyword。
不幸的是,parsekit 上的 doco 在其语法语法以及它决定触发方法的方式上似乎不存在。是的,我也浏览了源代码 :-)
有没有人有使用 parsekit 的经验并且可以对此有所了解?
【问题讨论】:
标签: objective-c parsekit