Quote Originally Posted by Eri523 View Post
Maybe I misunderstood your approach, but wouldn't that let something like ([)] pass?
You are right, but:
Quote Originally Posted by MrViggy View Post
For #3, using three stacks (one for each type), when you see an "open", push it onto the appropriate stack. When you see a close, pop from the appropriate stack. If (a) you try to pop an empty stack, then you've seen a mismatch; (b) if you're all done, and any of the stacks still have items in them, you've seen a mismatch.
If you add one more rule to that, then it should work - the rule being:
(assuming you keep track of the type of the last symbol) if you detect a "close" of a different type, then you also have a mismatch.

I'm not sure if I'm right... Darn! Where's a compiler expert when you need one?

Quote Originally Posted by Eri523 View Post
I'm also not sure whether the OP described the mathematical rules for the brackets correctly in post #3. Consulting my math lexicon I was surprised that I didn't find any rules about that at all. I also found nothing concrete on Wikipedia but maybe I didn't look close enough yet again... But thinking back to my math lessons in school, I think I remember I was taught the hierarchy {[()]}, used inside out, i.e. the first one to consider would be (), and of course with the ability to enter and leave any nesting level any number of times. But what if I have more that three levels of nesting?
I think that what the OP meant by verifying the "correct mathematical sequence" is that the algorithm should verify the syntax, and that "({[]{}()})" was just an example (sample). I don't think that mathematics defines a hierarchical arrangement for these. I guess it just became an unwritten rule, with the informal hierarchy based on the difficulty to write down each of the symbols using a pen (or a chalk ). Anyway, just like in programming, it is perfectly valid in math to have as many levels of nesting as you desire using only '(' and ')'. And the informal rule can be broken: consider an order pair where the two elements are sets. The standard notation for an ordered pair is [x, y]. This is an ordered pair of sets: [{s, e, t, _, A}, {s, e, t, _, B}].