用boost::spirit实现的表达式求值
2009-03-27 16:18
225 查看
用boost::spirit实现一个表达式求值看上去比较简单。我这个还有点问题,有空格时会解析失败,请大家看看是什么原因?
123*34+2323/324
parsing succeeded
result = 4189
2 + 3
parsing failed
#include <iostream>
#include <stack>
#include <functional>
#include <boost/function.hpp>
// #define BOOST_SPIRIT_DEBUG
#include <boost/spirit.hpp>
using namespace std;
stack<int> evaluationStack;
struct Push
{
void operator()(int d) const
{
evaluationStack.push(d);
}
};
void doOp(boost::function<int(int, int)> op)
{
int rhs = evaluationStack.top();
evaluationStack.pop();
int lhs = evaluationStack.top();
evaluationStack.pop();
int result = op(lhs, rhs);
evaluationStack.push(result);
}
void add2(char const*, char const*) { doOp(std::plus<int>()); }
void sub2(char const*, char const*) { doOp(std::minus<int>()); }
void mul2(char const*, char const*) { doOp(std::multiplies<int>()); }
void div2(char const*, char const*) { doOp(std::divides<int>()); }
int main()
{
using namespace boost::spirit;
/*
group ::= '(' expression ')'
factor ::= integer | group
term ::= factor (('*' factor) | ('/' factor))*
expression ::= term (('+' term) | ('-' term))*
Spirit:
group = '(' >> expression >> ')';
factor = integer | group;
term = factor >> *(('*' >> factor) | ('/' >> factor));
expression = term >> *(('+' >> term) | ('-' >> term));
*/
rule<> group, factor, term, expression;
group = '(' >> expression >> ')';
factor = int_p[Push()] | group;
term = factor
>> *( ('*' >> factor) [&mul2]
| ('/' >> factor) [&div2] )
;
expression = term
>> *( ('+' >> term) [&add2]
| ('-' >> term) [&sub2] )
;
string s;
while (getline(cin, s))
{
if (parse(s.c_str(), expression, space_p).full)
{
cout << "parsing succeeded/n";
cout << "result = " << evaluationStack.top();
evaluationStack.pop();
}
else
{
cout << "parsing failed";
}
cout << endl;
}
}
123*34+2323/324
parsing succeeded
result = 4189
2 + 3
parsing failed
#include <iostream>
#include <stack>
#include <functional>
#include <boost/function.hpp>
// #define BOOST_SPIRIT_DEBUG
#include <boost/spirit.hpp>
using namespace std;
stack<int> evaluationStack;
struct Push
{
void operator()(int d) const
{
evaluationStack.push(d);
}
};
void doOp(boost::function<int(int, int)> op)
{
int rhs = evaluationStack.top();
evaluationStack.pop();
int lhs = evaluationStack.top();
evaluationStack.pop();
int result = op(lhs, rhs);
evaluationStack.push(result);
}
void add2(char const*, char const*) { doOp(std::plus<int>()); }
void sub2(char const*, char const*) { doOp(std::minus<int>()); }
void mul2(char const*, char const*) { doOp(std::multiplies<int>()); }
void div2(char const*, char const*) { doOp(std::divides<int>()); }
int main()
{
using namespace boost::spirit;
/*
group ::= '(' expression ')'
factor ::= integer | group
term ::= factor (('*' factor) | ('/' factor))*
expression ::= term (('+' term) | ('-' term))*
Spirit:
group = '(' >> expression >> ')';
factor = integer | group;
term = factor >> *(('*' >> factor) | ('/' >> factor));
expression = term >> *(('+' >> term) | ('-' >> term));
*/
rule<> group, factor, term, expression;
group = '(' >> expression >> ')';
factor = int_p[Push()] | group;
term = factor
>> *( ('*' >> factor) [&mul2]
| ('/' >> factor) [&div2] )
;
expression = term
>> *( ('+' >> term) [&add2]
| ('-' >> term) [&sub2] )
;
string s;
while (getline(cin, s))
{
if (parse(s.c_str(), expression, space_p).full)
{
cout << "parsing succeeded/n";
cout << "result = " << evaluationStack.top();
evaluationStack.pop();
}
else
{
cout << "parsing failed";
}
cout << endl;
}
}
相关文章推荐
- 用boost::spirit实现的表达式求值
- 栈实现表达式求值
- 数据结构之应用 "栈(Stack)" 实现: 解析算术表达式及计算求值 (C#/Java)
- 利用栈实现算术表达式求值(Java语言描述)
- 简单表达式求值(用栈实现)
- 数据结构之——用C++实现算术表达式求值
- boost spirit 关于复杂表达式的解析
- 栈---定义、应用(递归、后缀表达式实现数学表达式求值)
- 奇怪的表达式求值 (java实现)
- 表达式求值 (栈) 用C++实现
- 数据结构(19)栈典型问题之C++实现表达式求值
- 表达式求值——栈实现
- 分别用C++和JavaScript 实现四则运算表达式求值
- 表达式求值--栈(c++/java实现)
- c语言:表达式求值实现(包含加减乘除括号)
- “栈”的典型应用—表达式求值(C语言实现)
- 表达式求值,中缀后缀转换,表达式递归直接求值等相关算法的实现
- 利用栈实现简单算术表达式求值
- “栈”的典型应用—表达式求值(C语言实现)
- 数据结构之应用 "栈(Stack)" 实现: 解析算术表达式及计算求值 (C#/Java)