Algorithm to Evaluate Expressions Using Prefix Notation

Algorithm to Evaluate Expressions Using Prefix Notation

<p> Algorithm To Evaluate Expressions Using Prefix Notation</p><p>Initialization: </p><p>1. Read a token from the input stream. a. If it is an operand, push it into the operand stack. b. If it is an operator, push a marker onto operand stack and push the actual operator onto operator stack. </p><p>Loop While (there are more input tokens):</p><p>1. Read a token.</p><p>2. If token is an operator, push operator onto operator stack and push the marker onto operand stack. </p><p>3. Else if, the token is an operand, check the top of the operand stack for a marker. </p><p> a. If the marker is on top of the stack, push the read operand onto the operand stack. </p><p> b. Else If – the top of the operand stack contains another operand: </p><p>Loop While (the top of the stack is an operand): 1. Pop operand from operand stack. </p><p>2. Pop marker from operand stack and the corresponding operator from the operator stack. </p><p>3. Apply the operation on the read token and popped operand and obtain result. </p><p>4. 1 If the top of the stack is another operand, continue Looping (as stated above in Loop While constraint). </p><p>4.2 Else if the top of the stack is an operator or the operand stack is empty, push the result and exit the loop.</p><p>4. Go back to step 1 (loop) Loop is finished:</p><p>When the loop is finished (all of the input has been read), the value on top of the operand stack is the result of evaluating an expression using the prefix notation. </p><p>Some possible errors: </p><p>The operand stack was not empty at the end of algorithm.</p><p>The operator stack was not empty at the end of algorithm. </p><p>An operator is the last token in the input. </p><p>Reading illegal token from the input. </p>

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    2 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us