Jump to content

Syntax Checker


EJW

Recommended Posts

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!

Link to comment
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)").

Link to comment

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

Link to comment
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

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.