LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Valid loops algorithm

Solved!
Go to solution
Dear friends,

I need algorithm / VI for checking an array which contains items and loop starts tags (text) (While and For) and loop end tags (End For and End While). The loops may be nested to any level and may be mixed (While loops inside For loops and vice versa). I need a VI that can parse this array (of strings) and output a Boolean if the loops are not logically placed (number of start loops not matching number of end loops, end loops starting before start loops etc) and a text stating the problem. Can anyone help!

Thanks a lot!

Pleasant day to you!
0 Kudos
Message 1 of 9
(4,566 Views)

Just implement a stack using an array or a queue (dequeue at opposite). Put the For or While start tags on the stack. For end tags, pop the stack and check if it is a match. At the end, check if the stack is empty.

 

Felix 

Message 2 of 9
(4,563 Views)
A reply within 4 minutes of the original post. Fantastic Felix. Great support. Smiley Happy
Message 3 of 9
(4,558 Views)
But what about illogical nested loops, like maybe;

For

While

End For

End While

and other such irregularities. What I had in mind was that maybe someone has got an algorithm (from programming languages, compiler creations, etc) that has captured all such possible illogical nested loops that I may safely use (reusable code).
0 Kudos
Message 4 of 9
(4,539 Views)
NI forums are one of the best support forums out there. Thanks to all those who help out.
0 Kudos
Message 5 of 9
(4,536 Views)
Solution
Accepted by topic author Rashid-Malik

Taking your example of an illogically nested loop:

 

put 'For'

put 'While'

 

Now the stack is:

While

For

 

and we get an ending tag 'For' which would evaluate:

pop=='For' -> 'While'=='For' -> false

and now we can also use both tags to display: "End For" encountered where "End While" was expected.

 

Back home I have some code (in-developement) that parses xml and needs to handle the Open/Closing tag of xml. I'll have a look at it if it might help and post it in case.

 

Felix 

Message 6 of 9
(4,521 Views)
Okay! This looks promising! I will try to code it. Actually what I was looking for was a perfect solution (thinking that maybe I may miss out on some way(s) a user may come up with an invalid nested loops and my code would still pass it and I could get an standard/accepted algorithm for checking such conditions). Anyway, your suggested solutions looks like it (I ran it in my head and it seems to check the invalid loops that I can think of). Kudos to you and a very pleasant day as well. Thanks a lot for helping me out.
0 Kudos
Message 7 of 9
(4,502 Views)

I checked my (unfinished) xml parser. As I'm interested in the contents, things are a bit more complicated and my approach is different than my suggestion above:

 

* From start tag I start the search for the next tag of same name. I use a counter to count the number of starting (+1) and closing tags (-1) until I find the corresponding closing tag (=0).

 

xml Parse.png

 

* The tag ('parent') and the contents are clustered and pushed on the stack and the next iteration I pop the top element from the stack. In my implementation I use an array (I marked it with red circles), but queues work as well. You can also do this with recursion, but it's slower as we debated at one of Darrens nuggets.

 

xml Stack.png

 

I'm planning to publish the code on LAVA once I find the time to cover some more of the xml specs + fix some bugs. If you are interested in the code, you can PM me (blackpearl) on LAVA.

 

Felix

Message 8 of 9
(4,482 Views)
Yes this certainly seems a bit more complex. Your earlier suggestion was used and till I find a way that it can be dodged into passing an invalid nested loop placement, its okay by me. And yes I had also used an array as an stack. Thanks a lot for helping me out. I am indebted to you for your kindness.

May this be a pleasant (and productive) day for you!
0 Kudos
Message 9 of 9
(4,451 Views)