我有一段boost::spirit::qi
可以匹配"M7. x . y . z"
or的代码"M7.x.y.z"
,但我想要一个解析器,它在前一个输入上失败。
我想我需要插入qi::lexeme[]
或qi::no_skip[]
在那里,但我没有运气让它正确编译。
编辑变量的简化规则
代码
#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";
}