03-23-2010 06:57 AM
Solved! Go to Solution.
03-23-2010 07:01 AM
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
03-23-2010 07:07 AM
03-23-2010 08:13 AM
03-23-2010 08:16 AM
03-23-2010 09:06 AM
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
03-23-2010 11:13 AM
03-23-2010 02:54 PM
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).
* 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.
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
03-24-2010 06:15 AM