Advertisement
Guest User

Untitled

a guest
May 24th, 2010
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.72 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <locale>
  4. #include <string>
  5.  
  6. #include <boost/spirit/include/lex_lexertl.hpp>
  7. #include <boost/spirit/include/phoenix_object.hpp >
  8. #include <boost/spirit/include/phoenix_operator.hpp>
  9. #include <boost/spirit/include/phoenix_statement.hpp>
  10. #include <boost/spirit/include/phoenix_container.hpp>
  11.  
  12.  
  13. namespace lex = boost::spirit::lex;
  14. namespace phx = boost::phoenix;
  15.  
  16.  
  17. struct output_operation_impl
  18. {
  19.     template <typename TokenId, typename Val>
  20.     struct result { typedef void type; };
  21.  
  22.     template <typename TokenId, typename Val>
  23.     void operator()(TokenId const& tokenid, Val const& val) const
  24.     {
  25.         std::wcout << L'<' << tokenid << L':' << val << L'>';
  26.     }
  27. };
  28. phx::function<output_operation_impl> const output_operation = output_operation_impl();
  29.  
  30.  
  31. enum tokenids
  32. {
  33.     ID_IDENTIFICATOR = 1,
  34.     ID_CONSTANT,
  35.     ID_OPERATION,
  36.     ID_BRACKET,
  37.     ID_WHITESPACE
  38. };
  39.  
  40. template <typename Lexer>
  41. struct mega_tokens
  42.     : lex::lexer<Lexer>
  43. {
  44.  
  45.     mega_tokens()
  46.         : identifier(L"[a-zA-Z_][a-zA-Z0-9_]*", ID_IDENTIFICATOR)
  47.         , constant  (L"[0-9]+(\\.[0-9]+)?",     ID_CONSTANT     )
  48.         , operation (L"[\\+\\-\\*/]",           ID_OPERATION    )
  49.         , bracket   (L"[\\(\\)\\[\\]]",         ID_BRACKET      )
  50.     {
  51.         using lex::_tokenid;
  52.         using lex::_val;
  53.         using phx::val;
  54.  
  55.         this->self
  56.             = operation  [ std::wcout
  57.                             << val(L'<') << _tokenid
  58.                             << L':' << _val
  59.                             << L'>'
  60.                          ]
  61.             | identifier [ output_operation(_tokenid, _val) ]
  62.             | constant   [ std::wcout
  63.                             << val(L'<') << _tokenid
  64.                             << L':' << _val
  65.                             << L'>'
  66.                          ]
  67.             | bracket    [ std::wcout
  68.                             << val(L'<') << _tokenid
  69.                             << L':' << lex::_val
  70.                             << L'>'
  71.                          ]
  72.         ;
  73.  
  74.     }
  75.  
  76.     lex::token_def<wchar_t,      wchar_t> operation;
  77.     lex::token_def<std::wstring, wchar_t> identifier;
  78.     lex::token_def<double,       wchar_t> constant;
  79.     lex::token_def<wchar_t,      wchar_t> bracket;
  80. };
  81.  
  82.  
  83. int main()
  84. {
  85.     setlocale(LC_ALL, "Russian");
  86.  
  87.     namespace lex = boost::spirit::lex;
  88.  
  89.     typedef std::wstring::iterator base_iterator;
  90.     typedef lex::lexertl::token <
  91.         base_iterator,
  92.         boost::mpl::vector<wchar_t, std::wstring, double, wchar_t>,
  93.         boost::mpl::true_
  94.     > token_type;
  95.     typedef lex::lexertl::actor_lexer<token_type> lexer_type;
  96.     typedef mega_tokens<lexer_type>::iterator_type iterator_type;
  97.  
  98.     mega_tokens<lexer_type> mega_lexer;
  99.  
  100.     std::wstring str = L"alfa+x1*(2.836-x2[i])";
  101.     base_iterator first = str.begin();
  102.  
  103.     bool r = lex::tokenize(first, str.end(), mega_lexer);
  104.  
  105.     if (r) {
  106.         std::wcout << L"Success" << std::endl;
  107.     }
  108.     else {
  109.         std::wstring rest(first, str.end());
  110.         std::wcerr << L"Lexical analysis failed\n" << L"stopped at: \""
  111.             << rest << L"\"\n";
  112.     }
  113.  
  114.     return EXIT_SUCCESS;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement