shunting-yard

Problems with a shunting yard algorithm

蓝咒 提交于 2019-11-28 10:21:19
I have successfully implemented a shunting yard algorithm in java. The algorithm itself was simple however I am having trouble with the tokenizer. Currently the algorithm works with everything I want excluding one thing. How can I tell the difference between subtraction(-) and negative (-) such as 4-3 is subtraction but -4+3 is negative I now know how to find out when it should be a negative and when it should be a minus, but where in the algorithm should it be placed because if you use it like a function it wont always work for example 3 + 4 * 2 / -( 1 − 5 ) ^ 2 ^ 3 when 1-5 becomes -4 it

handling unary minus for shunting-yard algorithm

时光毁灭记忆、已成空白 提交于 2019-11-28 00:22:13
问题 Is there a better way to handle unary "-" in converting a infix expression to a postfix one? The obvious one would be prefix every unary "-" with a 0. Does anyone know better implementation? Thanks! 回答1: The way I did this years ago was invent a new operator for my postfix expression. So when I encountered a unary minus in the infix, I'd convert it to # . So my postfix for a + -b became ab#+ . And, of course, my evaluator had to know that # only popped one operand. Kind of depends on how you

Shunting-Yard Validate Expression

不想你离开。 提交于 2019-11-27 16:14:46
We use the Shunting-Yard algorithm to evaluate expressions. We can validate the expression by simply applying the algorithm. It fails if there are missing operands, miss-matched parenthesis, and other things. The Shunting-Yard algorithm however has a larger supported syntax than just human readable infix. For example, 1 + 2 + 1 2 1 2 + are all acceptable ways to provide '1+2' as input to the Shunting-Yard algorithm. '+ 1 2' and '1 2 +' are not valid infix, but the standard Shunting-Yard algorithm can handle them. The algorithm does not really care about the order, it applies operators by order

Shunting Yard implementation in PHP needed, interpret and parse a string perform a mathematical comparison and return a boolean result

浪子不回头ぞ 提交于 2019-11-27 08:07:53
问题 I'm looking for something that can interpret a string in php and perform simple math calculation, and then return a boolean result as to whether the expression is true or false. For example: Sue types in "3*{mysalary}/9=10000" PHP splits this up into two expressions - explode('=',string); PHP takes my list of database fields, and replaces any "{}" delimited fields with the data (typecasted to int) PHP then evaluates the maths expression php then compares the left side to the right side

Handling extra operators in Shunting-yard

落爺英雄遲暮 提交于 2019-11-27 06:21:55
问题 Given an input like this: 3+4+ Algorithm turns it in to 3 4 + + I can find the error when it's time to execute the postfix expression. But, is it possible to spot this during the conversion? (Wikipedia article and internet articles I've read do not handle this case) Thank you 回答1: Valid expressions can be validated with a regular expression, aside from parenthesis mismatching. (Mismatched parentheses will be caught by the shunting-yard algorithm as indicated in the wikipedia page, so I'm

Problems with a shunting yard algorithm

白昼怎懂夜的黑 提交于 2019-11-27 03:34:02
问题 I have successfully implemented a shunting yard algorithm in java. The algorithm itself was simple however I am having trouble with the tokenizer. Currently the algorithm works with everything I want excluding one thing. How can I tell the difference between subtraction(-) and negative (-) such as 4-3 is subtraction but -4+3 is negative I now know how to find out when it should be a negative and when it should be a minus, but where in the algorithm should it be placed because if you use it