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===
Line 51: Line 50:
 
===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===

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)