这是我在这里发布的第一篇帖子 :)
我发现有很多关于Shunting yard算法的问题,但我希望仍然有论坛成员愿意帮助我解决这个算法的另一个问题。
我搜索了其他帖子,看看是否已经回答了我的问题,并在其他论坛和互联网上进行了一些研究:
http://stackoverflow.com/questions/16877546/modifying-the-shunting-yard-algorithm-c
http://www.computerhope.com/forum/index.php?topic=146535.0
http://en.wikipedia.org/wiki/Shunting-yard_algorithm
http://www.autoitscript.com/forum/topic/164627-shunting-yard-with-functions/
我的代码是用vb-script编写的,因为我喜欢它的简单性,而且我不知道java或类似语言的c..
我的问题:
目前这个算法允许错误使用“(”和“)”,例如: function((10,20,)30),虽然这显然不是调用函数的正确方法...
我也不确定我的代码是否编写正确,维基百科上的伪代码是我的参考,但不是很清晰:(
我还计划扩展它,加入if-else语句和嵌套循环等内容,因为主要目标是用类似vb的语言编写一种解释器作为学习项目 :)
我的代码[编辑]:
SET PRECEDENCE = CREATEOBJECT("SCRIPTING.DICTIONARY")
WITH PRECEDENCE
.ADD "^",3
.ADD "*",2
.ADD "/",2
.ADD "%",2
.ADD "+",1
.ADD "-",1
.ADD "FUNCTION",0
.ADD "(",0
.ADD ",",0
.ADD ")",0
END WITH
'#############################################################################
tokenArray = split("FUNCTION ( ( A , B ) , C )")
msgbox SHUNTINGYARD(tokenArray)
'#############################################################################
FUNCTION SHUNTINGYARD(INPUT)
TOKEN_QUEUE = ARRAY()
TOKEN_STACK = ARRAY()
FOR TOKEN_NUMBER = 0 TO UBOUND(INPUT)
SELECT CASE INPUT(TOKEN_NUMBER)
CASE "("
CALL PUSH(INPUT(TOKEN_NUMBER), TOKEN_STACK)
CASE ")"
DO WHILE NOT( PRECEDENCE( PEEK(TOKEN_STACK) ) = 0 )
CALL PUSH(POP(TOKEN_STACK), TOKEN_QUEUE)
IF STACKISEMPTY(TOKEN_STACK) THEN CALL ERRORS("Can't find a matching ""("".", TRUE)
LOOP
IF PEEK(TOKEN_STACK) = "FUNCTION" THEN
DISCARD = POP(TOKEN_STACK)
CALL PUSH("@", TOKEN_QUEUE)
ELSE
DISCARD = POP(TOKEN_STACK)
END IF
CASE ","
DO WHILE NOT( PRECEDENCE( PEEK(TOKEN_STACK) ) = 0 )
CALL PUSH(POP(TOKEN_STACK), TOKEN_QUEUE)
IF STACKISEMPTY(TOKEN_STACK) THEN CALL ERRORS("Can't find a matching function ""("".", TRUE)
LOOP
CASE "+","-","*","/","^","%"
TOKEN_A = INPUT(TOKEN_NUMBER)
DO WHILE ISOPERATOR(PEEK(TOKEN_STACK))
TOKEN_B = PEEK(TOKEN_STACK)
IF (ASSOCIATIVITY(TOKEN_B) = "left" AND PRECEDENCE(TOKEN_A) = PRECEDENCE(TOKEN_B)) OR (PRECEDENCE(TOKEN_A) < PRECEDENCE(TOKEN_B)) THEN
CALL PUSH(POP(TOKEN_STACK), TOKEN_QUEUE)
ELSE
EXIT DO
END IF
LOOP
CALL PUSH(TOKEN_A, TOKEN_STACK)
CASE ELSE
CALL PUSH(INPUT(TOKEN_NUMBER), TOKEN_QUEUE)
END SELECT
NEXT
FOR ITEMCOUNT = 0 TO UBOUND(TOKEN_STACK)
IF PEEK(TOKEN_STACK) = "(" THEN CALL ERRORS("Can't find a matching "")"".", TRUE)'(
CALL PUSH(POP(TOKEN_STACK), TOKEN_QUEUE)
NEXT
SHUNTINGYARD = JOIN(TOKEN_QUEUE,"|")
END FUNCTION
'#############################################################################
FUNCTION ASSOCIATIVITY(ASSOC)
SELECT CASE LCASE(ASSOC)
CASE "^","\"
ASSOCIATIVITY = "right"
CASE ELSE
ASSOCIATIVITY = "left"
END SELECT
END FUNCTION
FUNCTION ISOPERATOR(ITEM)
ISOPERATOR = LEN(ITEM) = 1 AND INSTR("+-*/%^",ITEM)
END FUNCTION
SUB PUSH(ITEM,BYREF STACK)
IF UBOUND(STACK) > -1 THEN
REDIM PRESERVE STACK(UBOUND(STACK) + 1)
STACK(UBOUND(STACK)) = ITEM
ELSE
STACK = ARRAY(ITEM)
END IF
END SUB
FUNCTION POP(BYREF STACK)
IF UBOUND(STACK) > -1 THEN
POP = STACK(UBOUND(STACK))
REDIM PRESERVE STACK(UBOUND(STACK) - 1)
END IF
END FUNCTION
FUNCTION STACKISEMPTY(STACK)
IF UBOUND(STACK) > -1 THEN
STACKISEMPTY = FALSE
ELSE
STACKISEMPTY = TRUE
END IF
END FUNCTION
FUNCTION PEEK(STACK)
IF UBOUND(STACK) > -1 THEN
PEEK = STACK(UBOUND(STACK))
END IF
END FUNCTION