逻辑非运算符不在boost :: spirit :: qi中工作

时间:2013-09-05 06:34:28

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

如果在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

在下面,提供了一些替代方案。

1 个答案:

答案 0 :(得分:1)

为了它的价值:

  • 正如其他人指出的那样operator&operator!零宽度前瞻断言(它们不匹配,但是'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域中的原型表达式,否则您并不总是需要在qi::lit中“包装”文字。


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

&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