

{"id":96451,"date":"2021-06-12T09:00:21","date_gmt":"2021-06-12T03:30:21","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=96451"},"modified":"2021-06-03T20:52:42","modified_gmt":"2021-06-03T15:22:42","slug":"expression-parsing-in-data-structure","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/expression-parsing-in-data-structure\/","title":{"rendered":"Expression Parsing in Data Structure"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">A+B-(C*D)\/E+X*Y-<\/span><\/p>\n<p><span style=\"font-weight: 400;\">I am sure you must have seen this kind of expression at some point in your life. When you approach solving it, you assign the values to the variable and perform the operations based on the BODMAS rule. But that is not how expression solving works in computer systems. When you enter an expression like this in the system, it reads it as a string.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">In this article, we will be learning types of expression notations, precedence order, and associativity. Later we will be focusing on how a computer system approaches solving an arithmetic or logical expression<\/span><\/p>\n<h3>Expression Parsing<\/h3>\n<p><span style=\"font-weight: 400;\">Expression parsing in data structure refers to the evaluation of arithmetic and logical expressions. The arithmetic expression can be written in three forms:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">1. Infix notation<\/span><\/p>\n<p><span style=\"font-weight: 400;\">2. Postfix notation<\/span><\/p>\n<p><span style=\"font-weight: 400;\">3. Prefix notation<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The final answers to the arithmetic or logical expression remain the same no matter in which form they are represented.<\/span><\/p>\n<h3>Associativity in Expression Parsing<\/h3>\n<p><span style=\"font-weight: 400;\">It is used when two operators of the same precedence are present in an expression. Associativity can be:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">1. left to right<\/span><\/p>\n<p><span style=\"font-weight: 400;\">2. right to left.<\/span><\/p>\n<p><b>For example:<\/b><span style=\"font-weight: 400;\">\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Expression 40\/20*20 is evaluated as :<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0= (40\/20)*20<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0= 2*20<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u00a0= 40<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Since the order of precedence for * and \/ is the same, expression is solved according to associativity, which for \u2731 and <\/span><b>\/<\/b><span style=\"font-weight: 400;\"> is left to right.<\/span><\/p>\n<h3>Order of precedence in Expression parsing<\/h3>\n<p><span style=\"font-weight: 400;\">Order of precedence is the order that the operators in an expression will be following during execution. If two or more symbols of the same precedence level are preset in an expression, then the expression is read according to associativity.<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>Operator<\/b><\/td>\n<td><b>Description<\/b><\/td>\n<td><b>Associativity<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">()<\/span><\/td>\n<td><span style=\"font-weight: 400;\">parenthesis<\/span><\/td>\n<td rowspan=\"5\"><span style=\"font-weight: 400;\">left-to-right<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">[]<\/span><\/td>\n<td><span style=\"font-weight: 400;\">brackets<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Object\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">-&gt;<\/span><\/td>\n<td><span style=\"font-weight: 400;\">pointer<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">++\u00a0 &#8211; &#8211;<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Postfix increment, decrement<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">++\u00a0 &#8211; &#8211;<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Prefix increment\/decrement<\/span><\/td>\n<td rowspan=\"7\"><span style=\"font-weight: 400;\">right-to-left<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">+ &#8211;\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Unary plus\/minus<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">! ~<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Logical negation\/bitwise complement<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">(type)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Cast<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">*<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Dereference<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">&amp;<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Address (of operand)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">sizeof<\/span><\/td>\n<td><span style=\"font-weight: 400;\">return size in bytes\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">*\u00a0 \/\u00a0 %<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Multiplication\/division\/modulus<\/span><\/td>\n<td><span style=\"font-weight: 400;\">left-to-right<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">+\u00a0 &#8211;<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Addition\/subtraction<\/span><\/td>\n<td><span style=\"font-weight: 400;\">left-to-right<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">&lt;&lt;\u00a0 &gt;&gt;<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Bitwise shift left, Bitwise shift right<\/span><\/td>\n<td><span style=\"font-weight: 400;\">left-to-right<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">&lt;\u00a0 &lt;=<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Relational less than\/less than or equal to<\/span><\/td>\n<td rowspan=\"2\"><span style=\"font-weight: 400;\">left-to-right<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">&gt;\u00a0 &gt;=<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Relational greater than\/greater than or equal to<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">==\u00a0 !=<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Relational is equal to\/is not equal to<\/span><\/td>\n<td><span style=\"font-weight: 400;\">left-to-right<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">&amp;<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Bitwise AND<\/span><\/td>\n<td><span style=\"font-weight: 400;\">left-to-right<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">^<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Bitwise exclusive OR<\/span><\/td>\n<td><span style=\"font-weight: 400;\">left-to-right<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">|<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Bitwise inclusive OR<\/span><\/td>\n<td><span style=\"font-weight: 400;\">left-to-right<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">&amp;&amp;<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Logical AND<\/span><\/td>\n<td><span style=\"font-weight: 400;\">left-to-right<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">| |<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Logical OR<\/span><\/td>\n<td><span style=\"font-weight: 400;\">left-to-right<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">? :<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Ternary conditional<\/span><\/td>\n<td><span style=\"font-weight: 400;\">right-to-left<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">=<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Assignment<\/span><\/td>\n<td rowspan=\"6\"><span style=\"font-weight: 400;\">right-to-left<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">+=\u00a0 -=<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Addition\/subtraction assignment<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">*=\u00a0 \/=<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Multiplication\/division assignment<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">%=\u00a0 &amp;=<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Modulus\/bitwise AND assignment<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">^=\u00a0 |=<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Bitwise exclusive\/inclusive OR assignment<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">&lt;&lt;=\u00a0 &gt;&gt;=<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Bitwise shift left\/right assignment<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">,<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Comma (separate expressions)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">left-to-right<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Types of notations in Expression Parsing<\/h3>\n<h4>1. Infix notation in Expression Parsing<\/h4>\n<p><span style=\"font-weight: 400;\">The normal form of expression that we generally use is the infix notation. Operators in infix notation are used in between the operands. Infix notation is easy for humans but for computer systems, it might require more memory and time to solve.<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/p>\n<p><span style=\"font-weight: 400;\">For example:\u00a0 <\/span><span style=\"font-weight: 400;\">(a+b) * (c-d)<\/span><\/p>\n<h4>2. Prefix notation in Expression parsing<\/h4>\n<p><span style=\"font-weight: 400;\">In prefix notation, also known as polish notation, the operator is written before the operands.<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/p>\n<p><span style=\"font-weight: 400;\">For example: <\/span><span style=\"font-weight: 400;\">*+ab-cd<\/span><\/p>\n<h4>3. Postfix notation in Expression Parsing<\/h4>\n<p><span style=\"font-weight: 400;\">In prefix notation, also known as reverse polish notation, the operator is written after the operands.<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/p>\n<p><span style=\"font-weight: 400;\">For example:\u00a0 <\/span><span style=\"font-weight: 400;\">ab+cd-*<\/span><\/p>\n<h3>Expression Parsing<\/h3>\n<p><span style=\"font-weight: 400;\">Writing algorithms for parsing expressions written in infix notation is very tough and inefficient. For this reason, infix expressions are first converted to postfix or prefix notation and then evaluated.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Converting of expression is done on basis of associativity and order of precedence.<\/span><\/p>\n<p>For example:<\/p>\n<p><b>Convert the following infix to postfix:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">X+(R-Q)*S-Y\/Z\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Step 1: start from the expression inside the brackets<\/span><\/p>\n<p><span style=\"font-weight: 400;\">=X+(RQ-)*S-Y\/Z<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Step 2: scanning from left to right, convert the expression for highest precedence and associativity.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">=X+RQ-S*-YZ\/<\/span><\/p>\n<p><span style=\"font-weight: 400;\">=XRQ-S*+YZ\/-<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>Input symbol<\/b><\/td>\n<td><b>stack<\/b><\/td>\n<td><b>postfix<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">X<\/span><\/td>\n<td><span style=\"font-weight: 400;\">empty<\/span><\/td>\n<td><span style=\"font-weight: 400;\">X<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">+<\/span><\/td>\n<td><span style=\"font-weight: 400;\">+<\/span><\/td>\n<td><span style=\"font-weight: 400;\">X<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">(<\/span><\/td>\n<td><span style=\"font-weight: 400;\">+(<\/span><\/td>\n<td><span style=\"font-weight: 400;\">X<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">R<\/span><\/td>\n<td><span style=\"font-weight: 400;\">+(<\/span><\/td>\n<td><span style=\"font-weight: 400;\">XR<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">&#8211;<\/span><\/td>\n<td><span style=\"font-weight: 400;\">+(-<\/span><\/td>\n<td><span style=\"font-weight: 400;\">XR<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Q<\/span><\/td>\n<td><span style=\"font-weight: 400;\">+(-<\/span><\/td>\n<td><span style=\"font-weight: 400;\">XRQ<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">+<\/span><\/td>\n<td><span style=\"font-weight: 400;\">XRQ-<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">*<\/span><\/td>\n<td><span style=\"font-weight: 400;\">+*<\/span><\/td>\n<td><span style=\"font-weight: 400;\">XRQ-<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">S<\/span><\/td>\n<td><span style=\"font-weight: 400;\">+*<\/span><\/td>\n<td><span style=\"font-weight: 400;\">XRQ-S<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">&#8211;<\/span><\/td>\n<td><span style=\"font-weight: 400;\">&#8211;<\/span><\/td>\n<td><span style=\"font-weight: 400;\">XRQ-S*+<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Y<\/span><\/td>\n<td><span style=\"font-weight: 400;\">&#8211;<\/span><\/td>\n<td><span style=\"font-weight: 400;\">XRQ-S*+Y<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">\/<\/span><\/td>\n<td><span style=\"font-weight: 400;\">-\/<\/span><\/td>\n<td><span style=\"font-weight: 400;\">XRQ-S*+Y<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Z<\/span><\/td>\n<td><span style=\"font-weight: 400;\">-\/<\/span><\/td>\n<td><span style=\"font-weight: 400;\">XRQ-S*+YZ<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">null<\/span><\/td>\n<td><span style=\"font-weight: 400;\">EMPTY<\/span><\/td>\n<td><span style=\"font-weight: 400;\">XRQ-S*+YZ\/-<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><b>Convert the following infix to prefix:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">X+(R-Q)*S-Y\/Z\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">STEP 1: reverse the expression<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Z\/Y-S*)Q-R(+X<\/span><\/p>\n<p><span style=\"font-weight: 400;\">STEP 2:<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>Input Symbol<\/b><\/td>\n<td><b>stack<\/b><\/td>\n<td><b>prefix<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Z<\/span><\/td>\n<td><span style=\"font-weight: 400;\">empty<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Z<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">\/<\/span><\/td>\n<td><span style=\"font-weight: 400;\">\/<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Z<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Y<\/span><\/td>\n<td><span style=\"font-weight: 400;\">\/<\/span><\/td>\n<td><span style=\"font-weight: 400;\">ZY<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">&#8211;<\/span><\/td>\n<td><span style=\"font-weight: 400;\">&#8211;<\/span><\/td>\n<td><span style=\"font-weight: 400;\">ZY\/<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">S<\/span><\/td>\n<td><span style=\"font-weight: 400;\">&#8211;<\/span><\/td>\n<td><span style=\"font-weight: 400;\">ZY\/S<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">*<\/span><\/td>\n<td><span style=\"font-weight: 400;\">-*<\/span><\/td>\n<td><span style=\"font-weight: 400;\">ZY\/S<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">-*)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">ZY\/S<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Q<\/span><\/td>\n<td><span style=\"font-weight: 400;\">-*)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">ZY\/SQ<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">&#8211;<\/span><\/td>\n<td><span style=\"font-weight: 400;\">-*)-<\/span><\/td>\n<td><span style=\"font-weight: 400;\">ZY\/SQ<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">R<\/span><\/td>\n<td><span style=\"font-weight: 400;\">-*)-<\/span><\/td>\n<td><span style=\"font-weight: 400;\">ZY\/SQR<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">(<\/span><\/td>\n<td><span style=\"font-weight: 400;\">-*<\/span><\/td>\n<td><span style=\"font-weight: 400;\">ZY\/SQR-<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">+<\/span><\/td>\n<td><span style=\"font-weight: 400;\">-+<\/span><\/td>\n<td><span style=\"font-weight: 400;\">ZY\/SQR-*<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">X<\/span><\/td>\n<td><span style=\"font-weight: 400;\">-+<\/span><\/td>\n<td><span style=\"font-weight: 400;\">ZYSQR-*X<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">null<\/span><\/td>\n<td><span style=\"font-weight: 400;\">empty<\/span><\/td>\n<td><span style=\"font-weight: 400;\">ZYSQR-*X+-<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">Reverse the above result: -+X*-RQS\/YZ (final prefix expression)<\/span><\/p>\n<p><b>Postfix evaluation<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Evaluate:\u00a0 5 2 + 6 3 \/ * using stack<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>Input<\/b><\/td>\n<td><b>Current stack<\/b><\/td>\n<td><b>Current symbol<\/b><\/td>\n<td><b>Operation on stack<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">5 2 + 6 3 \/ *<\/span><\/td>\n<td><span style=\"font-weight: 400;\">empty<\/span><\/td>\n<td><span style=\"font-weight: 400;\">5<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Push 5<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">2 + 6 3 \/ *<\/span><\/td>\n<td><span style=\"font-weight: 400;\">5<\/span><\/td>\n<td><span style=\"font-weight: 400;\">2<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Push 2<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">+ 6 3 \/ *<\/span><\/td>\n<td><span style=\"font-weight: 400;\">2 5<\/span><\/td>\n<td><span style=\"font-weight: 400;\">+<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Pop 2 and 5, push 5+2<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">6 3 \/ *<\/span><\/td>\n<td><span style=\"font-weight: 400;\">7<\/span><\/td>\n<td><span style=\"font-weight: 400;\">6<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Push 6<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">3 \/ *<\/span><\/td>\n<td><span style=\"font-weight: 400;\">6 7<\/span><\/td>\n<td><span style=\"font-weight: 400;\">3<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Push 3<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">\/ *<\/span><\/td>\n<td><span style=\"font-weight: 400;\">3 6 7<\/span><\/td>\n<td><span style=\"font-weight: 400;\">\/<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Pop 3 and 6, push 6\/3<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">*<\/span><\/td>\n<td><span style=\"font-weight: 400;\">2 7<\/span><\/td>\n<td><span style=\"font-weight: 400;\">*<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Pop 2 and 7, push 2*7<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">The Final result of the above expression is 14.<\/span><\/p>\n<p><b>Algorithm of Expression Parsing<\/b><\/p>\n<p>Below are the steps to do Expression Parsing:<\/p>\n<p>1: start scanning one symbol at a time of the expression from left to right.<br \/>\n2: if the symbol is an operand, push it to stack<br \/>\n3: if the symbol is an operator, pop operand from the stack and perform the operation<br \/>\n4: push the result of step 3, into the stack<br \/>\n5: Repeat steps 1 to 4 until all operands are consumed<br \/>\n6: pop the stack and perform the operation<\/p>\n<h4>Postfix evaluation in C++<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;bits\/stdc++.h&gt;\r\nusing namespace std;\r\n \r\nint precedence_order(char op){\r\n    if(op == '+'||op == '-')\r\n    return 1;\r\n    if(op == '*'||op == '\/')\r\n    return 2;\r\n    return 0;\r\n}\r\n \r\nint operation(int a, int b, char op){\r\n    switch(op){\r\n        case '+': return a + b;\r\n        case '-': return a - b;\r\n        case '*': return a * b;\r\n        case '\/': return a \/ b;\r\n    }\r\n}\r\n \r\nint evaluate(string expression){\r\n    int i;\r\n     \r\n    stack &lt;int&gt; values;\r\n     \r\n    stack &lt;char&gt; ops;\r\n     \r\n    for(i = 0; i &lt; expression.length(); i++){\r\n        \r\n        if(expression[i] == ' ')\r\n            continue;\r\n         \r\n        else if(expression[i] == '('){\r\n            ops.push(expression[i]);\r\n        }\r\n         \r\n        else if(isdigit(expression[i])){\r\n            int val = 0;\r\n             \r\n\r\n            while(i &lt; expression.length() &amp;&amp;\r\n                        isdigit(expression[i]))\r\n            {\r\n                val = (val*10) + (expression[i]-'0');\r\n                i++;\r\n            }\r\n             \r\n            values.push(val);\r\n             \r\n              i--;\r\n        }\r\n         \r\n       \r\n        else if(expression[i] == ')')\r\n        {\r\n            while(!ops.empty() &amp;&amp; ops.top() != '(')\r\n            {\r\n                int val2 = values.top();\r\n                values.pop();\r\n                 \r\n                int val1 = values.top();\r\n                values.pop();\r\n                 \r\n                char op = ops.top();\r\n                ops.pop();\r\n                 \r\n                values.push(operation(val1, val2, op));\r\n            }\r\n             \r\n            \r\n            if(!ops.empty())\r\n               ops.pop();\r\n        }\r\n         \r\n        \r\n        else\r\n        {\r\n            \r\n            while(!ops.empty() &amp;&amp; precedence_order(ops.top())\r\n                                &gt;= precedence_order(expression[i])){\r\n                int val2 = values.top();\r\n                values.pop();\r\n                 \r\n                int val1 = values.top();\r\n                values.pop();\r\n                 \r\n                char op = ops.top();\r\n                ops.pop();\r\n                 \r\n                values.push(operation(val1, val2, op));\r\n            }\r\n             \r\n            \r\n            ops.push(expression[i]);\r\n        }\r\n    }\r\n     \r\n    \r\n    while(!ops.empty()){\r\n        int val2 = values.top();\r\n        values.pop();\r\n                 \r\n        int val1 = values.top();\r\n        values.pop();\r\n                 \r\n        char op = ops.top();\r\n        ops.pop();\r\n                 \r\n        values.push(operation(val1, val2, op));\r\n    }\r\n     \r\n    \r\n    return values.top();\r\n}\r\n \r\nint main() {\r\n    cout &lt;&lt; evaluate(\"34 + 13 * 93\") &lt;&lt; \"\\n\";\r\n    cout &lt;&lt; evaluate(\"85 \/ 23 - 16\") &lt;&lt; \"\\n\";\r\n    cout &lt;&lt; evaluate(\"100 * ( 2 + 12 )\") &lt;&lt; \"\\n\";\r\n    cout &lt;&lt; evaluate(\"121 * ( 5 - 1 ) \/ 11\");\r\n    return 0;\r\n}\r\n\r\n<\/pre>\n<h4>Expression Parsing Postfix evaluation in Python<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">def precedence_order(op):\r\n    \r\n    if op == '+' or op == '-':\r\n        return 1\r\n    if op == '*' or op == '\/':\r\n        return 2\r\n    return 0\r\n \r\ndef operation(a, b, op):\r\n     \r\n    if op == '+': return a + b\r\n    if op == '-': return a - b\r\n    if op == '*': return a * b\r\n    if op == '\/': return a \/\/ b\r\n \r\n\r\ndef evaluate(expression):\r\n     \r\n    values = []\r\n    ops = []\r\n    i = 0\r\n     \r\n    while i &lt; len(expression):\r\n         \r\n        if expression[i] == ' ':\r\n            i += 1\r\n            continue\r\n         \r\n        elif expression[i] == '(':\r\n            ops.append(expression[i])\r\n         \r\n        elif expression[i].isdigit():\r\n            val = 0\r\n             \r\n            while (i &lt; len(expression) and\r\n                expression[i].isdigit()):\r\n             \r\n                val = (val * 10) + int(expression[i])\r\n                i += 1\r\n             \r\n            values.append(val)\r\n             \r\n            i-=1\r\n        \r\n        elif expression[i] == ')':\r\n         \r\n            while len(ops) != 0 and ops[-1] != '(':\r\n             \r\n                val2 = values.pop()\r\n                val1 = values.pop()\r\n                op = ops.pop()\r\n                 \r\n                values.append(operation(val1, val2, op))\r\n            ops.pop()\r\n         \r\n        else:\r\n        \r\n            while (len(ops) != 0 and\r\n                precedence_order(ops[-1]) &gt;=\r\n                   precedence_order(expression[i])):\r\n                         \r\n                val2 = values.pop()\r\n                val1 = values.pop()\r\n                op = ops.pop()\r\n                 \r\n                values.append(operation(val1, val2, op))\r\n             \r\n            \r\n            ops.append(expression[i])\r\n         \r\n        i += 1\r\n     \r\n    while len(ops) != 0:\r\n         \r\n        val2 = values.pop()\r\n        val1 = values.pop()\r\n        op = ops.pop()\r\n                 \r\n        values.append(operation(val1, val2, op))\r\n     \r\n    return values[-1]\r\n \r\nif __name__ == \"__main__\":\r\n     \r\n    print(evaluate(\"34 + 13 * 93\"))\r\n    print(evaluate(\"85 \/ 23 - 16\"))\r\n    print(evaluate(\"100 * ( 2 + 12 )\"))\r\n    print(evaluate(\"121 * ( 5 - 1 ) \/ 11\"))\r\n\r\n\r\n\r\n<\/pre>\n<h3>Conclusion<\/h3>\n<p>Expression parsing is the method that a computer uses to solve an arithmetic expression, Unlike humans, a computer system has completely different approaches as we discussed in this article since infix expression is not understandable to the system.<\/p>\n<p>We also discussed different components of expression parsing, notations, associativity, and precedence. The precedence order is similar to Mathematical concert of BODMAS, just with a lot more symbols.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A+B-(C*D)\/E+X*Y- I am sure you must have seen this kind of expression at some point in your life. When you approach solving it, you assign the values to the variable and perform the operations&#46;&#46;&#46;<\/p>\n","protected":false},"author":7,"featured_media":96569,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24020],"tags":[24485,24487,24486,24490,24489,24488,24492,24491],"class_list":["post-96451","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-structure-tutorials","tag-expression-parsing","tag-expression-parsing-algorithm","tag-expression-parsing-in-data-structure","tag-infix-notation","tag-infix-to-postfix-conversion","tag-infix-to-prefix-conversion","tag-postfix-notation","tag-prefix-notation"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Expression Parsing in Data Structure - DataFlair<\/title>\n<meta name=\"description\" content=\"Data Structure - Expression Parsing - The way to write arithmetic expression is known as a notation. Learn more about expression parsing.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/expression-parsing-in-data-structure\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Expression Parsing in Data Structure - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Data Structure - Expression Parsing - The way to write arithmetic expression is known as a notation. Learn more about expression parsing.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/expression-parsing-in-data-structure\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-06-12T03:30:21+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Expression-parsing.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Expression Parsing in Data Structure - DataFlair","description":"Data Structure - Expression Parsing - The way to write arithmetic expression is known as a notation. Learn more about expression parsing.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/expression-parsing-in-data-structure\/","og_locale":"en_US","og_type":"article","og_title":"Expression Parsing in Data Structure - DataFlair","og_description":"Data Structure - Expression Parsing - The way to write arithmetic expression is known as a notation. Learn more about expression parsing.","og_url":"https:\/\/data-flair.training\/blogs\/expression-parsing-in-data-structure\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2021-06-12T03:30:21+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Expression-parsing.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/expression-parsing-in-data-structure\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/expression-parsing-in-data-structure\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/beb0cab24b7aa54423a3b50e669a9dcd"},"headline":"Expression Parsing in Data Structure","datePublished":"2021-06-12T03:30:21+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/expression-parsing-in-data-structure\/"},"wordCount":911,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/expression-parsing-in-data-structure\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Expression-parsing.jpg","keywords":["Expression Parsing","Expression Parsing Algorithm","Expression Parsing in Data Structure","Infix Notation","infix to postfix conversion","infix to prefix conversion","Postfix Notation","Prefix Notation"],"articleSection":["Data Structure Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/expression-parsing-in-data-structure\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/expression-parsing-in-data-structure\/","url":"https:\/\/data-flair.training\/blogs\/expression-parsing-in-data-structure\/","name":"Expression Parsing in Data Structure - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/expression-parsing-in-data-structure\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/expression-parsing-in-data-structure\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Expression-parsing.jpg","datePublished":"2021-06-12T03:30:21+00:00","description":"Data Structure - Expression Parsing - The way to write arithmetic expression is known as a notation. Learn more about expression parsing.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/expression-parsing-in-data-structure\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/expression-parsing-in-data-structure\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/expression-parsing-in-data-structure\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Expression-parsing.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Expression-parsing.jpg","width":1200,"height":628,"caption":"Expression parsing"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/expression-parsing-in-data-structure\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Data Structure Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/data-structure-tutorials\/"},{"@type":"ListItem","position":3,"name":"Expression Parsing in Data Structure"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/beb0cab24b7aa54423a3b50e669a9dcd","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c322416204232f4dd97ef3901b0a499a5d34d7ba7fe333f4bfe53a907873d293?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c322416204232f4dd97ef3901b0a499a5d34d7ba7fe333f4bfe53a907873d293?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c322416204232f4dd97ef3901b0a499a5d34d7ba7fe333f4bfe53a907873d293?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team specializes in creating clear, actionable content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Backed by industry expertise, we make learning easy and career-oriented for beginners and pros alike.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam3\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/96451","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=96451"}],"version-history":[{"count":4,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/96451\/revisions"}],"predecessor-version":[{"id":96570,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/96451\/revisions\/96570"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/96569"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=96451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=96451"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=96451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}