简体   繁体   English

这个 ParseKit BNF 有什么问题?

[英]What is wrong with this ParseKit BNF?

I'm using ParseKit for objective-C which takes a BNF-like syntax for specifying grammers:我正在为 objective-C 使用 ParseKit,它采用类似 BNF 的语法来指定语法:

@start = command+;
command = new;
new = 'new' object ';';
object = 'house' | other;

Inclusion of the last line causes an error.包含最后一行会导致错误。 Basically I want to say an object can be a house or something else.基本上我想说一个 object 可以是房子或其他东西。 The non-terminal element "other" is supposed to catch whatever word was there that wasn't house.非终结元素“other”应该能捕捉到任何不是房子的词。

Am I going about the "anything-here" idea the wrong way?我是否以错误的方式谈论“这里的任何东西”的想法?

Thanks!谢谢!

Developer of ParseKit here. ParseKit 的开发者在这里。 Carmine's answer above is excellent and you should take his advice. Carmine 的上述回答非常好,您应该听取他的建议。 One small additional note:一个小的附加说明:

If you want to make it easy for your Parser delegate to notice when 'house' was matched vs. any other random word, I would change the last line of your grammar above to:如果您想让您的 Parser 代表更容易注意到“house”与任何其他随机单词匹配的时间,我会将上面语法的最后一行更改为:

object = house | other;
house = 'house';
other = Word;

Then you should implement the two following callback methods in your Parser delegate:然后,您应该在 Parser 委托中实现以下两个回调方法:

- (void)parser:(PKParser *)p didMatchHouse:(PKAssembly *)a;
- (void)parser:(PKParser *)p didMatchOther:(PKAssembly *)a;

If you want to allow other to match any token at all (not just words, but also numbers, symbols, quoted strings, etc), you can use the builtin Any type.如果你想允许other匹配任何标记(不仅仅是单词,还有数字、符号、带引号的字符串等),你可以使用内置的Any类型。 In that case, you would change the last line of my example above to:在这种情况下,您可以将上面示例的最后一行更改为:

other = Any;

As suggested in the comments, you should either replace other with Word or add a new rule:正如评论中所建议的,您应该将other替换为Word或添加新规则:

other = Word;

Since 'house' is a Word , you can also directly replace the object rule with:由于'house'是一个Word ,您也可以直接将object规则替换为:

object = Word;

A Word in ParseKit is a contiguous sequence of characters ( [a-zA-Z] ), numbers ( [0-9] ), and the symbols - , _ , and ' , that starts with a character. ParseKit 中的Word是字符 ( [a-zA-Z] )、数字 ( [0-9] ) 和符号-_'的连续序列,它们以字符开头。 You can find more information about ParseKit tokens in the documentation .您可以在文档中找到有关 ParseKit 令牌的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM