Reputation: 2399
I need to implement an expression evaluator with some custom types, it is easy to evaluate expressions with only numbers or variables with the help of postfix (or prefix) notation but how to extend the postfix (or prefix) form to support functions with arbitrary parameters?
Infix | postfix |
---|---|
(3+2)*2 | 32+2* |
funone(funtwo(3), 2) * 2 | ?????? |
How to convert the expression with function calls to postfix and evaluate it there are any standard algorithms for it?
I have looked at expr-eval-web and expr-eval-github the library saying it is modified version of Raphael Graf’s ActionScript Expression Parser and provided a link for it but i think it is not valid anymore. where can i find algorithm for Raphael Graf’s ActionScript Expression Parser ?
Upvotes: 0
Views: 119
Reputation: 111466
It is not clear from your question if (1) you have a postfix (or prefix) notation interpreter that you use now to evaluate code like 3 2 + 2 *
(and presumably (* (+ 3 2) 2)
) and you need information on how the function call would look like in postfix (or prefix) notation to then update your interpreter to support function calls, or (2) you are asking how to actually programmatically parse code in some language for which funone(funtwo(3), 2) * 2
is valid (many languages can look like this).
If (1) then here are the examples:
For this:
funone(funtwo(3), 2) * 2
The postfix notation would be:
3 funtwo 2 2 funone 2 *
The prefix notation would be:
(* (funone (funtwo 3) 2) 2)
If (2) then there is no general ready algorithm that will look for your language out of the box (unless this is a well-known language, but you didn't say it in your question).
Generally if you need to write a parser then either:
Here is a comparison of parser generators. They vary so widely that it is really impossible to recommend anything specific knowing so little about your requirements, but maybe reading about them can get you started:
Upvotes: 0