Classic Algorithm for expression evaluation

Source: Internet
Author: User

The classic method used to evaluate an arithmetic expression in code was described by Donald knuth on July 6, 1962 (see references ). Knuth outlines this as three steps:

  • Perform syntax analysis on the infix expression
  • Converting an infix expression to a suffix expression
  • Evaluate the suffix expression

Note that the classic algorithm we mentioned is somewhat simplified: an arithmetic expression contains only the operands, binary operators, and a bracket. In addition, each operand and operator is represented by only one character, making the syntax analysis intuitive.

Expression notation

The most common forms of expression in arithmetic expressions are:Infix and prefixAndSuffixRepresentation. Infix notation is a common method for writing expressions, while prefix and suffix notation are mainly used in computer science.

Infix notation 
The infix notation is the regular expression of an arithmetic expression. It is calledInfixThe representation is because each operator is located in the middle of its operand, this notation is only applicable when the operator exactly corresponds to two operands (when the operator is a binary operator, such as addition, subtraction, multiplication, division, and modulo ). When performing syntax analysis on Expressions written in infix notation, you must use parentheses and priority rules to exclude ambiguity.

 

Syntax: operand1 operator operand2Example: (A+B)*C-D/(E+F)

 

Prefix notation 
In the prefix representation, operators are written before the operands. This representation is often used in computer science, especially in compiler design. To commemorate its inventor Jan Lukasiewicz (see references), this notation is also calledPolish notation.

 

Syntax  : operator operand1 operand2Example : -*+ABC/D+EF

 

Suffix notation 
In the suffix representation, the operator is behind the operand. Suffix notation is also calledReverse Polish notation(Reverse Polish notation, RPN) is widely used because it makes it easy to evaluate expressions.

 

Syntax  : operand1 operand2 operatorExample : AB+C*DEF+/-

 

Prefix and suffix notation have three common features:

  • The order of the operands is the same as that of the operands in the equivalent infix expression.
  • No parentheses
  • Irrelevant operator priority

Converting an infix expression to a suffix expression

To convert an expression from an infix expression to an equivalent expression expressed in a suffix notation, you must understand the priority and associativity of operators.PriorityOr the intensity of operators determines the order of values. Operators with higher priorities are evaluated first than operators with lower priorities. If all operators have the same priority, the order of evaluation depends on theirAssociativity. The concatenation of operators defines the order (from right to left or from left to right) of operators of the same priority ).

 

Left associativity  : A+B+C = (A+B)+CRight associativity : A^B^C = A^(B^C)

 

The conversion process includes using the following algorithm to read the operands, operators, and parentheses of the infix expression:

  1. Initialize an empty stack and leave the result string variable empty.
  2. Read the infix expression from left to right, one character at a time.
  3. If the character is an operand, add it to the result string.
  4. If the character is an operator, the pop operator pops up until it meets opening parenthesis, a lower-priority operator, or a right-bound symbol with the same priority. Press this operator into the (push) stack.
  5. If the character is an open bracket, press it into the stack.
  6. If the character is a closed parenthesis (closing parenthesis), all operators will pop up before it is met, and then add them to the result string.
  7. If the end of the input string is reached, all operators are displayed and added to the result string.

Suffix expression evaluate

Evaluate a suffix expression is simpler than simply evaluate an infix expression. In a suffix expression, parentheses are not required, and the operator priority is no longer effective. You can use the following algorithm to evaluate the suffix expression:

  1. Initialize an empty Stack
  2. Read suffix expressions from left to right
  3. If the character is an operand, press it into the stack.
  4. If the character is an operator, two operands are displayed, perform the appropriate operation, and then press the result into the stack. If you cannot bring up two operands, the syntax of the suffix expression is incorrect.
  5. To the end of the suffix expression, the result is displayed from the stack. If the suffix expression format is correct, the stack should be empty.

Reprinted from: http://www-128.ibm.com/developerworks/cn/java/j-w3eva/index.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.