编译原理值之缀表达式生成与计算(2)
上一篇编译原理值后缀表达式生成与计算只提供了生成后缀表达式的方法,这篇文章提供如何计算后缀表达式。
先给 Parser
这个类一个 code
字段,用来保存后缀表达式:
class Parser {
public:
void expr(LexerStream* lexers)
{
}
void term(LexerStream* lexers)
{
}
void factor(LexerStream* lexers)
{
}
vector<string> code;
};
然后在每个解析函数中合适的地方压入值。具体代码前往 LearnCompilers/HLL/calculator001 查看。
class Calculator {
public:
string result() {
string current;
do {
current = code.front();
code.erase(code.begin());
if(!isOperator(current)) {
calc.push(current);
} else {
string arg0 = calc.top();
calc.pop();
string arg1 = calc.top();
calc.pop();
calc.push(calcula(current, arg0, arg1));
}
} while(!code.empty());
return calc.top();
}
bool isOperator(const string& s) ;
string calcula(const string& operation, const string& arg0, const string& arg1);
string add(const string& arg0, const string& arg1)
string sub(const string& arg0, const string& arg1);
string mul(const string& arg0, const string& arg1);
string div(const string& arg0, const string& arg1);
vector<string> code;
stack<string> calc;
}
具体看 result
函数。先取出后缀表达式中的 token
,如果是操作符,就从运算结果栈中取出两个 token
,然后进行计算,将获取到的结果压入运算结果栈中;如果不是操作符,就压入运算结果栈中。
具体代码前往 LearnCompilers/HLL/calculator001 查看。