Ticket #4253: main.cpp

File main.cpp, 2.7 KB (added by sergey.olendarenko@…, 12 years ago)

Example of lex::_val bug

Line 
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
13namespace lex = boost::spirit::lex;
14namespace phx = boost::phoenix;
15
16
17enum tokenids
18{
19 ID_IDENTIFICATOR = 1,
20 ID_CONSTANT,
21 ID_OPERATION,
22 ID_BRACKET
23};
24
25template <typename Lexer>
26struct mega_tokens
27 : lex::lexer<Lexer>
28{
29
30 mega_tokens()
31 : identifier(L"[a-zA-Z_][a-zA-Z0-9_]*", ID_IDENTIFICATOR)
32 , constant (L"[0-9]+(\\.[0-9]+)?", ID_CONSTANT )
33 , operation (L"[\\+\\-\\*/]", ID_OPERATION )
34 , bracket (L"[\\(\\)\\[\\]]", ID_BRACKET )
35 {
36 using lex::_tokenid;
37 using lex::_val;
38 using phx::val;
39
40 this->self
41 = operation [ std::wcout
42 << val(L'<') << _tokenid
43 << L':' << _val
44 << L'>'
45 ]
46 | identifier [ std::wcout
47 << val(L'<') << _tokenid
48 << L':' << _val
49 << L'>'
50 ]
51 | constant [ std::wcout
52 << val(L'<') << _tokenid
53 << L':' << _val
54 << L'>'
55 ]
56 | bracket [ std::wcout
57 << val(L'<') << _tokenid
58 << L':' << _val
59 << L'>'
60 ]
61 ;
62
63 }
64
65 lex::token_def<wchar_t, wchar_t> operation;
66 lex::token_def<std::wstring, wchar_t> identifier;
67 lex::token_def<double, wchar_t> constant;
68 lex::token_def<wchar_t, wchar_t> bracket;
69};
70
71
72int main()
73{
74 namespace lex = boost::spirit::lex;
75
76 typedef std::wstring::iterator base_iterator;
77 typedef lex::lexertl::token <
78 base_iterator,
79 boost::mpl::vector<wchar_t, std::wstring, double, wchar_t>,
80 boost::mpl::true_
81 > token_type;
82 typedef lex::lexertl::actor_lexer<token_type> lexer_type;
83 typedef mega_tokens<lexer_type>::iterator_type iterator_type;
84
85 mega_tokens<lexer_type> mega_lexer;
86
87 std::wstring exampleStr = L"alfa+x1*(2.836-x2[i])";
88 base_iterator first = exampleStr.begin();
89
90 bool r = lex::tokenize(first, exampleStr.end(), mega_lexer);
91
92 if (r) {
93 std::wcout << L"Success" << std::endl;
94 }
95 else {
96 std::wstring rest(first, exampleStr.end());
97 std::wcerr << L"Lexical analysis failed\n" << L"stopped at: \""
98 << rest << L"\"\n";
99 }
100
101 return EXIT_SUCCESS;
102}