整理一下中缀表达式转后缀表达式(逆波兰表达式)的算法-调度场算法(shunting yard algorithm)
来源:互联网 发布:阿里云网站备案核验单 编辑:程序博客网 时间:2024/05/22 06:08
由于后缀表达式求值比较容易,一个下推栈即可,所以在编译过程中,中缀表达式会转成后缀表达式,简单算法如下:
中缀表达式转换为后缀表达式(逆波兰表达式),即调度场算法(shunting yard algorithm)
1.建立运算符栈用于运算符的存储,此运算符遵循越往栈顶优先级越高的原则。
2.预处理表达式,正、负号前加0(如果一个加号(减号)出现在最前面或左括号后面,则该加号(减号) 为正负号)。
3.顺序扫描表达式,如果当前字符是数字(优先级为0的符号),则直接输出该数字;如果当前字符为运算符或者括号(优先级不为0的符号),则判断第四点。
4.若当前运算符为'(',直接入栈;
若为')',出栈并顺序输出运算符直到遇到第一个'(',遇到的第一个'('出栈但不输出;
若为其它,比较运算符栈栈顶元素与当前元素的优先级:
如果栈顶元素是'(',当前元素直接入栈;
如果栈顶元素优先级>=当前元素优先级,出栈并顺序输出运算符直到栈顶元素优先级<当前元素优先级,然后当前元素入栈;
如果栈顶元素优先级<当前元素优先级,当前元素直接入栈。
5.重复第三点直到表达式扫描完毕。
6.顺序出栈并输出运算符直到栈元素为空。
下面给出英文版本:
- While there are tokens to be read:
- Read a token.
- If the token is a number, then add it to the output queue.
- If the token is a functiontoken, then push it onto the stack.
- If the token is a function argument separator (e.g., a comma):
- Until the token at the top of the stack is a left parenthesis, pop operators off the stack onto the output queue. If no left parentheses are encountered, either the separator was misplaced or parentheses were mismatched.
- If the token is an operator, o1, then:
- while there is an operator token, o2, at the top of the stack, and
- either o1 is left-associative and its precedence is less than or equal to that of o2,
- or o1 is right-associative and its precedence is less than that of o2,
- pop o2 off the stack, onto the output queue;
- push o1 onto the stack.
- If the token is a left parenthesis, then push it onto the stack.
- If the token is a right parenthesis:
- Until the token at the top of the stack is a left parenthesis, pop operators off the stack onto the output queue.
- Pop the left parenthesis from the stack, but not onto the output queue.
- If the token at the top of the stack is a function token, pop it onto the output queue.
- If the stack runs out without finding a left parenthesis, then there are mismatched parentheses.
- When there are no more tokens to read:
- While there are still operator tokens in the stack:
- If the operator token on the top of the stack is a parenthesis, then there are mismatched parentheses.
- Pop the operator onto the output queue.
- Exit.
- 整理一下中缀表达式转后缀表达式(逆波兰表达式)的算法-调度场算法(shunting yard algorithm)
- 调度场算法与逆波兰表达式
- 计算器表达式计算 后缀表达式+树实现 - 逆波兰表示法+调度场算法
- 逆波兰表达式 中缀表达式 后缀表达式
- 中缀表达式转后缀表达式(逆波兰表达式)
- 中缀表达式转后缀表达式(逆波兰表达式)
- 前缀、中缀、后缀表达式(逆波兰表达式)
- 中缀表达式算法实现&中缀表达式转后缀表达式
- 将中缀表达是转换为后缀表达式(逆波兰式)的一般算法
- 中缀表达式转后缀表达式(逆波兰式)
- 中缀表达式转后缀表达式(又称逆波兰式)
- 中缀表达式转后缀表达式(逆波兰)
- Shunting+yard -- RPN 算法
- 数据结构--中缀表达式转为后缀表达式(逆波兰表达式)
- 1、后缀表达式,以及中缀转后缀算法的思考
- 中缀表达式转换为后缀表达式算法
- 中缀表达式转换后缀表达式---算法
- 算法之中缀表达式和后缀表达式
- C++访问权限
- shell中各种引号的用法
- Paint一个彩虹
- vmware下能上内网,但上不了外网的解决方法
- msc
- 整理一下中缀表达式转后缀表达式(逆波兰表达式)的算法-调度场算法(shunting yard algorithm)
- ejb3 学习小结1
- iPhone高清语音功能iMessage对移动运营商产生严重威胁
- 动态库加载路径之RPATH与RUNPATH(小记)
- 冒泡排序
- [创建型设计模式:singleton]单线程设计模式验证代码
- 无奈之事
- P1000的使用感受
- 为什么我这段时间闲置Blog了