Editing Shunting yard algorithm

Jump to: navigation, search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 19: Line 19:
 
* an opening parenthesis behaves like the beginning of an expression, in the sense that none of the operands ''after'' belong to ''any'' of the operands ''before'' it; thus, when we encounter one, we push it onto the stack, and, until the corresponding closing parenthesis is encountered, we cannot examine any of the operators beneath it. So in the expression <code>1*2+3</code>, we would normally pop off the <code>*</code> after encountering the <code>+</code>; but if we have <code>1*(2+3)</code> instead, then, upon reaching the <code>+</code>, the stack will have <code>(</code> on the top and <code>*</code> below it; so here we do not pop off anything at all.
 
* an opening parenthesis behaves like the beginning of an expression, in the sense that none of the operands ''after'' belong to ''any'' of the operands ''before'' it; thus, when we encounter one, we push it onto the stack, and, until the corresponding closing parenthesis is encountered, we cannot examine any of the operators beneath it. So in the expression <code>1*2+3</code>, we would normally pop off the <code>*</code> after encountering the <code>+</code>; but if we have <code>1*(2+3)</code> instead, then, upon reaching the <code>+</code>, the stack will have <code>(</code> on the top and <code>*</code> below it; so here we do not pop off anything at all.
 
* a closing parenthesis behaves like the end of an expression, in the sense that once we reach it, we pop off all remaining operators that have accumulated on the stack since the corresponding opening parenthesis was encountered, and transfer them to the output stream. Finally, we pop off the <code>(</code>. However, we do not add either parenthesis to the output stream, since parentheses do not appear in postfix expressions.
 
* a closing parenthesis behaves like the end of an expression, in the sense that once we reach it, we pop off all remaining operators that have accumulated on the stack since the corresponding opening parenthesis was encountered, and transfer them to the output stream. Finally, we pop off the <code>(</code>. However, we do not add either parenthesis to the output stream, since parentheses do not appear in postfix expressions.
Actually, if we add an opening parenthesis at the beginning of the input, and another one at the end, then we can eliminate the need to pop off all remaining operators at the end (as they will be popped when the final parenthesis is encountered), as well as the need to check whether the stack ever becomes empty (as it will always contain at least the initial parenthesis).
 
  
 
===Summary===
 
===Summary===
 
Start off with an empty output stream and an empty stack. Repeatedly read a symbol from the input.  
 
Start off with an empty output stream and an empty stack. Repeatedly read a symbol from the input.  
 
* If it is part of a number (''i.e.'', a digit or a decimal separator), then keep reading tokens until an operand or parenthesis is encountered, and convert the entire string just read into a number, and transfer the number to the output stream.
 
* If it is part of a number (''i.e.'', a digit or a decimal separator), then keep reading tokens until an operand or parenthesis is encountered, and convert the entire string just read into a number, and transfer the number to the output stream.
* If it is a left-associative operator, then repeatedly pop from the stack into the output stream until either the stack becomes empty or the top of the stack is a parenthesis or a lower-precedence operator. After that, push it onto the stack.
+
* If it is a left-associative operator, then repeatedly pop from the stack into the output stream until either the stack becomes empty or the top of the stack is a parenthesis or a lower-precedence operator.
* If it is a right-associative operator, then repeatedly pop from the stack into the output stream until either the stack becomes empty or the top of the stack is a parenthesis or an operator of lower or equal precedence. After that, push it onto the stack.
+
* If it is a right-associative operator, then repeatedly pop from the stack into the output stream until either the stack becomes empty or the top of the stack is a parenthesis or an operator of lower or equal precedence.
 
* If it is an opening parenthesis, push it onto the stack.
 
* If it is an opening parenthesis, push it onto the stack.
 
* If it is a closing parenthesis, repeatedly pop operators from the stack into the output stream until an opening parenthesis is encountered. Pop the opening parenthesis off the stack, but do not emit it into the output stream.
 
* If it is a closing parenthesis, repeatedly pop operators from the stack into the output stream until an opening parenthesis is encountered. Pop the opening parenthesis off the stack, but do not emit it into the output stream.
Line 34: Line 33:
 
==Extensions==
 
==Extensions==
 
===Unary operators===
 
===Unary operators===
Unary minus signs (and possibly unary plus signs) often appear in infix expressions. There are three modifications that have to be made to the algorithm in order to handle these:
+
Unary minus signs (and possibly unary plus signs) often appear in infix expressions. There are two modifications that have to be made to the algorithm in order to handle these:
 
* A minus sign is always binary if it immediately follows an operand or a right parenthesis, and it is always unary if it immediately follows another operator or a left parenthesis, or if it occurs at the very beginning of the input. The algorithm must be modified in order to distinguish between the two.
 
* A minus sign is always binary if it immediately follows an operand or a right parenthesis, and it is always unary if it immediately follows another operator or a left parenthesis, or if it occurs at the very beginning of the input. The algorithm must be modified in order to distinguish between the two.
* A unary minus sign does not cause any operators to be popped from the stack. This is because, in the postfix output, the unary minus sign will always immediately follow its operand (whereas it always immediately precedes it in the infix), so no other operators can be popped before it at this point.
 
 
* Unary minus signs and binary minus signs must be distinguished in the output in order to avoid ambiguity. Because postfix expressions are intended to be evaluated from left to right, we have a problem with an expression like <code>1 2 - 3 +</code> if the minus sign is allowed to be unary; upon reaching it, we cannot determine whether it is unary or binary. If it is binary, then both of the preceding operands belong to it, and if it is unary, then only one of the two belongs to it, but perhaps the other belongs to some following operator. It is advisable to have some separate symbol for unary and binary minus signs, as is common in handheld scientific calculators. Also, this symbolic distinction must be made before the operator is pushed onto the stack, because once it is on the stack, we lose the ability to retrospectively determine whether it was supposed to be unary or binary.
 
* Unary minus signs and binary minus signs must be distinguished in the output in order to avoid ambiguity. Because postfix expressions are intended to be evaluated from left to right, we have a problem with an expression like <code>1 2 - 3 +</code> if the minus sign is allowed to be unary; upon reaching it, we cannot determine whether it is unary or binary. If it is binary, then both of the preceding operands belong to it, and if it is unary, then only one of the two belongs to it, but perhaps the other belongs to some following operator. It is advisable to have some separate symbol for unary and binary minus signs, as is common in handheld scientific calculators. Also, this symbolic distinction must be made before the operator is pushed onto the stack, because once it is on the stack, we lose the ability to retrospectively determine whether it was supposed to be unary or binary.
It should also be pointed out that the unary minus sign is usually treated as though it has higher precedence than <code>*</code> and <code>/</code>. For example, the expression <code>10/-1*-2</code> usually evaluates to 20 rather than 5.
 
  
 
===Functions===
 
===Functions===
Line 51: Line 48:
 
===Evaluation===
 
===Evaluation===
 
It is a simple matter to obtain the postfix form of the input expression and then evaluate it, as postfix can be easily evaluated from left to right; it requires no knowledge of operator precedence, and contains no parentheses. However, we can also directly evaluate the input, without converting it into postfix. To do this, we replace the output stream with an output stack. Every time we encounter an operand, as before, we ''push'' it onto the output stack. Whenever we would normally transfer an operator into the output stream, we instead perform an evaluation; we pop off the correct number of operands from the output stack, apply the operator, and then push the result back onto the output stream. At the end of the input, as before, we process all remaining operators; and we should be left with a single value in the output stack&mdash;this is the value of the entire expression. So, for example, whenever we pop a <code>*</code> from the operator stack, we pop the top operand off the output stack; call this <code>y</code>; and we pop the next operand off the output stack too; call this <code>x</code>; and we push <code>x*y</code> onto the output stack. (Note that the order of the operands is the reverse of the order in which we pop them off; this is because a stack is a last-in, first-out structure.)
 
It is a simple matter to obtain the postfix form of the input expression and then evaluate it, as postfix can be easily evaluated from left to right; it requires no knowledge of operator precedence, and contains no parentheses. However, we can also directly evaluate the input, without converting it into postfix. To do this, we replace the output stream with an output stack. Every time we encounter an operand, as before, we ''push'' it onto the output stack. Whenever we would normally transfer an operator into the output stream, we instead perform an evaluation; we pop off the correct number of operands from the output stack, apply the operator, and then push the result back onto the output stream. At the end of the input, as before, we process all remaining operators; and we should be left with a single value in the output stack&mdash;this is the value of the entire expression. So, for example, whenever we pop a <code>*</code> from the operator stack, we pop the top operand off the output stack; call this <code>y</code>; and we pop the next operand off the output stack too; call this <code>x</code>; and we push <code>x*y</code> onto the output stack. (Note that the order of the operands is the reverse of the order in which we pop them off; this is because a stack is a last-in, first-out structure.)
 +
 +
Also, with direct evaluation, unary plus and minus signs are easier to handle; we just treat them as though they are preceded by a zero (that is, when we encounter them, we push a zero onto the output stack, and then treat the operator as though it were binary).
  
 
===Conversion into syntax tree===
 
===Conversion into syntax tree===
Direct conversion into a syntax tree follows the same principle as direct evaluation (the previous section). Here, we keep an output stack of subtrees. Every operand gets pushed onto the output stack immediately as a singleton tree, and, instead of pushing an operator directly onto the output stack, we pop off the appropriate number of operands from the output stack and make them the children of a new subtree (whose root is the operator), which we then push back onto the output stack. After the algorithm has finished, the output stack should contain a single tree: the syntax tree for the entire expression.
+
Direct conversion into a syntax tree follows the same principle as direct evaluation (the previous section). Here, we keep an output stack of subtrees. Every operand gets pushed onto the output stack immediately as a singleton tree, and, instead of pushing an operand directly onto the output stack, we pop off the appropriate number of operands from the output stack and make them the children of a new subtree (whose root is the operator), which we then push back onto the output stack. After the algorithm has finished, the output stack should contain a single tree: the syntax tree for the entire expression.
  
 
The use of a syntax tree representation relieves us of the responsibility of finding some way to tag unary minus signs and variadic functions in the output (although we must still identify unary minus signs before pushing them onto the operator stack); the correct number of operands of an operator is simply the number of children the operator's node has in the final syntax tree. A variation of the algorithm, in which function arguments are directly added as children to the function token still waiting on the operator stack as soon as they are finished construction, also eliminates the need for the arity stack; details are left as an exercise for the reader.
 
The use of a syntax tree representation relieves us of the responsibility of finding some way to tag unary minus signs and variadic functions in the output (although we must still identify unary minus signs before pushing them onto the operator stack); the correct number of operands of an operator is simply the number of children the operator's node has in the final syntax tree. A variation of the algorithm, in which function arguments are directly added as children to the function token still waiting on the operator stack as soon as they are finished construction, also eliminates the need for the arity stack; details are left as an exercise for the reader.
 
==Code==
 
* [[Shunting yard algorithm/foo.hs|Haskell reference implementation for this article]]
 
  
 
==References==
 
==References==
 
# Dijkstra, E. W. (1961). ''ALGOL-60 Translation''. Retrieved from http://www.cs.utexas.edu/~EWD/MCReps/MR35.PDF
 
# Dijkstra, E. W. (1961). ''ALGOL-60 Translation''. Retrieved from http://www.cs.utexas.edu/~EWD/MCReps/MR35.PDF
 
# [http://en.wikipedia.org/w/index.php?title=Shunting-yard_algorithm&oldid=477381628 Wikipedia]
 
# [http://en.wikipedia.org/w/index.php?title=Shunting-yard_algorithm&oldid=477381628 Wikipedia]

Please note that all contributions to PEGWiki are considered to be released under the Attribution 3.0 Unported (see PEGWiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

Cancel | Editing help (opens in new window)