shunting-yard

Apply distributive law on AST (or RPN) => disjunctive normal form

可紊 提交于 2021-01-27 20:32:52
问题 I have expressions like the following: {1000} AND ({1001} OR {1002} OR {1003}) Allowed operators are OR and AND, expressions can be nested using parenthesis. I already managed to tokenize this string and to convert it to an abstract syntax tree (AST) using the Shunting Yard algorithm, implemented in PHP 5.3. The above expression results in the following: 1000 1001 1002 | 1003 | & & / \ 1000 | / \ | 1003 / \ 1001 1002 When traversing this tree I want to output the final combinations of numbers

Shunting Yard Algorithm with Variables

▼魔方 西西 提交于 2020-08-04 18:24:08
问题 I'm currently working on a modified version of the Shunting Yard Algorithm that would work with variables, but I cant figure out how to get it to work. For example, I would want the algorithm to re-write 2 * (2x + 5) - 5 to 4x + 5. Any ideas / links to already implemented algorithms that does this already? 回答1: Take the expression: 2 * (2x + 5) - 5 Add the * symbol to make it more understandable for the computer: 2 * (2*x + 5) - 5 Parse it using the Shunting Yard Algorithm, it becomes: 2 2 x

Shunting Yard Algorithm with unnecessary parentheses

独自空忆成欢 提交于 2020-01-15 06:59:47
问题 Input (in javascript) is "3-2+(8-3)" I want to translate this expression to Reverse Polish Notation. However, according to the algorithm, I can get "3 2 8 3 - + -", which doesn't evaluate to the result 12..... Any work around method for this? I know the parentheses are unnecessary here, but, oh well...I have my function below: function ShuntingYard(str){ str=str.replace(/\)\(/g, ")*("); var arr=str.split(""); var sYqueue=[]; var sYstack=[]; while (arr.length>0){ var token=arr.shift(); if (/\d

Shunting Yard Algorithm with unnecessary parentheses

断了今生、忘了曾经 提交于 2020-01-15 06:59:01
问题 Input (in javascript) is "3-2+(8-3)" I want to translate this expression to Reverse Polish Notation. However, according to the algorithm, I can get "3 2 8 3 - + -", which doesn't evaluate to the result 12..... Any work around method for this? I know the parentheses are unnecessary here, but, oh well...I have my function below: function ShuntingYard(str){ str=str.replace(/\)\(/g, ")*("); var arr=str.split(""); var sYqueue=[]; var sYstack=[]; while (arr.length>0){ var token=arr.shift(); if (/\d

Parse Math Expression in PHP

萝らか妹 提交于 2020-01-01 19:56:17
问题 I'm currently trying to parse math expression into expression tree. But I'm stuck on the stage where I need to implement functions and negates. I don't understand logic to do it using Shunting-Yard algorithm. What I currently want to do is to support Negates, like -(x+5) Function calls, like min(x,y) Power just after function name, like cos^2(x) Implicit multiplication, like 2x is same as 2*x Scientific notation Constants e and pi Can somebody tell me hints how to implement this? 回答1: An

Parse Math Expression in PHP

喜夏-厌秋 提交于 2020-01-01 19:55:18
问题 I'm currently trying to parse math expression into expression tree. But I'm stuck on the stage where I need to implement functions and negates. I don't understand logic to do it using Shunting-Yard algorithm. What I currently want to do is to support Negates, like -(x+5) Function calls, like min(x,y) Power just after function name, like cos^2(x) Implicit multiplication, like 2x is same as 2*x Scientific notation Constants e and pi Can somebody tell me hints how to implement this? 回答1: An

Shunting-yard algorithm in c++

╄→гoц情女王★ 提交于 2019-12-22 10:19:17
问题 I need a function that takes an infix string (like "3 + 4 * 9"), and convert it to postfix (like "4 9 * 3 +"). I got it working until you throw in parentheses within parentheses. I've been working on it all day and can't figure out what I'm doing wrong- can someone with a fresh mind see it, maybe? I feel like I'm really close! Thanks! Here's the code: string ExpressionManager::infixToPostfix(string infixExpression) { cout << "itop Testing : " << infixExpression << endl; string posnums =

Arithmetic Expression Evaluation using Reverse Polish Notation (RPN)

╄→尐↘猪︶ㄣ 提交于 2019-12-22 09:19:08
问题 A mathematical expression is usually expressed in infix notation. For evaluation purposes, we can change it to postfix (reverse polish) notation (using algorithms like Shunting-Yard) and then evaluate the postfix notation using stack. I found out that calculators use this technique, but do today's modern compilers use this for arithmetic expression evaluation? Is it efficient enough or other techniques (or algorithms) are being used? 回答1: To answer this question let's focus on the concepts