【问题标题】:the logical not operator not working in boost::spirit::qi逻辑非运算符在 boost::spirit::qi 中不起作用
【发布时间】:2013-09-05 06:34:28
【问题描述】:

如果在 qi::grammar 中我使用这个基本规则

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

它只会解析“aaa”而不是别的

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

expression = !(boost::spirit::ascii::string("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;
}

编辑 2:

同样的更干净一点:

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

#include <iostream>
#include <string>
#include <boost/spirit/include/qi_not_predicate.hpp>

namespace client
{
    namespace qi = boost::spirit::qi;
    namespace ascii = boost::spirit::ascii;

    template <typename Iterator>
    struct test : qi::grammar<Iterator>
    {
        test() : test::base_type(expression)
        {
            using boost::spirit::ascii::string;

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

        qi::rule<Iterator> expression;
    };
}

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::test<iterator_type> test;

    test tester; // Our grammar

    std::string str;
    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, tester, space);

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

    std::cout << "Bye... :-) \n\n";
    return 0;
}

回答:

问题来自于测试:

if (r && iter == end)

正如所指出的,运营商没有消耗任何东西,所以 iter!=end

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

【问题讨论】:

  • operator!supposed 不消耗任何东西,并且确实在除aaa 之外的所有事情上都取得成功。你是如何确定的?请出示SSCCE
  • 我使用了解析函数。它返回真或假。我测试了两种表达方式。第二个总是返回 false。
  • 在您的问题中显示该代码。
  • @user2346536 来吧,别开玩笑了。如果这是您的问题,只需完全放弃原始问题,然后在...问题框中正确写下您的问题!这在评论中难以辨认,我不会尝试阅读它。
  • 好的,很公平。虽然,“如果你确切地解释你想要完成什么,我相信你会得到很好的选择”并不是为了你的人生目标:) 它问的是你是否认为你需要operator!

标签: c++ boost grammar boost-spirit boost-spirit-qi


【解决方案1】:

物有所值:

  • 正如其他人指出的那样,operator&amp;operator!零宽度前瞻断言(它们不匹配,而是“窥视”,匹配成功或失败)。

你会想知道的

  • 否定字符集:

    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如何使用!&amp;的示例:

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

!keyword_list >> identifier // parse any identifier that's not a known keyword

【讨论】:

  • 感谢:表达式 = *(qi::char_ - "{%");是我一直在寻找的。即使这样我仍然不确定是什么!应该做的!
  • @user2346536 查看我的“Edit 2”(总是有包含examplesdocumentation
  • 我明白了!!!!!! if (r && iter == end ) 失败但 if(r) 成功,所以运算符!完成它的工作,但零匹配长度 ==> iter != end 因为“!”没有消耗任何东西
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多