Click here to Skip to main content
15,860,972 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to parse a string to get something like a formula out of it. My question can be best explained by the following examples:

example 1: (12345 in year 1 / 23451 in year 0)-1)*100

solution
1. (
2. /
3. )-1)*100

12345, 1
23451, 0

example 2: [[1234 in year 1) - (4567 in year 2)] / (7890 in year 3) * 100

solution
1. (
2. -
3. )/(
4. )*100

1234, 1
4567, 2
7890, 3

It is quite easy when we do it manually but unfortunately I need to do it for hundreds of inputs. The examples were provided to me along with the task. Notice how the third brackets have been ignored in example 2 but the input expression can still be framed without error, from the solution.

I just need some help with the logic, if we can get something out of it, so it I can write it like a general fucntion.
Posted
Comments
Abhijit Ghosh (Subho) 17-Aug-14 7:38am    
The question was downvoted. An explanation would be nice.
Subho

1 solution

Well, one of the ways to find enough material on the topic is searching CodeProject: http://www.codeproject.com/search.aspx?q=Expression+parser&doctypeid=1[^].

But please, don't tell me "this is not VBA". The problem is more or less serious, and people capable of solving serious problems rarely agree to waste time on VBA. If you want a VBA solution, read some relevant article, understand how the code works and implement it in the language and the system you prefer.

I think that an alternative approach could be fruitful: hosting some available language interpreter in your code. In practice, the most likely and apparent candidate would be Javascript: it already has the function eval (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval[^]) which allows to interpret and run a script written in some string, from the same script: http://www.codeproject.com/search.aspx?q=%28Javascript+interpreter%29+OR+%28%28host+OR+embed%29+Javascript%29&doctypeid=1[^].

To get familiar with the problem of hosting/embedding of the interpreter, you can perform something like this "sophisticated" query: http://www.codeproject.com/search.aspx?q=%28Javascript+interpreter%29+OR+%28%28host+OR+embed%29+Javascript%29&doctypeid=1[^].

Of course, you can do a global Web search with these queries, Google/Bing it. This way, I found some articles even for VB (but not VBA):
http://www.freevbcode.com/ShowCode.asp?ID=2090[^],
http://www.freevbcode.com/ShowCode.asp?ID=1263[^].

The main obstacle? VBA, of course. You just need the simplest features, it looks like. So, probably, the shortest route will be to look at some available expression interpreter and write some minimalistic implementation after learning the main ideas.

In brief, the idea is: the input expression in a single string is parsed into an expression tree. Then the calculation is performed by recursive traversal of the tree starting from the root: http://en.wikipedia.org/wiki/Expression_tree[^].

So, this is yet another option I would personally prefer: write the code from scratch following the article referenced above.

—SA
 
Share this answer
 
v2
Comments
Abhijit Ghosh (Subho) 17-Aug-14 7:35am    
Took your advice and wrote it from scratch. The parsing part is complete.
Thank you SA.
Subho
Sergey Alexandrovich Kryukov 17-Aug-14 14:07pm    
This is great. This is such a pleasure to help someone who can immediately get it and make things working.
You are very welcome. Good luck, call again.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900