#Intuition and C (part 2)
20 messages in this thread
<continued from previous message) How do I take a string that has been
entered via a string-gadget and get it evaluated as a numerical value??
Remember, the user will be entering things like this: (3*x)+4 or cos(x)
or sinh(x)/cos(x)+sin(pow(x,2.0)) Any help, as always, would be greatly
appreciated. BTW, I'm compiling on my A2500 using SAS/Lattice 5.10b as: lc
-Lm plot.c Regards, – al saveriano –
PS…if anyone is interested in seeing the entire source, let me know and I
will EasyPlex it to you.
Al,
The "easy" answer is that you can't.
The "hard" answer is that you'll have to write the code to parse the
string yourself, and then generate the routines to evaluate the
expressions. In other words, you'll have to write a (limited) language
interpreter that understands algebraic expressions. Not a trivial task!
Sorry to be the bearer of bad news….
– BobR
(Motorola Inside!)
Bob, As my daughter might say: "Yuk!" I have no clue as to how that might
be accomplished. THxs, – al –
Actually, it's not as bad as Bob might have made it sound. Don't write,
adopt. There must be a half-dozen simple expression interpreters here on
Compuserve. Look around in the Dr Dobbs Forum (go ddjforum) or the Computer
Language forum, or check the IBM file finder.
(And if anyone makes any cracks about Lisp, I don't want to hear them. 🙂
Al,
I thought Bison was around in the libraries here, somewhere. Doesn't
it allow you to create a parser?
Brian
->on AutoPilot
I don't think Al is ready for Bison. I'm not sure I'd like to use it. But
for a quick-and-dirty parser, there are several hand-crafted ones in other
forums on Compuserve. They'd be much easier to understand than Bison.
John,
ok, but it is a bit simpler, if one understands the techniques
involved, especially if your unwilling to code by hand. I still stand by
my suggestion using Scripit and ARexx, especially when you compare it to
developing a parser.
I do know that works, since I graphed quite a few functions using
Scripit.
Brian
->on AutoPilot
Brian, I don't even know WHAT Bison is! 🙁 – al –
Al,
I believe it's GNU's (GNU Not Unix) answer to YACC (Yet Another
Compiler Compiler).
Purdue came up with something called "Antlr". It's a mix of YACC and
LEX….Mary's using it for her compiler project this semester.
Steve
Al,
Bison is the PD (Shareware?) version of YACC, an import from the Unix
world. Yacc (Yet Another Compiler Compiler) allows you to detail the
grammatical rules that your compiler is to use and then generates the
object code for you.
In some ways easier than trying to do it by hand, but in other ways,
i.e. knowing the notation/rules, a bit more difficult. I'm still
attempting to figure out my book on compiler construction 🙂
Good luck on your search.
Brian
->on AutoPilot
Al,
have you considered using ARexx with, perhaps, Scripit to do the actual
plotting function?
Brian
->on AutoPilot
Brian, No I hadn't. Probably because my ARexx is marginal and I haven't
used Scripit sincd first released it. Thxs, – al –
Al,
Have you ever heard of a freely distributable program called Amiga Plot?
This program does exactly what you are trying to accomplish. The only
problem (if you want to call it that) is that works on functions of two
variables (eg. f(x,y)). This gives some terrific 3-dimensional plots, but
for your use it sounds like that will be a hindrance (until your daughter
gets into calculus). But, by fiddling with the parameters a little, you
can get a decent 2-D plot. This program is probably on CIS. If not, I
know it is on Fred Fish or I can u/l for you here.
Bill
Bill, THanks for your generous offer. However, part of the challenge and
fun was programming the plotter myself. As noted in my earlier message, I
have the thing working fine but I don't have a way to use Intuition to
allow the typing in of the function. I have several 2d-3d plotter programs
on my drive but I don't have access to the source that shows how to get the
entered function (the string) translated into a numerical value. Thanks
anyway, – al –
Writing the function parser is actually not as bad as you might think. You
need to break the thing down into three basic pieces:
1) A tokenizer to identify each of the possible tokens: + – ( ) sin cos tan
arctan / * and so on
2) A data structure to represent the tokens (operation and children) with
routines to create and manipulate them.
3) A scanner to parse the tokens into a tree. For example, you might have:
sin(x)+y x+y+z x+(y+z)
+ + +
/ \ / \ / \
sin y + z x +
| / \ / \
x x y y z
Once you have the tree, it is pretty easy to create an interpreter to run
the tree and evaluate the operations. You might be surprised how easy it
is.
There's a file on here somewhere called 3dplot.lzh (I think that's it,
maybe *.lha?) which is the source for a 3d plotting program. Maybe it
would have a sample of what you're looking for in it?
-Sean
Thanks Sean ,I'll go look for it now. – al –
hmmm…
that is an interesting challenge!
I see two solutions, but both will limit the functionality of the progam a
little:
1) instead of a single string gadget use several, one for the x variable,
one for the function, etc.
2) write a simple parser that recognizes a handful of 'verbs;, and
translate it!
Actually, there is a third solution, but it isn't nearly as much fun and
it cost's money… buy Maple Math or Mathvision!
I wrote an expression evaluator in college, and it wasn't all that
tricky… of course I was in college, so I was writing code every day, and
had tons of resources to use!
Which reminds me, there may be expression parsers here on CIS or
ccertainly on internet! Ty the various language forums, or possibly the
technical magazine forums. I thin Dr. Dobbs still has a forum here. IF
they do, you culd leave a message there and would almost certainly receive
many answers!
Good luck!
Bill… on Autopilot in Womelsdorf 😉