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)....
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
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,
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.
Bookmarks