Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 将中缀转换为反向波兰符号(后缀)的方法_Algorithm_Search_Postfix Notation_Infix Notation_Shunting Yard - Fatal编程技术网

Algorithm 将中缀转换为反向波兰符号(后缀)的方法

Algorithm 将中缀转换为反向波兰符号(后缀)的方法,algorithm,search,postfix-notation,infix-notation,shunting-yard,Algorithm,Search,Postfix Notation,Infix Notation,Shunting Yard,除了Dijkstra调车场算法外,是否有其他方法将中缀转换为RPN?我试图通过与另一种转换方法的比较来研究调车场算法的优缺点。非常感谢与调车场算法日志的任何链接。谢谢当然有,LR解析,解析器组合器,可能还有很多其他的。在现实生活中,我总是递归解析表达式 在python中,基本算法如下所示: import re import sys def toRpn(infixStr): # divide string into tokens, and reverse so I can get the

除了Dijkstra调车场算法外,是否有其他方法将中缀转换为RPN?我试图通过与另一种转换方法的比较来研究调车场算法的优缺点。非常感谢与调车场算法日志的任何链接。谢谢

当然有,LR解析,解析器组合器,可能还有很多其他的。

在现实生活中,我总是递归解析表达式

在python中,基本算法如下所示:

import re
import sys

def toRpn(infixStr):
    # divide string into tokens, and reverse so I can get them in order with pop()
    tokens = re.split(r' *([\+\-\*\^/]) *', infixStr)
    tokens = [t for t in reversed(tokens) if t!='']
    precs = {'+':0 , '-':0, '/':1, '*':1, '^':2}

    #convert infix expression tokens to RPN, processing only
    #operators above a given precedence
    def toRpn2(tokens, minprec):
        rpn = tokens.pop()
        while len(tokens)>0:
            prec = precs[tokens[-1]]
            if prec<minprec:
                break
            op=tokens.pop()

            # get the argument on the operator's right
            # this will go to the end, or stop at an operator
            # with precedence <= prec
            arg2 = toRpn2(tokens,prec+1)
            rpn += " " + arg2 + " " +op
        return rpn

    return toRpn2(tokens,0)

print toRpn("5+3*4^2+1")

#prints: 5 3 4 2 ^ * + 1 +
重新导入
导入系统
def toRpn(infixStr):
#将字符串划分为标记,然后反向,这样我就可以使用pop()按顺序获取它们
tokens=re.split(r'*([\+\-\*\^/])*,infixStr)
令牌=[t表示t,如果t!='',则反转(令牌)
precs={'+':0'-':0'/':1'*':1'^':2}
#将中缀表达式标记转换为RPN,仅处理
#高于给定优先级的运算符
def toRpn2(代币,minprec):
rpn=tokens.pop()
而len(代币)>0:
prec=precs[令牌[-1]]
如果预先