CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2012
    Posts
    1

    Please Provide an Algorithm and Source Code

    I hve an assignment to make

    i have to make a C/C++ Program that automatically identifies any expression entered and solves the answer..

    Example if i enter an expression like 3.14+(14-23*4/6)*7 it should evaulate it according to the precedence of the operator and give a numeric answer...

    I need to generate a Grammar and then a parse tree and then finally evaluate that parse tree...but i dont know where to start and how to code.

    please provide the source code and flowchart also (if possible)....

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Please Provide an Algorithm and Source Code

    See this http://forums.codeguru.com/showthrea...ork-assignment regarding what this forum can help you with
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    May 2009
    Posts
    2,413

    Re: Please Provide an Algorithm and Source Code

    Quote Originally Posted by krprashant1 View Post
    I need to generate a Grammar and then a parse tree and then finally evaluate that parse tree...but i dont know where to start and how to code.
    If this is done as part of some course you should know what to do but if not you could try a Recursive Descent Parser. It's simple and straightfoward so there are plenty of resources on the net you can look for. Here's one,

    http://en.wikipedia.org/wiki/Recursive_descent_parser

    You start by developing the grammar making sure it's LL(1). Then the actual parser follows almost automatically from the grammar.

    For the evaluation you have two options. Either evaluate immediately during parsing or generate a parse tree which is evaluated afterwards. It's virtually the same because the recursive parsing process will walk through an implicit parse tree. It's your choise whether you evaluate by making it explicit or not.
    Last edited by nuzzle; September 14th, 2012 at 03:46 AM.

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

Featured