parsing math expressions

General FreeBASIC programming questions.
Post Reply
rmann05
Posts: 50
Joined: May 31, 2005 16:02
Location: Florida
Contact:

parsing math expressions

Post by rmann05 »

Hello. I'm working on a simple calculator in FreeBasic. It works, but it's limited in what kind of math problems you can enter. You could enter something like 12*3. After you press the enter key, it prints the result which in that case would be 36. If you wanted to add 5 you could type "+5" and press enter again. Is there a way to parse math expressions in FreeBasic so that the user could enter something like 12*3+4 or 4+3(9*2)? I figure there has to be a way to be able to have a user enter more complexed expressions than two numbers and an operator, but so far, I haven't been able to figure out how to do it.
Thanks.
Ryan
jdebord
Posts: 547
Joined: May 27, 2005 6:20
Location: Limoges, France
Contact:

Post by jdebord »

Two parsers have been proposed in the old QBN forum. I have saved them in the "Files" section of my mailing list (http://groups.yahoo.com/group/fbmathlib/files/Parsers/)

If you prefer, I can repost them here.
Nexinarus
Posts: 146
Joined: May 28, 2005 6:08
Location: Everywhere
Contact:

Post by Nexinarus »

marzec wrote a nice math parser already at bad-logic forums, it seems quite functional (and expandable).
jdebord
Posts: 547
Joined: May 27, 2005 6:20
Location: Limoges, France
Contact:

Post by jdebord »

Yes, it's one of the two I mentioned...

The other one is by Randy Keeling and uses RPN.
Keeling
Posts: 148
Joined: May 27, 2005 15:03
Location: USA
Contact:

Post by Keeling »

Marzec's and mine do the same thing in different ways. Both are expandable and easy enough to use. I did it in RPN because that is what I was comfortable with at the time.

What was posted on QBN for both of them were WIP. I had variable and constants implimented, Marzec didn't... but could have added them in no time.

In the end, use the one you like better. I don't think anyone had done any benchmarks on who's is faster. If they did, I have a gut feeling that Marzec's would out do mine. But I feel mine is easier to follow.
jdebord
Posts: 547
Joined: May 27, 2005 6:20
Location: Limoges, France
Contact:

Post by jdebord »

Keeling wrote: I had variable and constants implimented
The version I have doesn't have these features. Could you please post the new version?
etko
Posts: 113
Joined: May 27, 2005 7:55
Location: Slovakia
Contact:

Post by etko »

I've written my first parser according to Jack Crenshaw book about simple compilers, there is ASCII version on the net ;). It might be good for you if want to get it how it works. At least it was good reading for me.
Keeling
Posts: 148
Joined: May 27, 2005 15:03
Location: USA
Contact:

Post by Keeling »

Its the very first piece of code in that thread on QBN.

http://forum.qbasicnews.com/viewtopic.p ... light=math
Post Reply