I had the same question as the student in the video about the left bracket which goes on the stack, but I think I understand now; basically the left bracket 'has a precedence' which other operators compare their own precedence against, but the left bracket ignores its own precedence (i.e. never compares its own precedence against any of the operators (even though it could)) and just goes to sit on top of the stack. Thanks for your great example, with an assignment and other tricky bits! It's really helped me.
@kiran, your thinking is correct, because they are the same precedence, need to remove from stack (doesn't really matter with + on + or - on -) but is for + on - or - on +
Trigonometric functions, or functions in general, go above exponents. For example: 2 + 3^4 * cos(0) Steps: 1. 2 is an operand; Add 2 to the output expression 2. + is an operator; Stack = empty; push + onto the stack 3. 3 is an operand; Add 3 to the output expression 4. ^ is an operator; Top of stack has lower precedence; push ^ onto stack 5. 4 is an operand; Add 4 to the output expression 6. * is an operator; Top of stack has higher precedence; pop stack until lower precedence is found; push * onto stack 7. cos is treated as an operator; top of stack has lower precedence; push cos onto stack 8. "(" = push onto stack regardless of lower or higher precedence or stack is empty 9. 0 is an operand; Add 0 to output expression 10. ")" = Pop and add to output expression until "(" = top of stack; pop top of stack 11. Since ")" is the end of the expression, pop the stack until stack = empty Result: 2 3 4 ^ 0 cos * +
XXxlightmarex just keep applying the rules. every left bracket goes on top of stack. when you get a right bracket remove contents of stack untill you find a left bracket, then discard that snd continue to parse the expression
work through remember operands just get written down as rpn (don't involve the stack at all) ((a + b * c) / (d + e * (f - g ^ h))) reaching first right bracket stack contains * + ( ( remove up to topmost left bracket and discard giving: abc*+ stack only contains ( carrying on until we reach 2nd right bracket, stack should contain: ^ - ( * + ( / ( dump stack upto topmost left bracket and discard giving us this much of the rpn: abc*+defgh^- stack now contains * + ( / ( parse 3rd right bracket so remove stack contents again upto topmost left bracket giving rpn of: abc*+defgh^-*+ stack contains / ( process final right bracket by removing upto topmost left bracket giving rpn of: abc*+defgh^-*+/ stack now empty hope this helps
WooHoooP Don't understand your example that would come from the infix (p - q) / (r - s) But to answer your question: if you have an expression of this form a - - b the minus in front of the b is actually unary minus (negation, applied to one operand) we would re-write that expression like this a - ~ b (using tilda to represent the unary minus) this would generate as unary minus is higher precedence that + or minus. a b ~ - which would negate the b before attempting to subtract it from a, which is logical in this example The precedence of unary minus is open to debate in some environments its placed higher than exponent (raising to the power ^) but others may place it below this but above multiply and divide. ~ b ^ a if unary minus was higher we would get b ~ a ^ which would negate b then raise to the power of a, but some would argue that we should have exponent higher than unary minus meaning this b a ^ ~ which would raise the b to the power of a and negate the result
Left brackets are always going to override precedence, it goes on top of everything, it's also there as a marker for when we encounter a right bracket while parsing, so we know when to stop pulling operators off the stack
The diagram shows that all operators go on top of left bracket if it seen on top of the stack. Parsing a left bracket in the expression doesn't look at precedence as you automatically push it onto the stack
Another burgermate you can only place an operator on top if it is higher so remove the item on top of stack snd write it down in the rp expression, then see if the new operator can now go on the stack (like normal)
+Jatin Rajput To be fair nothing makes up for bunk'd classes, it's amazing the nuances and added extra's you'll get when someone goes through a topic with you. I always find that I talk about things I never planned.
You can only push onto the stack if the precedence is higher, if it is the same you must pop the stack until the operator is higher than the contents of the top