Think about it logically. How do you know if they match up? If you have a closing brace after an opening brace.
Here's a general algorithm:
- If encounter an opening brace "(", add to stack
- If encounter closing brace ")", pop one from the stack
- If you encounter a closing brace, be careful to check the stack size. If nothing is on the stack then that is also an error.
Once text has been parsed, your stack should be empty if there was a matching amount of braces and no errors encountered.