|
-
Sep 28th, 2002, 04:44 PM
#1
Thread Starter
Lively Member
Parse mathematical expressions
I need to parse mathematical expressions,so that the user can enter "2*2+4 instead of just 8.The user should also be able to use variables,so if the user sets the variable X to 4,2*2+X would also be 8.I also need to support some simple math functions like Tan,Cos and Sin.I was wondering if there was already a function/library which can do this,if so,could you post the link to some tutorials/documentation on this?
Btw,I need this to work under Linux,and preferable also on other OS'.
Thanks in advance.
-
Sep 28th, 2002, 04:59 PM
#2
Sounds like you're writing a very basic scripting language.
Maybe read about yacc, this is a tool that creates script parsers for you.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 29th, 2002, 11:07 AM
#3
Thread Starter
Lively Member
Yeah,but I only need to parse kinda simple expressions.I checked out yacc,but its way to hard for me Do you know any easier tutorial/explanation of how to make one (preferably with some exaple code)?
-
Sep 29th, 2002, 11:18 AM
#4
Monday Morning Lunatic
In it's simplest form, you need to parse it into tokens, which are the numbers, operations, etc.
Look up parsers and binary trees (the basis of how you determine how to interpret the stream of tokens).
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 29th, 2002, 01:47 PM
#5
Fanatic Member
Search the forums for "Expr" I made a parser a while ago (before the forums went down, so if you cant find it).... ill have to go dig it up for you, but search first and lemme know if you find it... or just search for threads started by MoMad and contain "Expr"
-
Sep 29th, 2002, 01:49 PM
#6
Fanatic Member
-
Oct 6th, 2002, 05:04 AM
#7
Lively Member
Re: Parse mathematical expressions
I need to parse mathematical expressions,so that the user can enter "2*2+4 instead of just 8.The user should also be able to use variables,so if the user sets the variable X to 4,2*2+X would also be 8
Search internet for INFIX and POSTFIX algorithms or pseudo algorithms.
It's quite simple do a *basic* expression parser:
- first
You should convert infix expression (eg. 2 * sin(3)) into postfix notation ( 2 3 sin * )
- second
You can "pop" each simbol from the stack and evaluate it.
see for example:
Good luck
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|