这个 ParseKit BNF 有什么问题?

发布于 2024-11-25 13:34:07 字数 286 浏览 4 评论 0原文

我正在使用用于 Objective-C 的 ParseKit,它采用类似 BNF 的语法来指定语法:

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

包含最后一行会导致错误。 基本上我想说一个物体可以是房子或其他东西。非终结符“other”应该捕获任何不属于 house 的单词。

我是否以错误的方式理解“这里任何东西”的想法?

谢谢!

I'm using ParseKit for objective-C which takes a BNF-like syntax for specifying grammers:

@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. The non-terminal element "other" is supposed to catch whatever word was there that wasn't house.

Am I going about the "anything-here" idea the wrong way?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘿嘿嘿 2024-12-02 13:34:07

ParseKit 的开发者在这里。卡迈恩上面的回答非常好,你应该听取他的建议。一个小的附加说明:

如果您想让您的解析器代表轻松注意到“house”何时与任何其他随机单词匹配,我会将上面语法的最后一行更改为:

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

那么您应该实现以下两个解析器委托中的回调方法:

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

如果您希望允许 other 匹配任何标记(不仅仅是单词,还包括数字、符号、带引号的字符串等),您可以使用内置的 Any 类型。在这种情况下,您可以将上面示例的最后一行更改为:

other = Any;

Developer of ParseKit here. Carmine's answer above is excellent and you should take his advice. 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:

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

Then you should implement the two following callback methods in your Parser delegate:

- (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. In that case, you would change the last line of my example above to:

other = Any;
命硬 2024-12-02 13:34:07

正如评论中所建议的,您应该将 other 替换为 Word 或添加新规则:

other = Word;

由于 'house'Word,你也可以直接将object规则替换为:

object = Word;

ParseKit中的Word是一个连续的字符序列([a-zA-Z]), 数字([0-9]),以及以 a 开头的符号 -_'特点。您可以在文档中找到有关 ParseKit 令牌的更多信息。

As suggested in the comments, you should either replace other with Word or add a new rule:

other = Word;

Since 'house' is a Word, you can also directly replace the object rule with:

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. You can find more information about ParseKit tokens in the documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文