Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Expression evaluation

Posted on 2003-02-25
11
Medium Priority
?
356 Views
Last Modified: 2010-04-01
I have written a program that takes an equation as an argument, converts it to postfix, evaluates it, and spits out the answer. For example, I can type in,

((2^16-1)+((5-3)*((9/3)*5)))/7

and I will get 9366.43 as the output. My question is this, where can I find information on how to perform more advanced expression evaluation? Like that used by compilers, for example. I do realize that the method I am using is similar to what the compiler is doing but I doubt that it is really this simple.

Exceter
0
Comment
Question by:Exceter
11 Comments
 
LVL 22

Expert Comment

by:ambience
ID: 8022730
can you give a concrete example of the expressions you would like to be able to evaluate. The algorithm used by compilers can be same as the one you have used or it might be little different etc. but it all depends upon the language syntax and other things, but in the end it has to be something that works, and works for all legitimate expressions to be supported.
0
 
LVL 8

Author Comment

by:Exceter
ID: 8022803
>> can you give a concrete example of the expressions you would like to be able to evaluate.

It's not so much a peticular kind of expression that I want to evaluate, although I am working towards it being able to solve equations with unknowns, but rather I want to know how a compiler evaluates a source file, finds errors, recognizes function names, etc.

>> ...it has to be something that works, and works for all legitimate expressions to be supported.

That is what I want to know. What kind of algorithm is used to evaluate all of the possible expressions found in a CPP file?

Note: I don't need any code. All I need is an article/tutorial that explains how this is done. I can do the rest myself. I wrote what I have so far from scratch out of a plain text explanation of how to convert from infix to postfix, I even wrote my own dynamic stack and yes I do realize that that is reinventing the wheel. :-)

Exceter
0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 8022897
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Accepted Solution

by:
ambience earned 500 total points
ID: 8022915
>> how a compiler evaluates a source file, finds errors, recognizes function names, etc.

that is very much implementation dependent, but in almost all the cases i've found out that compilers use state-machines and grammar rules etc. Maybe the best way for you to understand is to actualy have a look at something like the following

http://eic.sourceforge.net/overview/

this is an open-source C language interpreter. Maybe this can help you understand things.
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8023646

>>>Note: I don't need any code. All I need is an >>>article/tutorial that explains how this is done. I can >>>do the rest myself. I wrote what I have so far from >>>scratch out of a plain text explanation of how to >>>convert from infix to postfix, I even wrote my own >>>dynamic stack and yes I do realize that that is >>>reinventing the wheel. :-)


nice .. i know this .. and i wud also have written something that much explicit..

sorry for irrelevant comment.. at least i can get notifications on this topic
0
 
LVL 7

Expert Comment

by:burcarpat
ID: 8026167
for peer-reviewed, easy to use and complete parsing tool, recently accepted to boost.org, see,

   http://spirit.sourceforge.net/

this is a blazing fast parser, is not just a simple regex engine, and can be extended easily for future needs.  the introductory example is a simple expression evaluator so i am guessing it's a perfect match ;-)

0
 
LVL 8

Author Comment

by:Exceter
ID: 8028129
Please don't think I have forgotten about this question. It is going to take some time for me to sift through all of this information. :-)

Exceter
0
 
LVL 8

Author Comment

by:Exceter
ID: 8049902
Okay, I have looked at each of the links that you were good enough to send me. I beleive that the documentation that came with EIC(Extensible Interactive C), suggested by ambience, and the documentation that came with Boost's Spirit Parser, suggested by burcarpat, were the most helpful so I intend to split the points between them.

roshmon and thienpnguyen, I found the links you posted to be helpful as well and so I posting a 50 point question for each of you.

thienpnguyen,
http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20535056.html

roshmon,
http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20535057.html

ambience and burcarpat, a moderator should be showing up shortly.

Exceter
0
 
LVL 1

Expert Comment

by:Computer101
ID: 8050002
Points reduced for a split.  You can now accept one experts comment as an answer.  After that, make another question in this topic area for the other expert.  Make the question for the amount of points intended.  Label the question, "Question for (Expert Name) and post this question number in the base of the question, i.e. For your assistance in question # --------


Computer101
E-E Admin
0
 
LVL 8

Author Comment

by:Exceter
ID: 8050481
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question