Balancing Parentheses
Write a program that reads a line of text from the terminal and checks whether the parentheses in the line are balanced.
Notes
- The program should distinguish among three types of parentheses, curly braces { }, brackets [ ], and plain parentheses ( ).
- Parentheses checking could be thought of as working from the inside of nested parentheses to the outside.
- In each case, the appropriate right parenthesis should be matched with a left parenthesis of the same type.
Examples
- ( these { parentheses[] match } perfectly ) !!!
- (the {right [parentheses ) on } this line ] are in the wrong order.
- this ( line { is missing } one (round ) right parenthesis.
Strategy
This problem can be solved reasonably easily using a single left-to-right scan of a line, if left parentheses are placed on a stack as they are encountered. Then, when a right parenthesis is found, the stack can be popped and one can check whether the right parenthesis has the same type as the left one.
Programming note
Your program should use a self-contained list-based stack library package, as developed in the lab on stacks.
