Expression interpreter??
Hi!
Does anybody know a control or dll or function that interprets an expression like "x*sin(x)+x^3" with a given value for x ??
I tried looking for it in VB and C++, but I can''t seem to find such an evaluation function.
Can someone help?
Edo
Edo
[deleted because i apparently just don't understand the question
]
-me
[edited by - Palidine on April 25, 2002 6:59:44 PM]

-me
[edited by - Palidine on April 25, 2002 6:59:44 PM]
1) Google it (if you haven''t already). I''m sure there will be many evaluators around - if you look hard enough.
2) Try searching for things like Reverse Polish Notation. I''ve seen (and used) a site that explains how to parse an expression to turn it into reverse polish - but I can''t remember the address. Anyway, once you get it in reverse polish notation (for this operation, it''d end up as something like: "x,x,sin,*,x,3,^,+ ")
it becomes very easy and fast to write a stack based evaluator.
(you could probably write a stack based evaluator to deal with normal equations quite easily - but it''s up to you)
Good luck with it,
John B
2) Try searching for things like Reverse Polish Notation. I''ve seen (and used) a site that explains how to parse an expression to turn it into reverse polish - but I can''t remember the address. Anyway, once you get it in reverse polish notation (for this operation, it''d end up as something like: "x,x,sin,*,x,3,^,+ ")
it becomes very easy and fast to write a stack based evaluator.
(you could probably write a stack based evaluator to deal with normal equations quite easily - but it''s up to you)
Good luck with it,
John B
The best thing about the internet is the way people with no experience or qualifications can pretend to be completely superior to other people who have no experience or qualifications.
JohnBSmall describes the most common approach, and I'll expand on it a bit.
We have an expression parser that does exactly this, but unfortunately can't give it away.
You can create one for yourself using a lexical analyzer generator (e.g., GNU Flex, or Lex) and parser generator (e.g., yacc or GNU Bison). These tools take several input files, and generate C code for you that will parse an expression and output, possibly, a sequence of commands to be executed. You may be able to find input files for equation evalution using lex/yacc-generated code on the web. As another poster suggested, try google.
For simplistic equations, you could write your own parser without having to bother with lex and yacc. They generate ugly, possibly nonportable code anyway.
For example, the expression you gave,
could be parsed to yield this sequence of commands:
This is exactly what our parser and evaluator does, it just happens to be copyrighted code. Hope the description helps!
Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
[edited by - grhodes_at_work on April 25, 2002 6:40:51 PM]
We have an expression parser that does exactly this, but unfortunately can't give it away.
You can create one for yourself using a lexical analyzer generator (e.g., GNU Flex, or Lex) and parser generator (e.g., yacc or GNU Bison). These tools take several input files, and generate C code for you that will parse an expression and output, possibly, a sequence of commands to be executed. You may be able to find input files for equation evalution using lex/yacc-generated code on the web. As another poster suggested, try google.
For simplistic equations, you could write your own parser without having to bother with lex and yacc. They generate ugly, possibly nonportable code anyway.
For example, the expression you gave,
x*sin(x)+x^3
could be parsed to yield this sequence of commands:
// push X onto a stack 1 timePUSH X// pop X off the stack, raise to power 3, then push result// back onto the stackPOW 3// Push X onto the stackPUSH X// pops X off the stack, calcs the sin, and pushes result// back onto the stackSIN// You know, PUSH X// pop the top two items off the stack (X and sin(x)), multiply // them and push the result back on the stack (x*sin(x)).MUL// pop the top two items off the stack (x^3 and x*sin(x)), then// push the result back onto the stackADD// pop the result, and return this as the result of the // evaluationPOP
This is exactly what our parser and evaluator does, it just happens to be copyrighted code. Hope the description helps!
Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
[edited by - grhodes_at_work on April 25, 2002 6:40:51 PM]
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
The easiest would most likely be to embed a scripting language like Python. I think you can also do VB with Windows Scripting or something like that. VB I believe has a command to execute a string. That is rather trivial for an interpreter to support since it is already doing it with the program.
PS: If you used the command in VB you would have to assign the values to x before executing it and the statement executed would need to be an assignment statement assuming you actually wanted the value back.
[edited by - LilBudyWizer on April 25, 2002 6:56:43 PM]
PS: If you used the command in VB you would have to assign the values to x before executing it and the statement executed would need to be an assignment statement assuming you actually wanted the value back.
[edited by - LilBudyWizer on April 25, 2002 6:56:43 PM]
Keys to success: Ability, ambition and opportunity.
April 25, 2002 05:58 PM
look on flipcode code of day. it has expression compiler exactly like you want
quote:
Original post by LilBudyWizer
The easiest would most likely be to embed a scripting language like Python. I think you can also do VB with Windows Scripting or something like that. VB I believe has a command to execute a string. That is rather trivial for an interpreter to support since it is already doing it with the program.
PS: If you used the command in VB you would have to assign the values to x before executing it and the statement executed would need to be an assignment statement assuming you actually wanted the value back.
[edited by - LilBudyWizer on April 25, 2002 6:56:43 PM]
You are making the assumption that many people still make: That VB is an interpreted language.
I will tell you this is completely false now. VisualBasic 6 has a fairly decent optimizing compiler. Yes, you can compile to P-Code, but that is still an intermediate compiled language. VB code itself is never interpreted in its raw form anymore (even in the IDE VisualBasic uses dynamic compilation to P-Code).
And VB has a function that will execute a function or method by name, but it has no function to execute a line of code. If you want to execute VB code, the best alternative is Windows Scripting Host. But you must remember that this uses VBScript code, which IS NOT the same as VisualBasic.
The Windows Scripting Host would work well for expression interpreting, becuase you could allow your user to define his or her own functions. I used it for a simple function-graphing program once for this reason. Sure, it isn't the fastest script interpreter, but it is very versatile. Your code can even generate more code, which can be very powerful in some cases.
Of course, VB7 is another beast all together. As with all .NET languages, you could actually compile VB7 code programatically using the compiler objects. So you could take your mathematical expression, wrap it in a class, compile it into memory (converting it to MSIL code) and execute it. Don't ask me how, though, as I haven't looked at that part of the framework yet.
And I once wrote a simple infix expression evaluator that basically expands the implied parenthesis of the function, and then uses the parenthesis to evaluate it. However, it does not work with functions...just binary operators.
--TheMuuj
[edited by - TheMuuj on April 25, 2002 7:22:31 PM]
--TheMuuj
It isn''t so much that I assume it is interpreted as it is that I know it was once an interpreted language and assume backwards compatibility ties it to those roots. Microsoft being Microsoft though that is a mistaken assumption. I tend to forget that they are in the position to say too bad your programs are not going to work anymore. So did it ever support executing a string? Not all interpreters do.
Keys to success: Ability, ambition and opportunity.
Dear all,
I am a newbie of GL4java.
I wrote a opengl program in java. When I compile it
I encounter a kind od problem which is
"can resolve the symbol gl"
I have downloaded GL4java and installed it in my
computer. I think I did not set up the path or some
parameter well.
Pls indiate me! many thanks.
George
ee
I am a newbie of GL4java.
I wrote a opengl program in java. When I compile it
I encounter a kind od problem which is
"can resolve the symbol gl"
I have downloaded GL4java and installed it in my
computer. I think I did not set up the path or some
parameter well.
Pls indiate me! many thanks.
George
ee
ee
quote:
Original post by TheMuuj
You are making the assumption that many people still make: That VB is an interpreted language.
You are making the assumption he''s talking about VB6. He''s not. He is talking about VBScript (which can be got from the fact that he says "with Windows Scripting", which is interpreted, and available through the Windows Scripting Host.
VBScript has a method,
Exec
, to which you pass a string of valid VBScript code and it parses and executes it. codeka.com - Just click it.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement