下面的代码编译得很好
clang++ -std=c++11 test.cpp -o test
但是运行时会抛出异常
在抛出 'boost::lexer::runtime_error' what() 的实例后调用终止:尚不支持 Lookahead ('/')。
问题是输入和/或正则表达式(第 12 和 39 行)中的斜杠(/),但我找不到如何正确转义它的解决方案。有什么提示吗?
#include <string>
#include <cstring>
#include <boost/spirit/include/lex.hpp>
#include <boost/spirit/include/lex_lexertl.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
namespace lex = boost::spirit::lex;
namespace qi = boost::spirit::qi;
namespace phoenix = boost::phoenix;
std::string regex("FOO/BAR");
template <typename Type>
struct Lexer : boost::spirit::lex::lexer<Type> {
Lexer() : foobar_(regex) {
this->self.add(foobar_);
}
boost::spirit::lex::token_def<std::string> foobar_;
};
template <typename Iterator, typename Def>
struct Grammar
: qi::grammar <Iterator, qi::in_state_skipper<Def> > {
template <typename Lexer> Grammar(const Lexer & _lexer);
typedef qi::in_state_skipper<Def> Skipper;
qi::rule<Iterator, Skipper> rule_;
};
template <typename Iterator, typename Def>
template <typename Lexer>
Grammar<Iterator, Def>::Grammar(const Lexer & _lexer)
: Grammar::base_type(rule_) {
rule_ = _lexer.foobar_;
}
int main() {
// INPUT
char const * first("FOO/BAR");
char const * last(first + strlen(first));
// LEXER
typedef lex::lexertl::token<const char *> Token;
typedef lex::lexertl::lexer<Token> Type;
Lexer<Type> l;
// GRAMMAR
typedef Lexer<Type>::iterator_type Iterator;
typedef Lexer<Type>::lexer_def Def;
Grammar<Iterator, Def> g(l);
// PARSE
bool ok = lex::tokenize_and_phrase_parse (
first
, last
, l
, g
, qi::in_state("WS")[l.self]
);
// CHECK
if (!ok || first != last) {
std::cout << "Failed parsing input file" << std::endl;
return 1;
}
return 0;
}