Balancing Symbols Compilers check programs for syntax errors, but frequently a l
ID: 3627600 • Letter: B
Question
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”.
IT WILL BE A LIFESAVER. CODE IN "JAVA" PLEASE
Explanation / Answer
This should work =] import java.util.*; import java.io.*; public class Test { private static char[] stack=new char[100000]; private static int stackPoint=-1; public static void main(String[] args) throws Exception { System.out.println("Enter file name: "); BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); String file=in.readLine(); Scanner scan=new Scanner(new FileReader(file)); out:while (scan.hasNext()) { String stg=scan.nextLine(); for(int k=0; kRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.