提振精神:: ::气关键字和标识符 [英] boost::spirit::qi keywords and identifiers

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

问题描述

我看到有关齐语法关键字/标识符采用细微差别的几个职位,但我不能完全使如何助推例子证明这种方法应该工作意义...

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")
        ;

标识符规则声明:

Identifier rule declaration:

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

下面是标识符规则是如何在气钙和编译器的例子定义的:

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 | '_' ) ] ];

也许我读补气语法错了,但在我看来,这将不接受任何文字匹配或开始以的关键字。拒绝一个完整的关键字匹配所需的行为。但是,我要接受食作为标识,即使它以关键字foo的开始。这似乎是一个pretty规范使用的情况下,但无法找到文件,真正的指甲下来。

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?

谢谢!

推荐答案

其实这个问题值得一些选票。应该更经常被实现,而且很可能在精神教程来解决,因为它常常被忽视(显然编译器样品 都OK了)

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)

也许我读补气语法错了,但在我看来,这将不接受任何文字匹配或关键字开始。

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).

同时,看看在最相关的


  • <一个href=\"http://www.boost.org/doc/libs/1_57_0/libs/spirit/repository/doc/html/spirit_repository/qi_components/directives/distinct.html\"相对=nofollow>齐不同的解析器指令 从圣灵库

  • Qi distinct Parser Directive from the Spirit Repository

如果您正在构建一个真正强大的通用语言的语法,这是关于你应该考虑使用精神词法分析器的地步。再说,在我的愚见,精神的目的是快速开发和正在使用圣灵的前pression模板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.

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

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