Tuesday, 13 September 2016

In any language, arithmetic operation is support is needed which is same as mathematical expression. But writing data structure for understanding arithmetic expression has so many complications. Like scanning direction, operations priority and after that how to decide the priority if any parenthesis is coming. So designing compiler for any language has a need to take care of these complications. But support of arithmetic expressions one of basic requirement of any language. Let us take an arithmetic expression 

a+b/3
Suppose a=0 and b=6. suppose we are diving operation first then add. So the resultant value will be


9+6/3
=9+2
=11
Now, suppose we are talking and operation first then divide operation. So the resultant 
9+6/3
=15/3
=5

Here we can see that priority of operation is giving different result. So it is decided that scanning will be from left to right and each operation will have some precedence  level. Now suppose we are taking parenthesis in expression. let us take the same expression with parenthesis

(a+b)/3

Here first the operation will be for expression inside the parenthesis . So we have a need to decide the priority should be highest.

Here we will take five operation '+','-','*','/' and '^' (for exponential)  and based on that all expression will be given.

Level 2      ^ (Exponentiation)
Level 1      * (Multiplication and Division)
Level 0       + (Addition  or subtraction)

Here Level 2 is highest priority and Level 0 is lowest.