Expression Parser

welchb

Active member
Joined
Jul 4, 2005
Messages
25
Location
UK
Programming Experience
Beginner
Hello All,

Has anyone on these forums ever written an Expression Parser and probably more to the point a simple expression parser.

I'm reasonably competent in VB6 and am now starting a project in VB.NET where I would like to have the ability for the users to define their own expressions/functions.

If I'm not describing this very well it's like the formula you can put into Excel.

My requirement is for both Math and String functions and I'm happy to code the individual functions but I'm not sure how to write the expression parser or where to start.

I've searched the web and really have conflicting examples so failing anyone doing it themselves can anyone point me at some useful info. they have.

Thanks & regards,

Brian
 
Hi Thanks very much,

Very interesting article -- I think this is over and above what I want to do but very powerful.

Might take me a bit of time to come up to speed with this.

Again tkx.

Brian
 
It might not be as difficult as I was thinking at first. If you are going to have predefined functions, say, for example: myFunc(int,string)...

you would just parse that like you would any other string. Anything before the left parenthesis would be your function name, anything withing the parentheses would be your argument list. Then just call your function. No?
 
Sorry to double post.. I keep thinking of more things...

If a person does something like... = 3 + myFunc(int,string)

you would just keep in mind your basic algebra... break down the expression into parts. In this case, you have a literal value, 3, a function, myFunc, and an operator, +. Your code would need to submit each expression to a series of checks to determine what parts are there. Follow the order of operations. First, is there a plus or minus sign? If so, evaluate the segments on either side of that operator. Is there a multiplication, division, modulus, etc.? Are there parentheses?

It will take some time to make sure you've covered all the bases, but I'll bet it is more within your grasp than you think. Pull out your old algebra book. :)
 
Yep this is more from where I was coming from I want to write an expression parser that's capable of understanding something like the following examples (where the variables/formula are defined within a form) -->

Example 1) Integer
x=10
y=5
z=5

Myresult=(x+y)*z

Example 2) String
mystring1="abcdefghi"
mystring2="1234567890"

MyResult=MID(mystring1,3,2)+RIGHT(mystring2,3)

etc.....

Obviously the parser would need to undertand the expressions in terms of formatting paranethess and the maths expressions and then evaulte the results.

Myresult=(x+y)*z
MyResult=MID(mystring1,3,2)+RIGHT(mystring2,3)

I'm still not clear if the 1st example you provided is an easier way to go or not.

I know many of the functions/subs/expressions I want to use are built into VB.NET -- I just want to try to expose these within the user interface.

Thanks again for your help.

Brian
 
Resolved

:D Excellent thanks so much for your help.

I had searched the net but had not found this little gem.

I think I (again) need to understand the workings and then adapt it to handel string functions too but I'm sure that's possible now.

All the best,

Brian
 
Hello there,

I did something like this for a software I made that used MS Agents. I needed to make the characters say things based on a script that would be given to it. So I wrote an expression parser. I guess mine wasn't very complicated, I just had a lot of Select Case statements :) for the functions themselves. And then I had separate routines to handle the parameters given to each function.

Heh
 
Do you have an example you can post or email please ?

I think the above has given me some idea's but I'd like to consider all other alternatives.

I'm quite new to VB.NET so would appreciate all the help.

Thanks in advance,

Brian
 
I've just written a simple Expression Parser for VB.NET :D I know this is a very old topic, but if you still need it, let me know.
 
Hi,

Many thanks for the reply -- if you could email to me that would be great as a .ZIP

Probably the best email address is -->

******email removed****

Or if you could post back to this board with the .ZIP

Many thanks,

Brian
 
Last edited by a moderator:
Back
Top