【问题标题】:How to ensure that this qi parser disallows spaces between the dot operators?如何确保这个 qi 解析器不允许点运算符之间有空格?
【发布时间】:2012-11-29 18:15:45
【问题描述】:

我有一个可以匹配"M7. x . y . z""M7.x.y.z"boost::spirit::qi 代码的sn-p,但我想要一个解析器在前一个输入上失败。

我想我需要在其中插入 qi::lexeme[]qi::no_skip[],但我没有运气让它正确编译。

EDIT变量的简化规则

代码

#define BOOST_SPIRIT_DEBUG

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

#include <string>
namespace client
{
    namespace qi = boost::spirit::qi;
    namespace ascii = boost::spirit::ascii;

    template <typename Iterator>
    struct my_parser : qi::grammar<Iterator, std::vector<std::string>(), 
        ascii::space_type>
    {
      my_parser() : 
            my_parser::base_type( variable )
        {
            using qi::int_;
            using qi::lit;
            using qi::double_;
            using qi::lexeme;
            using ascii::char_;

            identifier %= 
                char_( "[a-z_]" )
                >> *char_( "*[a-zA-Z0-9_]" )
            ;

            variable %= simple_var % '.'   // <----- need fix here
            ;

            simple_var %= qi::string("M7") | identifier;

            BOOST_SPIRIT_DEBUG_NODE( variable );
            BOOST_SPIRIT_DEBUG_NODE( identifier );
        }

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

        qi::rule<Iterator, std::vector<std::string>(), ascii::space_type> 
            variable;
    };
}

int main( int argc, char* argv[] )
{
    using boost::spirit::ascii::space;

    typedef std::string::const_iterator iterator_type;
    typedef client::my_parser<iterator_type> my_parser;

    my_parser       g; 

    std::vector< std::string >    result;
    std::string input( "M7. x . y . z" );  // want this to FAIL!!!

    std::string::const_iterator iter = input.begin();
    std::string::const_iterator end  = input.end();

    if (phrase_parse( iter, end, g, space, result ) && iter == end)
        std::cout << "Parsing succeeded\n";
    else
        std::cout << "Parsing failed\n";
}

【问题讨论】:

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


【解决方案1】:

qi::lexeme[] 只接受无船长规则。

您可以将 identifier 和 simple_var 声明为:

qi::rule<Iterator, std::string()> identifier,simple_var;

现在你可以在variable中使用lexeme

variable %= lexeme[simple_var % '.'];

【讨论】:

    猜你喜欢
    • 2013-09-17
    • 1970-01-01
    • 1970-01-01
    • 2011-12-03
    • 2017-03-05
    • 2020-01-14
    • 1970-01-01
    • 2015-11-12
    • 1970-01-01
    相关资源
    最近更新 更多