boost::spirit::qi 关键字和标识符 [英] boost::spirit::qi keywords and identifiers

查看:15
本文介绍了boost::spirit::qi 关键字和标识符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过一些与 qi 语法中关键字/标识符使用的细微差别相关的帖子,但我不太明白 boost 示例中演示的方法应该如何工作......

I've seen a few posts related to the nuances of keyword/identifier use in qi grammars, but I can't quite make sense of how the approach demonstrated in the boost examples is supposed to work...

关键字声明:

qi::symbols<char> keywords;

示例关键字集:

keywords.add
        ("foo")
        ("bar")
        ;

标识符规则声明:

qi::rule<std::string::const_iterator, std::string(), ascii::space_type> identifier;

以下是 qi calc 和编译器示例中标识符规则的定义方式:

Here's how the identifier rule is defined in the qi calc and compiler examples:

identifier = !keywords >> qi::raw[ qi::lexeme[ ( qi::alpha | '_' ) >> *( qi::alnum | '_' ) ] ];

也许我读错了 qi 语法,但在我看来,这不会接受任何匹配或关键字开头的文字.拒绝完整的关键字匹配是所需的行为.但是,我想接受food"作为标识符,即使它以关键字foo"开头.这似乎是一个非常标准的用例,但很难找到真正确定这一点的文档.

Perhaps I'm reading the qi syntax wrong, but it seems to me that this would not accept any literal that matches or starts with a keyword. Rejecting a full keyword match is the desired behavior. But, I want to accept "food" as an identifier, even though it begins with the keyword "foo". This seems like a pretty standard use case, but having trouble finding documentation that really nails this down.

谁能提供一个仅拒绝与关键字完全匹配的标识符规则?

Can anyone offer an identifier rule that only rejects exact matches to keywords?

谢谢!

推荐答案

其实这个问题值得一票.它应该更经常被实现,并且可能在 Spirit 教程中解决,因为它经常被忽视(显然 编译器示例没问题)

Actually this question deserves some votes. It should be much more oft realized, and probably be addressed in the Spirit Tutorials, as it's often overlooked (obviously the compiler samples are ok)

也许我读错了 qi 语法,但在我看来这不会接受任何匹配或以关键字开头的文字.

Perhaps I'm reading the qi syntax wrong, but it seems to me that this would not accept any literal that matches or starts with a keyword.

没错.如果您发现我自己的一个答案(很有可能),我倾向于将其作为一种快速而肮脏的方式来修复一开始就没有适当关键字保护的语法.

That's correct. In case you spotted in one of my own answers (quite a good chance) I tend to do this as a quick-and-dirty way to fixup grammars that didn't have proper keyword guards there in the first place.

但是,需要不同的关键字/标识符需要做更多的工作.我可能会找到一个指向正确完成的答案的链接(这并不难,只是很乏味).

But yeah, requiring distinct keywords/identifiers requires some more work. I might find a link to an answer where it's done correctly (it's not hard, it's just tedious).

同时,看看非常相关的

  • Qi distinct Parser Directive from the Spirit Repository

如果您正在构建一个非常强大的通用语言语法,那么您应该考虑使用 Spirit Lexer.再说一次,在我看来,Spirit 的目标是快速开发和使用 Spirit 的表达式模板 eDSL 简洁嵌入的小型一次性语法.我认为,在很多方面,这与何时这很重要.

If you're building a really robust general-purpose language grammar, this is about the point where you should consider using a Spirit Lexer. Then again, in my humble opinion, Spirit aims at rapid development and small, one-off grammars that are succinctly embedded using Spirit's expression template eDSL. In a lot of aspects, that is much the antipode of when this matters, I reckon.

这篇关于boost::spirit::qi 关键字和标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆