0
我设法找到关于维基百科以下伪代码演示了如何使用调度场算法来创建一个定位后表达:如何使用分流码算法创建抽象树,而不是后缀表达式?
While there are tokens to be read:
Read a token.
If the token is a number, then push it to the output queue.
If the token is a function token, 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 operator 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 has precedence less than that of o2,
pop o2 off the operator stack, onto the output queue;
at the end of iteration push o1 onto the operator stack.
If the token is a left parenthesis (i.e. "("), then push it onto the stack.
If the token is a right parenthesis (i.e. ")"):
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.
我如何修改这个算法来产生一个抽象语法树,什么我应该怎么做而不是推动操作员或操作数到输出?
看一看[这里](http://softwareengineering.stackexchange.com/questions/254074/how-exactly-is-an-abstract-syntax-tree-created?rq=1)。 –
谢谢,我只是做同样的事情,但把操作数推到表达式栈而不是输出。当一个操作符被推送到一个输出时,将栈顶的两个表达式连接起来? – user3102599