‎03-26-2010 07:11 AM - edited ‎03-26-2010 07:13 AM
I have an indented array containing texts that executes one by one. It has nested While and For loops, each terminated with a corresponding End While and End For. When I reach End For I need to find out to which For loop number does it belong (so that I can access its number of iterations from a global). How may I do it? I can't come up with an algotithm that works, although I have tried many possiblities (al without success) for the past few days. Kindly help and take me out of this predicament. Thanks!
‎03-26-2010 07:16 AM
Writing a parsing algorithm like that will need to be much more defined than the example you have posted. You will need to define constructs for functions (e.g. initialise, power off) and variable declaration (Count). You would need to take into account white space; tabs and spaces between words (power_off)
You would quite easily be able to do something with an array of strings and a state machine; Index the array and run the appropriate state.
‎03-26-2010 08:04 AM
‎03-26-2010 08:13 AM
Well in any language I can think of, the closing statement / bracket closes the most recently opened structure. What I would do would be to read all of your commands in the array array and convert it to reverse polish notation FILO stack, which would remove a lot of need for the open/close logic you are suggesting.
May I ask whether this is just a thought exercise, or if it will be implemented where users will be able to write their own commands
‎03-26-2010 08:15 AM
‎03-26-2010 08:21 AM
‎03-26-2010 08:30 AM - edited ‎03-26-2010 08:33 AM
First, I would avoid using global variables. There have been many threads which discuss why using globals is not a very good practice.
Regarding your specific issues have you considered using a stack to hold you state information. Stacks are widely used in the implementations of compilers and interpreters. As you encounter a loop statement you would push its information onto the stack. When you encounter an end statement you would pop off its information. This will maintain your order and nesting. Part of the data on the stack could be your termination condition, the currentl loop counter, or other state information about the loop. When you reach an end statement you pop off the data, evaluate it, update if necessary and either puch it back on the stack to do another iteration or move on to the next statement after the loop.
‎03-26-2010 08:34 AM
‎03-26-2010 08:51 AM
Its a functional global.
Lets simplify the problem. I have an array of strings. It contains some random strings and four of these strings "While", "End While", "For", "End For" which maybe placed in the form of nested loops. What I need now is when I reach End For, I pass its index and this array to that VI and that VI tells me which For (a number) that End For belongs to. That's all.
‎03-26-2010 08:51 AM