Author |
Message |
Ben Crai #1 / 13
|
 Parsing Equations in VB
I would like to develop a VB application that takes a mathematical equation, input by the user, and evaluates it. This requires writing a "parser" -- code that takes a string input, which the user puts into a text box as a string, something like sqrt(sin(x)*x^2/tan(x)), or whatever -- and "parses" it into its constituent components, which are then read into a module or procedure which evaluates the equation, returning the value of that equation for any particular value of x. This is done routinely in programs like Mathematica, Maple, Matlab, etc., which take strings typed in by the user and parses them into functions that can be evaluated. This is really a daunting task, since the equation can be quite arbitrary, i.e., consiting of almost any combination of the standard arithmetic operatios (+,-,*,^,/) and the standard functions (trig, exponential, polynomials,radicals, etc.), embedded within any number of parentheses. (Of course, in practice, some limitation on the input could be imposed). I'm interested in learning how this parsing is done. This is not a programming question about VB per se, but I want to implement it in VB. (To be precise, I want to develop a VB application that solves any arbitrary differential equation input by the user.) So my query is really directed at the programming community per se, since I want to understand how this is done, conceptually. Then I can implement it in VB. If anyone can direct me to general info on the subject, I would greatly appreciate the advice. Better yet, does anyone know where it has been done in VB specifically? I guess it's too much to expect a 3rd party to have developed some tool to do this, but ...
|
Mon, 29 Jan 2001 03:00:00 GMT |
|
 |
Charles E. Bortle, Jr #2 / 13
|
 Parsing Equations in VB
Quote: >I would like to develop a VB application that takes a mathematical >equation, input by the user, and evaluates it. This requires writing a >"parser" -- code that takes a string input, which the user puts into a >text box as a string, something like sqrt(sin(x)*x^2/tan(x)), or >whatever -- and "parses" it into its constituent components, which are >then read into a module or procedure which evaluates the equation, >returning the value of that equation for any particular value of x. >This is done routinely in programs like Mathematica, Maple, Matlab, >etc., which take strings typed in by the user and parses them into >functions that can be evaluated. This is really a daunting task, since >the equation can be quite arbitrary, i.e., consiting of almost any >combination of the standard arithmetic operatios (+,-,*,^,/) and the >standard functions (trig, exponential, polynomials,radicals, etc.), >embedded within any number of parentheses. (Of course, in practice, >some limitation on the input could be imposed). I'm interested in >learning how this parsing is done. This is not a programming question >about VB per se, but I want to implement it in VB. (To be precise, I >want to develop a VB application that solves any arbitrary differential >equation input by the user.) So my query is really directed at the >programming community per se, since I want to understand how this is >done, conceptually. Then I can implement it in VB. If anyone can >direct me to general info on the subject, I would greatly appreciate the >advice. Better yet, does anyone know where it has been done in VB >specifically? I guess it's too much to expect a 3rd party to have >developed some tool to do this, but ...
Hello Ben, As a *very* general bit of info: what you need to do is look up the Bauer-Samelson algorithm. Texts on compilers or programming techniques will discuss this. One source is: PROGRAMMING AN INTRODUCTION TO COMPUTER TECHNIQUES, by Ward Douglas Maurer, Holden-Day, 1972, pp. 274. (This is an older book which I had close at hand.) You will use two stacks, one for operators, one for operands, and, of course, the input string (the formula to evaluate). Hope this helps, Charles
"For God So Loved The World, That He Gave His Only Begotten Son, That Whosoever Believeth In Him Should Not Perish, But Have Everlasting Life"John3:16 * http://pw2.netcom.com/~cbrtjr/wrdthing.html *
|
Tue, 30 Jan 2001 03:00:00 GMT |
|
 |
Paul Stro #3 / 13
|
 Parsing Equations in VB
: I would like to develop a VB application that takes a mathematical : equation, input by the user, and evaluates it. This requires writing a : "parser" -- code that takes a string input, which the user puts into a : text box as a string, something like sqrt(sin(x)*x^2/tan(x)), or : whatever -- and "parses" it into its constituent components, which are : then read into a module or procedure which evaluates the equation, : returning the value of that equation for any particular value of x. : This is done routinely in programs like Mathematica, Maple, Matlab, : etc., which take strings typed in by the user and parses them into : functions that can be evaluated. This is really a daunting task, since : the equation can be quite arbitrary, i.e., consiting of almost any : combination of the standard arithmetic operatios (+,-,*,^,/) and the : standard functions (trig, exponential, polynomials,radicals, etc.), : embedded within any number of parentheses. (Of course, in practice, : some limitation on the input could be imposed). I'm interested in : learning how this parsing is done. This is not a programming question : about VB per se, but I want to implement it in VB. (To be precise, I : want to develop a VB application that solves any arbitrary differential : equation input by the user.) So my query is really directed at the : programming community per se, since I want to understand how this is : done, conceptually. Then I can implement it in VB. If anyone can : direct me to general info on the subject, I would greatly appreciate the : advice. Better yet, does anyone know where it has been done in VB : specifically? I guess it's too much to expect a 3rd party to have : developed some tool to do this, but ...
I did it for a formal logic program, though I did it in QuickBasic 4.5 at the time--all the calculations would translate to VB just fine, however. I have this to say: it made my head hurt. How to do it? I don't know. I just went at it until it worked. And I wasn't terribly skilled at the time--it was my first program since I learned BASIC on a c64 in 1980, some 16 or 17 years later. The result was functional, but man was the code ugly. However I learned a great deal. Which is all to say: it can be done; it does not require guru-level technical skills; and it will probably drive you mad. I was mad before I began, so I had nothing to lose. Good luck and best wishes, Paul
|
Tue, 30 Jan 2001 03:00:00 GMT |
|
 |
mcar.. #4 / 13
|
 Parsing Equations in VB
Quote:
> ... I guess it's too much to expect a 3rd party to have > developed some tool to do this, but ...
-- I can't give you actual URLs but I found (less than 6 months back) this stuff on the Web: - VB source code for evaluating expressions (with support for some functions) - An OCX - A general purpose parser (look for nparse32.zip) A search with Altavista (altavista.digital.com) should turn up some useful stuff. Good luck. MikeC PS Would really appreciate it if you post final results of your search. Please reply to the group.
|
Tue, 30 Jan 2001 03:00:00 GMT |
|
 |
<RyesonC.. #5 / 13
|
 Parsing Equations in VB
Quote:
> : I would like to develop a VB application that takes a mathematical > : equation, input by the user, and evaluates it. This requires writing a > : "parser" -- code that takes a string input, which the user puts into a > : text box as a string, something like sqrt(sin(x)*x^2/tan(x)), or > : whatever -- and "parses" it into its constituent components, which are > : then read into a module or procedure which evaluates the equation, > : returning the value of that equation for any particular value of x. > : This is done routinely in programs like Mathematica, Maple, Matlab, > : etc., which take strings typed in by the user and parses them into > : functions that can be evaluated. This is really a daunting task, since > : the equation can be quite arbitrary, i.e., consiting of almost any > : combination of the standard arithmetic operatios (+,-,*,^,/) and the > : standard functions (trig, exponential, polynomials,radicals, etc.), > : embedded within any number of parentheses. (Of course, in practice, > : some limitation on the input could be imposed). I'm interested in > : learning how this parsing is done. This is not a programming question > : about VB per se, but I want to implement it in VB. (To be precise, I > : want to develop a VB application that solves any arbitrary differential > : equation input by the user.) So my query is really directed at the > : programming community per se, since I want to understand how this is > : done, conceptually. Then I can implement it in VB. If anyone can > : direct me to general info on the subject, I would greatly appreciate the > : advice. Better yet, does anyone know where it has been done in VB > : specifically? I guess it's too much to expect a 3rd party to have > : developed some tool to do this, but ...
> I did it for a formal logic program, though I did it in QuickBasic 4.5 at > the time--all the calculations would translate to VB just fine, however. > I have this to say: it made my head hurt. How to do it? I don't know. I > just went at it until it worked. And I wasn't terribly skilled at the > time--it was my first program since I learned BASIC on a c64 in 1980, > some 16 or 17 years later. The result was functional, but man was the > code ugly. However I learned a great deal. Which is all to say: it can be > done; it does not require guru-level technical skills; and it will > probably drive you mad. I was mad before I began, so I had nothing to lose. > Good luck and best wishes, > Paul
I saw an article in a back issue of VBPJ that did this.
|
Tue, 30 Jan 2001 03:00:00 GMT |
|
 |
Gerry Thoma #6 / 13
|
 Parsing Equations in VB
Quote:
> I would like to develop a VB application that takes a mathematical > equation, input by the user, and evaluates it. This requires writing a > "parser" -- code that takes a string input, which the user puts into a > text box as a string, something like sqrt(sin(x)*x^2/tan(x)), or > whatever -- and "parses" it into its constituent components, which are > then read into a module or procedure which evaluates the equation, > returning the value of that equation for any particular value of x. > This is done routinely in programs like Mathematica, Maple, Matlab, > etc., which take strings typed in by the user and parses them into > functions that can be evaluated. This is really a daunting task, since > the equation can be quite arbitrary, i.e., consiting of almost any > combination of the standard arithmetic operatios (+,-,*,^,/) and the > standard functions (trig, exponential, polynomials,radicals, etc.), > embedded within any number of parentheses. (Of course, in practice, > some limitation on the input could be imposed). I'm interested in > learning how this parsing is done. This is not a programming question > about VB per se, but I want to implement it in VB. (To be precise, I > want to develop a VB application that solves any arbitrary differential > equation input by the user.) So my query is really directed at the > programming community per se, since I want to understand how this is > done, conceptually. Then I can implement it in VB. If anyone can > direct me to general info on the subject, I would greatly appreciate the > advice. Better yet, does anyone know where it has been done in VB > specifically? I guess it's too much to expect a 3rd party to have > developed some tool to do this, but ...
VideoSoft's vsocx contains an Awk control that does more or less what you want. However, even V6 of this control continues to have problems, specifically, it fails to catch errors 11-16. If they manage to get it working to its claimed design, it will be a very useful tool. Good Luck, Gerry T.
|
Tue, 30 Jan 2001 03:00:00 GMT |
|
 |
Thomas Nieman #7 / 13
|
 Parsing Equations in VB
At http://members.xoom.com/thomasn/o_man.htm is a document that explains operator-precedence parsing. While the examples are in C, it shouldn't be very difficult to translate to VB.
|
Tue, 30 Jan 2001 03:00:00 GMT |
|
 |
VBDi #8 / 13
|
 Parsing Equations in VB
Quote: >I'm interested in learning how this parsing is done.
Parsing depends on the type of your grammar, so you should specify what kind of parser (LALR1...) you need. If you have not yet designed your grammar, you should do so first. Without a grammer, only a scanner for equations can be implemented. Herefore I'd suggest to restrict operators to single characters, and hold these in a single string. Then you can classify every input character with InStr(Operators, InputChar). If it's found in the string, the previous non-operator characters are combined into a literal token, then the according operator token can be created. The handling of white space can be simplified to Trim$ of the literals, and no token must be created when the trimmed string is empty. The string literals can be subdivided into numeric values (starting with a digit), that can be converted to value tokens with Val, or other names, that can be searched in the collections of all predefined functions (sin, sqrt...), of all user defined functions, and finally in the variables collections. If no matching entry was found, it should be the name of a new variable, and be added to that collection. The output of the scanner then consists of operators, values, predefined functions, user defined functions, and variables. The further handling of these tokens depends on your grammar. DoDi
|
Wed, 31 Jan 2001 03:00:00 GMT |
|
 |
Paul L Jackso #9 / 13
|
 Parsing Equations in VB
While there is lots of supporting code to learn about, the three central things to know are tokenizing (turning the character string into an array of identified particles) precedence removal for languages like you mentioned (sometimes called Polish notation for its inventor) and evaluation. Tokenizing can be accomplished in VB by identifying the type of each string segment. You look at the first non-blank character you encounter. Some first characters, such as quote require you to go looking for a matching end, while others just grow in length until a character not of that class is found, such as 123+ where plus isn't a member of numbers. In VB, I'd keep the tokens in a collection of objects which expose at least Value (the string itself), Style (the type, which happens to be reserved) and Precedence which is a function of the Style. Style is fairly simple, tokens are either functions, values, or controls. For a simple language, controls usually include (,). Looking just at the Value, the Polish for 3 + 4 * 5 would produce 3 4 5 * + because * has higher Precedence than +, where as ( 3 + 4 ) * 5 would produce 3 4 + 5 * Because the parenthesis force a Precedence. Evaluation is pretty simple once you have a precedence free statement, such as Polish notation. You take items from the Polish stack and push them on the result stack. Each function knows how many items from the result stack to pop as its arguments, and it pushes its result back on the result stack. Evaluation ends when the Polish stack is empty, and the result stack is the value to return. If your language supports assignment statements, then the result stack should be empty in those cases. There is a little more work to do if you want to support other statement types such as If or Do While, but these are the basics of statement evaluation. Paul L Jackson
|
Fri, 02 Feb 2001 03:00:00 GMT |
|
 |
Mark Pickenhei #10 / 13
|
 Parsing Equations in VB
Check out the script control that you can download from Microsoft, or get with VB6. It can do it all, enter the formula at runtime (or whatever code you want in VBScript or JavaScript) and it will just do it. It's great.
| |>I would like to develop a VB application that takes a mathematical |[snip] | |You can see my code for this: located at |http://www.geocities.com/SiliconValley/Lakes/1218/Solve10.zip | |it's commented in Italian, but the big problem is that it is a stupid |way to solve math expression (btw, it solves only +-/* operators and |parenthesis), very ugly coding, but it might still be a good start. | |Bye |
|
Sun, 04 Feb 2001 03:00:00 GMT |
|
 |
Gerald Campbe #11 / 13
|
 Parsing Equations in VB
Quote:
>I would like to develop a VB application that takes a mathematical
[snip] You can see my code for this: located at http://www.geocities.com/SiliconValley/Lakes/1218/Solve10.zip it's commented in Italian, but the big problem is that it is a stupid way to solve math expression (btw, it solves only +-/* operators and parenthesis), very ugly coding, but it might still be a good start. Bye
|
Mon, 05 Feb 2001 03:00:00 GMT |
|
|
|