Question
Balanced Parentheses, Curly Braces, and Square Brackets A sequence of characters contains balanced parentheses if the left - and right -parenthesis characters contribute to the
Balanced Parentheses, Curly Braces, and Square Brackets
A sequence of characters contains balanced parentheses if the left- and right-parenthesis characters contribute to the formation of valid mathematical expressions, such as (x+(y/z))*2. The JAVA language requires this, and any valid JAVA compiler enforces it. Other delimiters, such as curly braces { } and square brackets [ ] are also required to be balanced in a valid JAVA program.
In this project, we write a program which checks if a text file is balanced with respect to three types of
Delimiters: parentheses, curly braces, and square brackets. The user executes a load command, which reads the contents of a file into an ArrayList of String objects. (Each line of the text file becomes one member of the ArrayList.) The contents of the file may include curly braces { }, parentheses ( ), and/or square brackets [ ]. The task of our program is to determine if that text is correctly balanced.
The rules for balancing the delimiters are:
Each left delimiter is matched with a subsequent right delimiter of the same type.
Each right delimiter is matched with a preceding left delimiter of the same type.
If there are multiple pairs of delimiters, then the resulting blocks of text must be either
totally disjoint (not overlapping), or
one block of text is completely nested within the other.
The table below lists several examples. (Notice that the left- and right-delimiters are not necessarily on the same line of text.)
Example | Balanced or Not Balanced | Remark |
abc(ee)gg g[ijk]pqr | Balanced | The parentheses sequence and the square brackets sequence are disjoint (they do not overlap). |
abc([d ef]ghi) | Balanced | The square brackets sequence is nested within the parentheses sequence. (In this example, the left and right square bracket characters are not on the same line of text.) |
{[]} | Balanced | The square brackets sequence is nested within the curly braces sequence. |
123[xy z{a]b}cd | Not Balanced | The square brackets sequence overlaps the curly braces sequence. |
)( | Not Balanced | The left- and right-parenthesis characters are in the wrong order. |
User Commands
The program must support the following interactive user commands:
Command | Parameters(s) | Description |
help |
| Output help text. |
load | filename | Read input file into memory. |
|
| Output memory data to console. |
scan |
| Scan the data in memory checking for balance. |
verbose |
| Turn VERBOSE mode ON or OFF (optional feature). |
q |
| exit the program. |
Sample Input and Output
Initial testing may be made easier by using sample files provided with the project assignment: match.txt, mismatch.txt, missingLeftValue.txt and missingRightValue.txt. These are shown below, with the corresponding results.
match.txt |
x = (a + b) (
) { [( )] } |
mismatch.txt |
x = (a + b)
( ) { [( } )] } |
missingLeftValue.txt |
x = (a + b) ) { ( )] } |
missingRightValue.txt |
x = (a + b) ( ) { [( ) |
Sample output from match.txt |
Command: load match.txt 8 records loaded. Command: print 1: x = (a + b) 2: ( 3: 4: ) 5: { 6: [( 7: )] 8: } Command: scan MATCH: ')' at line 1, position 10 balances '(' at line 1, position 4. MATCH: ')' at line 4, position 8 balances '(' at line 2, position 0. MATCH: ')' at line 7, position 8 balances '(' at line 6, position 1. MATCH: ']' at line 7, position 9 balances '[' at line 6, position 0. MATCH: '}' at line 8, position 0 balances '{' at line 5, position 0. 0 error(s) found. Command: |
Sample output from mismatch.txt |
Command: load mismatch.txt 7 records loaded. Command: print 1: x = (a + b) 2: 3: ( ) 4: { 5: [( 6: } )] 7: } Command: scan MATCH: ')' at line 1, position 10 balances '(' at line 1, position 4. MATCH: ')' at line 3, position 9 balances '(' at line 3, position 0. MISMATCH: '}' at line 6, position 5 does NOT balance '(' at line 5, position 1, expected=')'. MATCH: ')' at line 6, position 9 balances '(' at line 5, position 1. MATCH: ']' at line 6, position 10 balances '[' at line 5, position 0. MATCH: '}' at line 7, position 0 balances '{' at line 4, position 0. 1 error(s) found. Command: |
Sample output from missingLeftValue.txt |
Command: load missingLeftValue.txt 6 records loaded. Command: print 1: x = (a + b) 2: ) 3: { 4: ( 5: )] 6: } Command: scan MATCH: ')' at line 1, position 10 balances '(' at line 1, position 4. ERROR: stack is empty, but input data=')' at line 2, position 8. MATCH: ')' at line 5, position 8 balances '(' at line 4, position 0. MISMATCH: ']' at line 5, position 9 does NOT balance '{' at line 3, position 0, expected='}'. MATCH: '}' at line 6, position 0 balances '{' at line 3, position 0. 2 error(s) found. Command: |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started