Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / String parser

Author
Message
keeblerElf
21
Years of Service
User Offline
Joined: 11th Jun 2003
Location: United States
Posted: 25th Aug 2003 08:50
I've been working on it for the past 15 minutes...it's not too good at all yet all it can do is add stuff like 1+x and 2+2 and stuff or do 2^x or 2*x...oh well just look at the code you'll get it...I'll work on it some more later and post a better version

Live life and play video games
APEXnow
Retired Moderator
21
Years of Service
User Offline
Joined: 15th Apr 2003
Location: On a park bench
Posted: 25th Aug 2003 14:24 Edited at: 25th Aug 2003 14:57
keeblerElf, just a suggestion but you may want to lookup recursive descent algorithms. They are used for interpreted multiple expressions in some parsers.

M00NSHiNE
21
Years of Service
User Offline
Joined: 4th Aug 2003
Location: England, UK
Posted: 25th Aug 2003 16:44
Sorry to sound like a newbie (I assure you im not) but you only learn by finding out so :

What is a parser?

Ive heard it mentioned but i am unsure what it does.

"It's amazin' what you can do with a computer and access to t'internet"
APEXnow
Retired Moderator
21
Years of Service
User Offline
Joined: 15th Apr 2003
Location: On a park bench
Posted: 25th Aug 2003 18:35
When DB compiles your code, it (Parses) your source code to compile it into the .EXE that you run. When you Spell check a document, the Word processor Parses your document to find the spelling mistakes. There are many types of parsers, but they all have a common function of scanning the input media and generating an output stream based on the parser's function.

keeblerElf
21
Years of Service
User Offline
Joined: 11th Jun 2003
Location: United States
Posted: 25th Aug 2003 19:35
Thanks APEXnow...I've been having trouble figuring out how to evaluate more than 3 terms in the expression...I'm looking up recursive decent algoritms now

Live life and play video games
keeblerElf
21
Years of Service
User Offline
Joined: 11th Jun 2003
Location: United States
Posted: 25th Aug 2003 19:36
Moonshine...the parser I'm trying to make will evaluate an expression like "2^x" when x=1 or 2 or 3....

Live life and play video games
M00NSHiNE
21
Years of Service
User Offline
Joined: 4th Aug 2003
Location: England, UK
Posted: 25th Aug 2003 21:53
Thanks for clearing that up guys.

"It's amazin' what you can do with a computer and access to t'internet"
David T
Retired Moderator
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: England
Posted: 26th Aug 2003 15:16
I once manged to get a VB app to interface with a DB app and evaluate expressions - you could write something like so:



and 100 would be printed. The only downside is it's really slow, 0.2s to evaluate one phrase

I'll post a demo tonight.

There's no place like 127.0.0.1

There are 10 people in this world, those who understand binary and those who don't.
keeblerElf
21
Years of Service
User Offline
Joined: 11th Jun 2003
Location: United States
Posted: 1st Sep 2003 06:37
I couldn't find anything on the internet to help me so I went on with mine...so far it allows you to use any positive or negative real number and +,-,/,*,^ and sin and cos. I'm still having trouble with the damned parenthesis I'll post it once I can get the parenthesis to work
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 5th Sep 2003 19:02
There was an article in the last couple of months in a UK magazine that showed you exactly how to do this. You could take an expression with an unlimited number of operations and evaluate it.

Just wish I could remember the mag date. It was by that Wilf Hey guy in PC Plus.

keeblerElf
21
Years of Service
User Offline
Joined: 11th Jun 2003
Location: United States
Posted: 12th Sep 2003 00:19
Yeah I've figured it out already and it works fine...decimals and fractions and parentheses and sin cos tan ln all that stuff...I'll put it up here once I get back to my comp at home
keeblerElf
21
Years of Service
User Offline
Joined: 11th Jun 2003
Location: United States
Posted: 22nd Sep 2003 08:03
Sorry it took so long...computer was broken...anyways there are 4 functions, but you only really need two. The other two functions are so you can use the natural logarithm command...The parser allows you to evaluate expressions like

(sin(x)/cos(x))*2+1

and so on and so on...it ignores "white space". Sorry if you try to read the code and figure it out, I'm not very organized and my coding shows that very well. If you need me to explain something just ask. Also I took the Midstr$ function off TDK's website, I needed the old qbasic Mid function and I found it on his website, thanks.
keeblerElf
21
Years of Service
User Offline
Joined: 11th Jun 2003
Location: United States
Posted: 22nd Sep 2003 08:05
Oh there are only two constants in it right now..."pi" and "e", and if you want to use radians instead of the normal darkbasic degrees than you would use the "rad" command. If I wanted to find the cosine of pi radians I would type

cos rad pi

or

cosradpi

or

cos(rad(pi))

or anything along that sort
Coder
21
Years of Service
User Offline
Joined: 26th Sep 2003
Location:
Posted: 27th Sep 2003 04:04
Okay, here is my situation. I am writing a code to allow a user to enter a formula using three variables (X,Y,Z). The program will then use this formula, along with other properties to draw a graph on the screen. I have everything coded for the calucations and the drawing, however, I cannot get the user inputted formula to be used in the calculations. I have to enter it in manually into the code. Is there a way to modify your code to do this? And if so, how?

Thanks.
Coder
21
Years of Service
User Offline
Joined: 26th Sep 2003
Location:
Posted: 27th Sep 2003 04:07
Oops, I tried to add a small section of my code as an example.
I will try again.
keeblerElf
21
Years of Service
User Offline
Joined: 11th Jun 2003
Location: United States
Posted: 27th Sep 2003 09:14
Ok try this...I just added a few lines of code to my parser and it should work for f(x,y,z) although I don't know how you would graph that because it would be a 4-d graph...are you sure you didn't want to graph z=f(x,y)?
Coder
21
Years of Service
User Offline
Joined: 26th Sep 2003
Location:
Posted: 27th Sep 2003 21:30
Actually, you are right. The formula will need to be structured:
Z=(Any function with X & Y), i.e.: Z=sin(x)*cos(y)

Thanks for the code, I will try it, and let you know how it works.
If it works, I may be able to release a beta of my program within a week.

If you think the difference in how the formula is structured to one that may be 4D (The misinterpretation based on my vague describtion). Feel free to let me know.

Thanks again for you help.
Coder
21
Years of Service
User Offline
Joined: 26th Sep 2003
Location:
Posted: 29th Sep 2003 00:04
I am attempting to use your code in my program. I seem to be having some problems. Question: what version of DarkBasic are you using? I am getting a lot of syntax errors with it.
keeblerElf
21
Years of Service
User Offline
Joined: 11th Jun 2003
Location: United States
Posted: 29th Sep 2003 22:43
I'm using darkbasic Pro...it worked fine on my computer and my sister's computer...where does it say the error is occuring?
keeblerElf
21
Years of Service
User Offline
Joined: 11th Jun 2003
Location: United States
Posted: 29th Sep 2003 22:46
If you want a z=f(x,y) equation than just change the



to



and just change the name of the function to fxyeval#(fx$,x#,y#)
Coder
21
Years of Service
User Offline
Joined: 26th Sep 2003
Location:
Posted: 30th Sep 2003 02:52
Error #1:

dim fxs$(len(fx$))
Syntax Error. DIM statement

Error #2:

for z=1 to array count(eqhold$())
Parameter Mismatch. Array must have at least one subscript.
keeblerElf
21
Years of Service
User Offline
Joined: 11th Jun 2003
Location: United States
Posted: 30th Sep 2003 03:21
I don't know what's wrong with them...What version of DBPro are you using?
Coder
21
Years of Service
User Offline
Joined: 26th Sep 2003
Location:
Posted: 30th Sep 2003 08:00 Edited at: 30th Sep 2003 08:01
I'm not using DBPro, I'm using DBC v1.13.
keeblerElf
21
Years of Service
User Offline
Joined: 11th Jun 2003
Location: United States
Posted: 30th Sep 2003 23:46
Well then that must be the problem...It was made in Darkbasic Pro
keeblerElf
21
Years of Service
User Offline
Joined: 11th Jun 2003
Location: United States
Posted: 30th Sep 2003 23:48
I would suggest going through the code and changing it to work with DBC...I would but I've never used DBC before so I couldn't tell you the difference
Coder
21
Years of Service
User Offline
Joined: 26th Sep 2003
Location:
Posted: 2nd Oct 2003 07:01
Well, I've been doing as you suggested. Although, I have discovered that it is easier to rewrite the code using yours as a referance.

Right now, I can evaluate simple (non-nested) expressions. i.e. X*X+24/Y*2

I will be working on the parenthesis and such later...

Also, thanks for the help. I will post my completed code here.
keeblerElf
21
Years of Service
User Offline
Joined: 11th Jun 2003
Location: United States
Posted: 3rd Oct 2003 04:49
Ok cool I'd appreciate to see how a different person would go about making one

Login to post a reply

Server time is: 2024-11-24 01:45:19
Your offset time is: 2024-11-24 01:45:19