Balancing Symbols Compilers check programs for syntax errors, but frequently a lack of one symbol (such as a missing brace or comment starter) will cause the compiler to spill out a hundred lines of...

Balancing Symbols Compilers check programs for syntax errors, but frequently a lack of one symbol (such as a missing brace or comment starter) will cause the compiler to spill out a hundred lines of diagnostics without identifying the real error. A program that is useful in such situations is one that checks whether everything is balanced. This means that every right brace, bracket, parenthesis and comment delimiter must correspond to its left counterpart. Some legal sequences are /**/{([()])[[()]]} {{([])}} and some illegal sequences are • ()[]/* • [(]) In this project, you are required to write a Java program that checks whether the source code of a given Java, C or C++ program is balanced or not. The operation of your program must be like the following: 1. The program starts by asking the name of the source file to check. The .java, .c or .cpp extension will be input by the user (i.e. “myprog.java”). 2. Your program checks if the symbols in the program is balanced and outputs “yes” if it is and “no” otherwise (to the screen). 3. The program asks if the user wants to check another source file and goes to step 1 if the answer is “yes”. 4. The program quits if the answer to the question at step 3 is “no”. The list of matching symbol pairs that your program has to act on is the following pairs: • { } • ( ) • [ ] • /* */ (Two characters identify a single symbol) You have to implement and use the STACK ADT (do not use the built in stack class of Java) in coming up with a solution to this project. The algorithm is easy: 1. Make an empty stack. 2. Read characters (or a pair of characters) until end of file. (You can use read method of FileInputStream class or any other way you are familiar with.) a) If the character(s) is an opening symbol (such as a left brace), push it onto the stack. b) If it is a closing symbol, then if the stack is empty report an error (say “no”). c) Otherwise pop the stack. i. If the symbol popped is not the corresponding opening symbol, then report an error (say “no”). 3. At end of file, if the stack is not empty report an error (say “no”), otherwise say “yes”.
May 19, 2022
SOLUTION.PDF

Get Answer To This Question

Submit New Assignment

Copy and Paste Your Assignment Here