-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParseExprBoost.cpp
More file actions
142 lines (115 loc) · 4.2 KB
/
Copy pathParseExprBoost.cpp
File metadata and controls
142 lines (115 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// http://www.boost.org/doc/libs/1_48_0/libs/spirit/example/qi/compiler_tutorial/calc3.cpp
// http://www.boost.org/doc/libs/1_40_0/libs/spirit/example/qi/complex_number.cpp
// das in [] sind die actions: http://boost-spirit.com/home/2010/03/03/the-anatomy-of-semantic-actions-in-qi/
// http://www.wilkening-online.de/programmieren/c++-user-treffen-aachen/2014_11_13/BoostSpirit_GeorgHellack.pdf
// Spirit v2.5 allows you to suppress automatic generation
// of predefined terminals to speed up complation. With
// BOOST_SPIRIT_NO_PREDEFINED_TERMINALS defined, you are
// responsible in creating instances of the terminals that
// you need (e.g. see qi::uint_type uint_ below).
#define BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <iostream>
#include <string>
#include "ParseExpr.h"
/*
int func(int attribute)
{
std::cout << "Matched integer: " << attribute << "\n";
return attribute;
}
*/
namespace client
{
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
///////////////////////////////////////////////////////////////////////////
// Our calculator grammar
///////////////////////////////////////////////////////////////////////////
template <typename Iterator>
struct Parser : qi::grammar<Iterator, int(), ascii::space_type>
{
int dir;
int F0;
Parser() : Parser::base_type(expression)
{
printf("init\n");
dir=0;
F0=0;
qi::lit_type lit;
qi::_val_type _val;
qi::_1_type _1;
qi::uint_type uint_;
// in [] sind die actions, wichtig: nachdem das was da ist schon beim initialisieren passiert müssen hier fÃr alles Lambda Funktionen verwendet werden!
expression =
term [_val = _1]
>> *( ("&&" >> term [_val &= _1 ] )
| ("||" >> term [_val |= _1 ] )
)
;
term =
factor [_val = _1]
| ('!' >> factor [_val = ! _1])
;
factor =
uint_ [_val = _1]
| ( lit("dir") [_val = boost::phoenix::ref(dir)] )
| ( lit("F0") >> "=" >> uint_ [boost::phoenix::ref(F0) = _1] )
// | ( lit("F0") >> "=" >> uint_ [func] )
| ( lit("F0") [_val = boost::phoenix::ref(F0)] )
;
}
qi::rule<Iterator, int(), ascii::space_type> expression, term, factor;
};
}
typedef std::string::const_iterator iterator_type;
typedef client::Parser<iterator_type> Parser;
Parser *parser=NULL;
ParseExpr::ParseExpr() {
parser = new Parser();
}
/*
int main()
{
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "Expression parser...\n\n";
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "Type an expression...or [q or Q] to quit\n\n";
typedef std::string::const_iterator iterator_type;
typedef client::calculator<iterator_type> calculator;
boost::spirit::ascii::space_type space; // Our skipper
calculator calc; // Our grammar
std::string str;
int result;
while (std::getline(std::cin, str))
{
calc.dir++;
calc.F0++;
printf("current F0:%d\n", calc.F0);
if (str.empty() || str[0] == 'q' || str[0] == 'Q')
break;
std::string::const_iterator iter = str.begin();
std::string::const_iterator end = str.end();
bool r = phrase_parse(iter, end, calc, space, result);
if (r && iter == end)
{
std::cout << "-------------------------\n";
std::cout << "Parsing succeeded\n";
std::cout << "result = " << result << std::endl;
std::cout << "-------------------------\n";
}
else
{
std::string rest(iter, end);
std::cout << "-------------------------\n";
std::cout << "Parsing failed\n";
std::cout << "stopped at: \" " << rest << "\"\n";
std::cout << "-------------------------\n";
}
}
std::cout << "Bye... :-) \n\n";
return 0;
}
*/