algorithm - Conceptual issue occurred while converting infix notation to postfix notation -


how can realize/understand preferential order/precedence of operators while converting infix notation postfix notation : " * ", " / ", " + ", " - ", " ^ ", " ) ", " ( ".

i understand 1 can @ algorithm , solve don't want that. should thought process?

operator precedence convention , property of formal infix language indicate operations should evaluate first. higher precedence operation means should operate before lower precedence operations.

grouping parentheses not part of operator precedence. (note other kinds of parentheses such function call parentheses may assuming not refer here) used explicitly indicate order of operations. parentheses useful indicate order of operations in infix notation. purpose of operator precedence convention in given language avoid using parentheses in case. so, example, if want multiply 4 5 , add 7 result, can write:

4*5+7 

this valid under normal arithmetic operator precedence rules because multiplication ('*') has higher precedence addition ('+'). if want add 3 , 4 , multiply result 8, need write:

(3+4)*8 

in case want order of operations different normal order of "higher precedence operations first." in other words, parenthesis necessary when using infix notation , want operations execute in order other precedence-order.

in standard arithmetic, exponentiation ("^") has highest precedence. next multiplication , division (equal precedence) , addition , subtraction. therefore, infix expression written using these operators without parenthesis evaluate exponentiation first, multiplications , divisions (in left right order) , additions , subtractions, again in left right order.

if want infer operator precedence of unknown language, need @ places parentheses , not used. since valid use parentheses everywhere when unnecessary, heuristic. examples above, can write:

((4*5)+7) 

and gives no hint operator precedence. because every binary operator in case has parentheses, , therefore @ least 1 of 2 sets redundant assuming precedence of addition , multiplication not same.

similarly, looking @ next example:

(3+4)*8 

since parentheses used around addition not multiplication, can infer in language addition has lower precedence multiplication. otherwise, parenthesis redundant. pattern parentheses , not used try figure out operator precedence in unknown languages. more common assume precedence level based on formal specification of language under consideration. formal languages have operator precedence chart infix form avoid ambiguity.

we never need parentheses in prefix or postfix languages because order of terms , operators makes order of evaluation explicit. therefore issue infix-language-specific problem.


Comments