Blog Archive

Data Structure - Expression Parsing ( Infix Notation, Prefix Notation, Postfix Notation)

 

Data Structure - Expression Parsing

 


 

The way to write arithmetic expression is known as a notation. An arithmetic expression can be written in three different but equivalent notations, i.e., without changing the essence or output of an expression. These notations are −

  • Infix Notation
  • Prefix (Polish) Notation
  • Postfix (Reverse-Polish) Notation

Infix Notation

We write expression in infix notation, e.g. a - b + c, where operators are used in-between operands. It is easy for us humans to read, write, and speak in infix notation but the same does not go well with computing devices. An algorithm to process infix notation could be difficult and costly in terms of time and space consumption.

Prefix Notation

In this notation, operator is prefixed to operands, i.e. operator is written ahead of operands. For example, +ab. This is equivalent to its infix notation a + b. Prefix notation is also known as Polish Notation.

Postfix Notation

This notation style is known as Reversed Polish Notation. In this notation style, the operator is postfixed to the operands i.e., the operator is written after the operands. For example, ab+. This is equivalent to its infix notation a + b.

 

 

Precedence

When an operand is in between two different operators, which operator will take the operand first, is decided by the precedence of an operator over others. For example −

As multiplication operation has precedence over addition, b * c will be evaluated first. A table of operator precedence is provided later.

Associativity

Associativity describes the rule where operators with the same precedence appear in an expression. For example, in expression a + b − c, both + and – have the same precedence, then which part of the expression will be evaluated first, is determined by associativity of those operators. Here, both + and − are left associative, so the expression will be evaluated as (a + b) − c.

Precedence and associativity determines the order of evaluation of an expression. Following is an operator precedence and associativity table (highest to lowest) −


The above table shows the default behavior of operators. At any point of time in expression evaluation, the order can be altered by using parenthesis. For example −

In a + b*c, the expression part b*c will be evaluated first, with multiplication as precedence over addition. We here use parenthesis for a + b to be evaluated first, like (a + b)*c.

Convert infix to Postfix

Example :1

A+B

Step 1

Expression string : A

Operator Stack : +

Remaining Expression : B

Step 2

Expression string : AB

Operator Stack : +

Postfix  Expression : AB+

Example :2

((A+B)-C*(D/E))+F

Step 1

Expression string :

Operator Stack : ((

Remaining Expression : A+B)-C*(D/E))+F

Step 2

Expression string : A

Operator Stack : ((

Remaining Expression : +B)-C*(D/E))+F

Step 3

Expression string : A

Operator Stack : ((+

Remaining Expression : B)-C*(D/E))+F

Step 4

Expression string : AB

Operator Stack : ((+

Remaining Expression : )-C*(D/E))+F

Step 5

Expression string : AB+

Operator Stack : (

Remaining Expression : -C*(D/E))+F

Step 6

Expression string : AB+

Operator Stack : (-

Remaining Expression : C*(D/E))+F

Step 7

Expression string : AB+C

Operator Stack : (-

Remaining Expression : *(D/E))+F

Step 8

Expression string : AB+C

Operator Stack : (-*

Remaining Expression : (D/E))+F

Step 9

Expression string : AB+C

Operator Stack : (-*(

Remaining Expression : D/E))+F

Step 10

Expression string : AB+CD

Operator Stack : (-*(

Remaining Expression : /E))+F

Step 11

Expression string : AB+CD

Operator Stack : (-*(/

Remaining Expression : E))+F

Step 12

Expression string : AB+CDE

Operator Stack : (-*(/

Remaining Expression : ))+F

Step 13

Expression string : AB+CDE/*-

Operator Stack :

Remaining Expression : +F

Step 14

Expression string : AB+CDE/*-

Operator Stack : +

Remaining Expression : F

Step 15

Expression string : AB+CDE/*-F

Operator Stack : +

Remaining Expression :

Step 16

Expression string : AB+CDE/*-F+

Operator Stack :

Postfix Expression : AB+CDE/*-F+

 


 

 

 

 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.

PG -TRB COMPUTER INSTRUCTOR C plus plus Class : 4 switch do while

  C++ switch The C++ switch statement executes one statement from multiple conditions. It is like if-else-if ladder statement in C++. sw...