Thread: Parse

  1. #1
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346

    Parse

    I am trying to write a function where I can pass it a string with a mathematical experession and it will return the answer in the form of a float variable.

    For example:

    Evaluate("12.2*2");

    This would return 24.4.

    If anyone has any links to resources and information about parsers or any source code could they please share it with me. I would also like to be able to evaluate advanced expressions like ((82.5-3.14)*2)/9.
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Personally, I would suggest a linked list container for this type of problem...anyway, there is no "simple" way, but to be brief, parsing, in general is something you need to master as a programmer. So spend a good two months hunkered down in a quiet spot and really put all your effort into it! The basic mechanism I use is a simple switching flag that works like a train switch, directing output as necessary. here's an example:

    //count words in a string...
    Code:
    
    int CountWords(char str[], int len)
    {
     int i = 0, flag = 0, count = 0;
    
     for( i = 0; i < len; i++ )
      {
    
        if(!isspace(str[i]))
         {
           if(!flag)
            {
              ++count,  flag = 1;   
            }
         }
        else
         {
           flag = 0;
         } 
    
     }
    return count;
    }


    ...This is the basic switching mechanism that is used in parsing....

    Play with it, and expand on it to fit your needs.

    happy Coding
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    One technique is to analyze the expression and place flags that the parser can identify and use appropriately. Let's take your expression as an example, this is the raw input:
    ((82.5-3.14)*2)/9

    After analyzing it we would end up with something like this for the parser:
    <o1><o2><val 82.5><op1 -><val 3.14><c2><op2 *><val 2><c1><op2 /><val 9>

    This will take a bit of explaining, but it really is simpler. Tokens are each surrounded by angle brackets to easier delimit parsing, immediately after the opening angle bracket is an identifier which tells the parser if the token is an operator, opening paren, closing paren, or value to be calculated. The paren tokens are each given matching values counting up from 1 so that the parser can determine which operations to perform first. The operator tokens also have a value which describes their precedence so the parser will return the correct value. Value tokens will also have the actual value.

    The job of the parser from here is to find the opening paren with the highest value and perform the calculation according to precedence until it reaches the closing paren with the same value. It saves that result by replacing the paren tokens and everything inside of them with a single value token and the result. The parser then moves on to the next highest opening paren and does the same thing, so the complete parsing of the expression would be:
    ((82.5-3.14)*2)/9
    (79.36*2)/9
    158.72/9
    17.63

    When all that is left is a value, the parser returns it. As you can probably see, this makes the job of parsing the expression considerably easier than if you tried to do it with the raw expression.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Preludes example was excellent.

    This would be a superior approach to tackling this problem.

    Good work Prelude...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    Thanks for your help
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  2. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM