EJW Posted June 21, 2006 Report Share Posted June 21, 2006 Does anyone have a clue how to set up a syntax checker. Here is the issue: I have an input form for a user that includes a string control where they can type in a formula. There are 6 variables allowed, the parentheses (), and the boolean operators "AND" and "OR". I want to somehow be able to check to make sure there are no errors in the formula, for example, a missing parenthesis, or a misspelled variable. A formula like this : (VARIABLEA AND VARIABLEC OR VARIABLE B would be incorrect because the ")" is missing after "VARIABLEC" Please HELP! Quote Link to comment
didierj Posted June 22, 2006 Report Share Posted June 22, 2006 Does anyone have a clue how to set up a syntax checker.Here is the issue: I have an input form for a user that includes a string control where they can type in a formula. There are 6 variables allowed, the parentheses (), and the boolean operators "AND" and "OR". I want to somehow be able to check to make sure there are no errors in the formula, for example, a missing parenthesis, or a misspelled variable. A formula like this : (VARIABLEA AND VARIABLEC OR VARIABLE B would be incorrect because the ")" is missing after "VARIABLEC" Please HELP! As far as I know you'll have to program the stuff yourself. Maybe by googling you might find something that you can modify to your need. I suggest to make yourself confident with the string primitives ("Match first string", "scan for tokens" could be good to begin with) and recursion. Recursion is needed to go through the unknown number of paranthesis and find the order to execute the operators (AND has higher priority than OR: "a OR b AND c" == "a OR (b AND c)"). Quote Link to comment
jpdrolet Posted June 22, 2006 Report Share Posted June 22, 2006 You can take a look at the VI Parse Formula String.vi which analyzes the syntax of a formula string. However, variables names are limited to a single lowercase letter alone of followed with a single digit. You can use Substitute Variables.vi to map your variable names to legal names. The binary operators OR and AND are not supported either so you have to substitue them with valid operators such as + and *. For example, map your formula (VARIABLEA AND VARIABLEC OR VARIABLE B to ((a) * (c) + (b) and the check the syntax using Parse Formula String.vi Quote Link to comment
adam85 Posted June 22, 2006 Report Share Posted June 22, 2006 Recursion is needed to go through the unknown number of paranthesis and find the order to execute the operators (AND has higher priority than OR: "a OR b AND c" == "a OR (b AND c)"). Are you sure? I don't think that the Parse Arithmetic Expressions.vi in the strings library uses recursion...it looks for the first ")" and then looks backwards till it finds a "(", and then simplifies the expression and keeps doing it (in a loop). By all means correct me if i'm wrong, again I just bring this up because coincidentally I was looking at that VI recently and that's what it SEEMED to do to me. Adam Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.