?
Solved

Expression evaluation

Posted on 2003-02-25
11
Medium Priority
?
348 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
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!

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

770 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