Results 1 to 7 of 7

Thread: Parse mathematical expressions

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Posts
    69

    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.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Posts
    69
    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)?

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  5. #5
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    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"
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  6. #6
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  7. #7
    Lively Member neodatatype's Avatar
    Join Date
    Aug 2002
    Location
    Italy
    Posts
    103

    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
    > NeoDataType.net <

    Try my Free .Net Reporting Tool!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width