C++ 逻辑not运算符在boost::spirit::qi中不工作

C++ 逻辑not运算符在boost::spirit::qi中不工作,c++,boost,grammar,boost-spirit,boost-spirit-qi,C++,Boost,Grammar,Boost Spirit,Boost Spirit Qi,如果在qi::语法中,我使用这个基本规则 expression = (boost::spirit::ascii::string("aaa")); 它将解析“aaa”而不解析其他内容 当我使用这个(注意!)时,它根本不解析任何东西,而我希望它在除“aaa”之外的所有方面都能成功 我会错过一些吗?我使用的是boost 1.54.0 编辑: 对不起,这有点牵强,我修改了计算器的例子,我的第一次尝试 #include <boost/config/warning_disable.hpp> #

如果在qi::语法中,我使用这个基本规则

expression = (boost::spirit::ascii::string("aaa"));
它将解析“aaa”而不解析其他内容

当我使用这个(注意!)时,它根本不解析任何东西,而我希望它在除“aaa”之外的所有方面都能成功

我会错过一些吗?我使用的是boost 1.54.0

编辑:

对不起,这有点牵强,我修改了计算器的例子,我的第一次尝试

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>

#include <iostream>
#include <string>
#include <boost/spirit/include/qi_lit.hpp>
#include <boost/spirit/include/qi_not_predicate.hpp>
/*
 * \
    __grammar_calculator.cpp

HEADERS += \
    __grammar_calculator.h
 */
namespace client
{
    namespace qi = boost::spirit::qi;
    namespace ascii = boost::spirit::ascii;

    ///////////////////////////////////////////////////////////////////////////
    //  Our calculator grammar
    ///////////////////////////////////////////////////////////////////////////
    template <typename Iterator>
    struct calculator : qi::grammar<Iterator, int(), ascii::space_type>
    {
        calculator() : calculator::base_type(expression)
        {
            using qi::_val;
            using qi::_1;
            using qi::uint_;
            using boost::spirit::qi::lit;
            using boost::spirit::ascii::string;

            expression = !(boost::spirit::ascii::string("aaa"));
        }

        qi::rule<Iterator, int(), ascii::space_type> expression, term, factor;
    };
}

///////////////////////////////////////////////////////////////////////////////
//  Main program
///////////////////////////////////////////////////////////////////////////////
int
main()
{
    std::cout << "/////////////////////////////////////////////////////////\n\n";
    std::cout << "Expression parser...\n\n";
    std::cout << "/////////////////////////////////////////////////////////\n\n";
    std::cout << "Type an expression...or [q or Q] to quit\n\n";

    using boost::spirit::ascii::space;
    typedef std::string::const_iterator iterator_type;
    typedef client::calculator<iterator_type> calculator;

    calculator calc; // Our grammar

    std::string str;
    int result;
    while (std::getline(std::cin, str))
    {
        if (str.empty() || str[0] == 'q' || str[0] == 'Q')
            break;

        std::string::const_iterator iter = str.begin();
        std::string::const_iterator end = str.end();
        bool r = phrase_parse(iter, end, calc, space, result);

        if (r && iter == end)
        {
            std::cout << "-------------------------\n";
            std::cout << "Parsing succeeded\n";
            //std::cout << "result = " << result << std::endl;
            std::cout << "-------------------------\n";
        }
        else
        {
            std::string rest(iter, end);
            std::cout << "-------------------------\n";
            std::cout << "Parsing failed\n";
            //std::cout << "stopped at: \": " << rest << "\"\n";
            std::cout << "-------------------------\n";
        }
    }

    std::cout << "Bye... :-) \n\n";
    return 0;
}
正如前面所指出的,操作员并没有消耗任何东西,所以iter=结束

在下文中,sehe提供了一些替代方案。

以获取其价值:

  • 正如其他人指出的那样,
    操作符&
    操作符
    是零宽度的前瞻断言(它们不匹配,但“peek”,匹配成功或失败)
你会想知道的

  • 求反字符集:

    qi::char_("a-z")    // matches lowercase letters
    
    ~qi::char_(“a-z”)//匹配任何小写字母

  • “解析器减法”-考虑异常:

    qi::char_ - qi::char_("a-z")  // equivalent to ~qi::char_("a-z") 
    qi::char_("a-z") - "keyword"  // any lowercase letters, but not if it spells "keyword" 
    

编辑以便向前扫描到下一个“{”,您可以执行以下操作

qi::omit [ qi::char_ - "{%" ] >> "{%"
请注意,您并不总是需要在
qi::lit
中“包装”文本,除非表达式在qi域中还没有涉及到原型表达式


编辑2如何使用
&
的示例:

&qi::int_ >> +qi::char_   // parse a string, **iff** it starts with an integer

为了它的价值:

  • 正如其他人指出的那样,
    操作符&
    操作符!
    是零宽度的前瞻断言(它们不匹配,但“偷看”,匹配成功或失败)
你会想知道的

  • 求反字符集:

    qi::char_("a-z")    // matches lowercase letters
    
    ~qi::char_(“a-z”)//匹配任何小写字母

  • “解析器减法”-考虑异常:

    qi::char_ - qi::char_("a-z")  // equivalent to ~qi::char_("a-z") 
    qi::char_("a-z") - "keyword"  // any lowercase letters, but not if it spells "keyword" 
    

编辑以便向前扫描到下一个“{”,您可以执行以下操作

qi::omit [ qi::char_ - "{%" ] >> "{%"
请注意,您并不总是需要在
qi::lit
中“包装”文本,除非表达式在qi域中还没有涉及到原型表达式


编辑2如何使用
&
的示例:

&qi::int_ >> +qi::char_   // parse a string, **iff** it starts with an integer


操作符!
是不消耗任何东西的,实际上除了
aaa
之外,任何东西都会成功。你是怎么决定的?请显示a。我使用了解析函数。它返回true或false。我测试了这两个表达式。第二个表达式总是返回false。在你的问题中显示该代码。@user2346536来吧,不要放弃很难回答。如果这是你的问题,只需完全放下原来的问题,把你的问题正确地写在…问题框中!这在评论中难以辨认,我不会尝试阅读。好吧,很公平。不过,“如果你能准确地解释你想要完成的任务,我相信你会找到好的替代方案。”不是在追求你的人生目标:)它是在问你是否确实认为你需要
操作符!
操作符!
就是不消耗任何东西,事实上,除了
aaa
之外,你什么都能成功。你是如何决定的?请给出一个。我使用了解析函数。它返回的是真还是假。我测试了这两个表达式。第二个总是返回false。在你的问题中显示代码。@user2346536来吧,别傻了。如果这是你的问题,只需完全删除原始问题,并将你的问题正确地写在…问题框中!这在评论中难以辨认,我不会尝试阅读它。好的,很公平。不过,“如果你准确地解释你想要完成什么,我相信你会得到好的选择”不是在追求你的人生目标:)而是在问你是否确实认为你需要
操作符!
谢谢:expression=*(qi::char \-“{%”);是我一直在寻找的。即使如此,我仍然不确定!应该做什么!@user2346536查看我的“编辑2”(并且始终有包含的),我理解!!!!!如果(r&&iter==end)失败,但如果(r)成功,那么操作符!执行其工作,但零匹配长度==>iter!=end,因为“!“谢谢:expression=*(qi::char_-“{%”);是我一直在寻找的。即使如此,我仍然不确定!应该做什么!@user2346536查看我的'Edit 2'(并且总是包含)我明白了!!!!!如果(r&&iter==end)失败,但是如果(r)成功,因此运算符!执行其工作,但零匹配长度==>iter!=结束,因为“!”没有消耗任何内容