postfix-notation

Any reason I couldn't create a language supporting infix, postfix, and prefix functions, and more?

北城余情 提交于 2019-12-03 08:06:45
I've been mulling over creating a language that would be extremely well suited to creation of DSLs, by allowing definitions of functions that are infix, postfix, prefix, or even consist of multiple words. For example, you could define an infix multiplication operator as follows (where multiply(X,Y) is already defined): a * b => multiply(a,b) Or a postfix "squared" operator: a squared => a * a Or a C or Java-style ternary operator, which involves two keywords interspersed with variables: a ? b : c => if a==true then b else c Clearly there is plenty of scope for ambiguities in such a language,

Trouble understanding what to do with output of shunting-yard algorithm

蹲街弑〆低调 提交于 2019-12-03 00:39:21
I've been looking at the wiki page: http://en.wikipedia.org/wiki/Shunting-yard_algorithm I've used the code example to build the first part, basically I can currently turn : 3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3 into 3 4 2 * 1 5 − 2 3 ^ ^ / + But I don't know how to then use 3 4 2 * 1 5 − 2 3 ^ ^ / + to obtain 3.00012207 And the example code and explanation on wiki aren't making any sense to me. Could someone please explain how to evaluate 3 4 2 * 1 5 − 2 3 ^ ^ / + and produce the answer. Thanks in advance. I don't need a code example just a good explanation or a breakdown of an example. Not that it

need to change regex for additional postfix criteria [duplicate]

好久不见. 提交于 2019-12-02 18:09:58
问题 This question already has answers here : Pattern is not splitting as desired, fails to split by + (2 answers) Closed 4 years ago . So i have this code: Pattern pattern = Pattern.compile("\\d*(\\s\\d+\\.)*\\s*[-\\+\\*/\\$£]"); String input = "4.0 5.0 2.0 / + 7.0 - 11.0 34.0 2.0 / 3.0 / 1.0 * +"; Matcher matcher = pattern.matcher(input); List<String> output = new ArrayList<>(); while (matcher.find()) { output.add(matcher.group()); } When i was working on just parsing integers the regex was of

Postfix to Infix conversation [closed]

折月煮酒 提交于 2019-12-02 04:08:37
I can not solve this expression from postfix to infix. Please help me to understand in detail 5 x y - / x y + 3 ^ 7 / + postfix to infix: 5 x y - / x y + 3 ^ 7 / + STEP 5 x y - / A) 5xy-/ = 5 (x-y)/ = (5 / (x-y)) x y + B) x y + = (x + y) (x+y) 3 ^ B.1) (x + y) 3 ^ = ((x + y) ^ 3 ) Now, (5 / (x-y)) ((x + y) ^ 3 ) 7 / + = (5 / (x-y)) (((x + y) ^ 3 )/7 ) + = (5 / (x-y)) + (((x + y) ^ 3 )/7 ) POSTFIX and PREFIX are expression in which no brackets are used. Precedence of operator are decided in order of there appearance in expression, So to evaluate expression no need to search next operation to

Is Left associativity only for Postfix expression?

和自甴很熟 提交于 2019-12-01 21:57:53
问题 In case of evaluation of postfix expression , is the associativity always left to right?. If yes, why? If no, why? 回答1: Yes. See this handy table of C's operators. The postfix operators are: ++ and -- () (function call) [] (array indexing) . (member select) -> (member select) And they are all in the left-to-right precedence group (2) near the top of the table. I guess the "why" is since that's how the language is defined. There are no "natural causes" for these kinds of things, they're human

Is Left associativity only for Postfix expression?

送分小仙女□ 提交于 2019-12-01 21:30:32
In case of evaluation of postfix expression , is the associativity always left to right?. If yes, why? If no, why? Yes. See this handy table of C's operators . The postfix operators are: ++ and -- () (function call) [] (array indexing) . (member select) -> (member select) And they are all in the left-to-right precedence group (2) near the top of the table. I guess the "why" is since that's how the language is defined. There are no "natural causes" for these kinds of things, they're human-made you know. 来源: https://stackoverflow.com/questions/21407061/is-left-associativity-only-for-postfix

`defined?` and `unless` not working as expected

懵懂的女人 提交于 2019-12-01 15:36:35
I was expecting the following snippet: var = "Not Empty" unless defined? var var # => nil to return "Not Empty" , but I got nil . Any insight into why this is happening? This is one of the only moments in Ruby I would call actual WTFs. You have to use unless defined? var var = :value end With the postfix syntax, the interpreter will internally nil -ify the value so it can reason about the variable, thus making it defined before the check is done: # Doesn't print anything unless defined?(foo) and (p(foo) or true) foo = :value end # Prints nil bar = :value unless defined?(bar) and (p(bar) or

Algorithm to evaluate prefix expression?

让人想犯罪 __ 提交于 2019-12-01 14:39:16
I have a prefix expression that only has the 4 binary operators(+,-,*,/) .A straight forward way to evaluate such an expression is to convert it to a postfix expression and then evaluate that expression. But I am looking for an algorithm that does this directly without converting it to any other expression ? Simple recursion: Evaluate(input): Read a token from input. If the token is a value: Return the value of the token If the token is a binary operator: Let first_argument = Evaluate(input) Let second_argument = Evaluate(input) Return apply(operator, first_argument, second_argument) Use a

Simplification Algorithm for Reverse Polish Notation

≡放荡痞女 提交于 2019-12-01 12:27:19
A couple of days ago I played around with Befunge which is an esoteric programming language. Befunge uses a LIFO stack to store data. When you write programs the digits from 0 to 9 are actually Befunge-instructions which push the corresponding values onto the stack. So for exmaple this would push a 7 to stack: 34+ In order to push a number greater than 9, calculations must be done with numbers less than or equal to 9. This would yield 123. 99*76*+ While solving Euler Problem 1 with Befunge I had to push the fairly large number 999 to the stack. Here I began to wonder how I could accomplish

Algorithm to evaluate prefix expression?

牧云@^-^@ 提交于 2019-12-01 12:13:17
问题 I have a prefix expression that only has the 4 binary operators(+,-,*,/) .A straight forward way to evaluate such an expression is to convert it to a postfix expression and then evaluate that expression. But I am looking for an algorithm that does this directly without converting it to any other expression ? 回答1: Simple recursion: Evaluate(input): Read a token from input. If the token is a value: Return the value of the token If the token is a binary operator: Let first_argument = Evaluate