Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Expression parser
Message
De
25/11/2007 06:59:16
Hilmar Zonneveld
Independent Consultant
Cochabamba, Bolivie
 
 
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Versions des environnements
Visual FoxPro:
VFP 8 SP1
OS:
Windows XP
Database:
Visual FoxPro
Divers
Thread ID:
01271086
Message ID:
01271112
Vues:
38
This message has been marked as a message which has helped to the initial question of the thread.
Nadya,

The "quick and dirty" approach, to evaluate formulae, would be to use evaluate().

However, this may open some security holes, i.e., the user can call any available UDF, for example. Also, you don't have all the flexibility you are asking for; although you might first parse the expression (inserting implicit "*", for example), and then use eval().

I would do a search on the Web, for something like "algorithm for evaluating expressions".

But form the top of my head, I would say that you could first look for parentheses and evaluate that part first, perhaps with recursion.

Then, evaluate the expression using standard algebraic rules, e.g.: first replace all variables with their values, then evaluate all "^", in a second step all "*" and "/", in a third step all "+" and "-", in every case from left to right (although, the standard interpretation of a^b^c = a^(b^c)) . For example, if you see a "*", multiply the value on the left with the value on the right, and replace the entire expression, e.g. 5*7, with the calculated value, before continuing with the next multiplication or division.

Some complications may arise, e.g. checking for mismatched operators or parentheses may require additional work. Or perhaps this comes quite naturally while evaluating expressions, i.e., you see no object on the right of a "*" sign, and declare an error.

>Hi everybody,
>
>I'm trying to work out an algorithm for expression parser. A while ago I read something about it in Knut, but I could not find this book at the moment, besides, my expressions are much easier.
>
>Let me try to describe the problem.
>
>The expression may include units, such as sf, lb, etc.
>
>It also may include variables that correspond to properties of the class, say,
>
>cw - this.nCW
>
>f - this.nFloor
>
>c - this.cCeiling
>
>w - this.nWalls
>
>etc.
>
>Examples of expressions:
>
>cf is the same as c*f = this.cCeiling * this.nFloor
>
>2f the same as 2 * this.nFloor
>
>2@ is the same as 2
>
>2sf is the same as 2 (units)
>
>multiplication may be expressed by *, by x or just by placing var var (no space) or Number var or var number.
>
>There could be multiple parts, such as, I think:
>
>sf + cf
>
>This would be 1 + this.nCeiling * this.nFloor
>
>My original idea based on limited number of examples was to first remove all units from string, replace x with * and then work with variables translating them into class properties and inserting * if needed.
>
>I'm wondering if you can give me some direction about approaching this problem.
>
>Thanks.
Difference in opinions hath cost many millions of lives: for instance, whether flesh be bread, or bread be flesh; whether whistling be a vice or a virtue; whether it be better to kiss a post, or throw it into the fire... (from Gulliver's Travels)
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform