#include #include #include #include #include #include #include #include #include namespace lex = boost::spirit::lex; namespace phx = boost::phoenix; enum tokenids { ID_IDENTIFICATOR = 1, ID_CONSTANT, ID_OPERATION, ID_BRACKET }; template struct mega_tokens : lex::lexer { mega_tokens() : identifier(L"[a-zA-Z_][a-zA-Z0-9_]*", ID_IDENTIFICATOR) , constant (L"[0-9]+(\\.[0-9]+)?", ID_CONSTANT ) , operation (L"[\\+\\-\\*/]", ID_OPERATION ) , bracket (L"[\\(\\)\\[\\]]", ID_BRACKET ) { using lex::_tokenid; using lex::_val; using phx::val; this->self = operation [ std::wcout << val(L'<') << _tokenid << L':' << _val << L'>' ] | identifier [ std::wcout << val(L'<') << _tokenid << L':' << _val << L'>' ] | constant [ std::wcout << val(L'<') << _tokenid << L':' << _val << L'>' ] | bracket [ std::wcout << val(L'<') << _tokenid << L':' << _val << L'>' ] ; } lex::token_def operation; lex::token_def identifier; lex::token_def constant; lex::token_def bracket; }; int main() { namespace lex = boost::spirit::lex; typedef std::wstring::iterator base_iterator; typedef lex::lexertl::token < base_iterator, boost::mpl::vector, boost::mpl::true_ > token_type; typedef lex::lexertl::actor_lexer lexer_type; typedef mega_tokens::iterator_type iterator_type; mega_tokens mega_lexer; std::wstring exampleStr = L"alfa+x1*(2.836-x2[i])"; base_iterator first = exampleStr.begin(); bool r = lex::tokenize(first, exampleStr.end(), mega_lexer); if (r) { std::wcout << L"Success" << std::endl; } else { std::wstring rest(first, exampleStr.end()); std::wcerr << L"Lexical analysis failed\n" << L"stopped at: \"" << rest << L"\"\n"; } return EXIT_SUCCESS; }