Your token_def should expose the expected attribute type (the compile error suggests you are assigning a string literal to an iterator_range):
lex::token_def<std::string> identifier;
Now, match up the type in the assignment
this->self = identifier [ lex::_val = std::string("identifier") ]
Don't forget to update the token type to reflect the set of possible token attribute types:
typedef
lex::lexertl::actor_lexer<lex::lexertl::token<base_iterator_type,
boost::mpl::vector<std::string> > >
lexer_type;
Now it should compile:
#include <boost/spirit/include/lex_lexertl.hpp>
#include <boost/spirit/include/phoenix.hpp>
namespace lex = boost::spirit::lex;
namespace phx = boost::phoenix;
template <typename Lexer>
struct my_tokens : lex::lexer<Lexer>
{
my_tokens()
{
identifier = "[a-zA-Z_][a-zA-Z0-9_]*";
this->self = identifier [ lex::_val = std::string("identifier") ]
;
}
lex::token_def<std::string> identifier;
};
int main()
{
typedef std::string::iterator base_iterator_type;
typedef
lex::lexertl::actor_lexer<lex::lexertl::token<base_iterator_type, boost::mpl::vector<std::string> > >
lexer_type;
my_tokens<lexer_type> myLexer;
std::string str = "id1";
base_iterator_type first = str.begin();
bool r = lex::tokenize(first, str.end(), myLexer);
if (!r) {
std::string rest(first, str.end());
std::cerr << "Lexical analysis failed\n" << "stopped at: \""
<< rest << "\"\n";
}
}