Shunting yard algorithm for immediate evaluation - algorithm

Generally, programs which evaluate an infix mathematical expression use some variation of the Shunting Yard Algorithm to first translate the expression into Reverse Polish Notation, and then evaluate the Reverse Polish Notation to get a single final value.
My question is, is there some well-known algorithm that bypasses the INFIX -> RPN step, and just evaluates the initial infix expression in place, using some kind of recursive descent parsing?
Presumably, it might be useful when writing compilers or parsers to translate INFIX -> RPN. The RPN is sort of a "compiled form" of the expression (an AST), which can be more easily evaluated by a computer using a simple output stack. But, if you're just simply writing code that immediately translates an infix expression into a numeric output value, you might not have any use for caching the intermediate RPN form.
So, is there any well-known algorithm for parsing infix expressions WITHOUT first converting to RPN? Or is converting to RPN generally more efficient than any other approach?

You can easily modify the shunting-yard algorithm to immediately evaluate the expression as you go rather than building up an RPN representation. Specifically, whenever you would normally pop off an operator and two operands from the stacks, rather than appending those tokens to the output, instead just evaluate the expression and push the result back onto the operand stack. Finally, at the very end, evaluate the expression implied by the operator and operand stacks by popping off two operands, popping an operator, computing the result, and pushing it back onto the stack.
For example, to evaluate 3 * 4 + 2, you'd do the following:
Process 3:
Operators
Operands 3
Process *:
Operators *
Operands 3
Process 4:
Operators *
Operands 3 4
Process +:
Operators +
Operands 12
Process 2:
Operators +
Operands 12 2
End of input:
Operators
Operands 14
Hope this helps!

Related

Double negation in Reverse Polish Notation

I'm writing a boolean Reverse Polish Notation parser and evaluator. When I want to evaluate a double-negation, like !!A, the corresponding RPN is !A!, according to the shunting-yard algorithm. However, when I try to run the evaluation algorithm from Wikipedia it fails, as, when the first ! is found, there is no value to apply the operator to, as expected.
However, if I write the expression as !(!A), it translates to A!! in RPN, which is what I would need.
Is it a problem with conversion to RPN, or a evaluation one? I could always fix it by enforcing parentheses with each negation, but that doesn't seem like an elegant solution...

Can the shunting-yard algorithm be used to parse expressions containing a mix of logical, comparison and arithmetic operators?

According to Wikipedia the shunting-yard algorithm is used to parse mathematical expressions. But is there any reason it could not be used with a mix of logical and arithmetic expressions, as well as comparisons?
As an example, could one use it to parse this:
a+b<17 && a+b>3 || a==b
As far as I can see, you could just define logical operators to have the lowest precedence, followed by comparison operators, and then have the usual arithmetic operators with increasing precedence. Or am I missing something?

How To Insert Parenthesis In An Infix Expression Using Stack

I need an algorithm to insert parenthesis in an infix expression using stack.
for example:
input : 12/c+c*p^(8+9)
output : ((12/c)+(c*(p^(8+9))))
I just need the algorithm for this and it is ok if we reach the output through prefix or postfix. but the output should be exact as the example. an infix expression with full parenthesis.
I'll appreciate if someone can give a pseudocode or step by step example.
Thanks
I have achieved this before by means of reverse polish notation (RPN). First you need to construct a postfix version of the expression. You could use the shunting-yard algorithm. Secondly, you need to "evaluate" (symbolically) the postfix expression you just created. The RPN evaluation algorithm does this.
1 - convert to postfix
2 - read the postfix expression
3 - a loop on postfix expression ... if character is operand push in stack else if operator ... pop top two in the stack and insert this two with the operator that is current character in the parentheses and push it in stack again

Conceptual issue occurred while converting infix notation to postfix notation

How can I realize/understand what is the preferential order/precedence of operators while converting infix notation to postfix notation :
" * ", " / ", " + ", " - ", " ^ ", " ) ", " ( ".
I understand that one can just look at an algorithm for this and solve this but I don't want to do that. What should be my thought process?
Operator precedence is a convention and property of a formal infix language to indicate which operations should evaluate first. A higher precedence operation means that it should operate before lower precedence operations.
Grouping parentheses are not part of the operator precedence. (Note that other kinds of parentheses such as function call parentheses may be however I am assuming you do not refer to those here) They are used to explicitly indicate the order of operations. Parentheses are only useful to indicate an order of operations in infix notation. The purpose of the operator precedence convention in a given language is to avoid using parentheses in most case. So, for example, if I want to multiply 4 by 5 and then add 7 to the result, I can write:
4*5+7
This is valid under normal arithmetic operator precedence rules because multiplication ('*') has higher precedence than addition ('+'). But if I want to add 3 and 4 and then multiply this result by 8, I need to write:
(3+4)*8
In this case I want the order of operations to be different than the normal order of "higher precedence operations first." In other words, parenthesis are only necessary when we are using infix notation and want operations to execute in an order other than precedence-order.
In standard arithmetic, exponentiation ("^") has highest precedence. Next is multiplication and division (equal precedence) and finally addition and subtraction. Therefore, an infix expression written using these operators without parenthesis will evaluate all exponentiation first, then all multiplications and divisions (in left to right order) and finally all additions and subtractions, again in left to right order.
If you want to infer the operator precedence of an unknown language, you would need to look at the places where parentheses are and are not used. Since it is valid to use parentheses everywhere even when unnecessary, this is only a heuristic. For the examples above, I can write:
((4*5)+7)
And this gives no hint about operator precedence. It is because every binary operator in this case has parentheses, and therefore at least one of the two sets is redundant assuming the precedence of addition and multiplication are not the same.
Similarly, looking at the next example:
(3+4)*8
since parentheses were used around the addition but not the multiplication, we can infer that probably in this language addition has lower precedence than multiplication. Otherwise, the parenthesis would be redundant. So look for the pattern where parentheses are and are not used to try to figure out operator precedence in unknown languages. It is more common to assume a certain precedence level based on the formal specification of the language under consideration. Most formal languages have an operator precedence chart for the infix form to avoid this ambiguity.
We never need parentheses in prefix or postfix languages because the order of terms and operators already makes the order of evaluation explicit. Therefore this issue is really an infix-language-specific problem.
If parentheses are balanced properly you can always find a parenthesis-free subexpression, which reduces the problem to that case.
Now just ask yourself, according to precedence rules, which operation in such an expression should be performed first?

Algorithm for testing for equivalence of two algebraic expressions

I have to write a program that tests whether two algebraic expressions are equivalent. It should follow MDAS precedence and parenthesis grouping. To solve the problem about precedence, I'm thinking I should implement a Infix to Postfix Notation converter for these expressions. But by doing this, I could not conclude their equivalence.
The program should look like this:
User Input: a*(a+b) = a*a + a*b
Output : Equivalent
For this problem I'm not allowed to use Computer Algebraic Systems or any external libraries. Please don't post the actual code if you have one, I just need an idea to work this problem out.
If you are not allowed to evaluate the expressions, you will have to parse them out into expression trees.
After that, I would get rid of all parenthesis by multiplying/dividing all members so a(b - c) becomes a*b - a*c.
Then convert all expressions back to strings, making sure you have all members alphabetically sorted (a*b, not b*a) ,remove all spaces and compare strings.
That's an idea:
You need to implement building expression tree first because it's a very natural representation of expression.
Then maybe you'll need to simplify it by open brackets and etc. using associative or distributive algebraic properties.
Then you'll have to compare trees. It's not obvious because you need to take care of all branch permutations in commutative operations and etc. E.g. you can sort them (I mean branches) and then compare for equality. Also you need to keep in mind possible renaming of parameters, i.e. a + b need to be equal x + y.

Resources