Download C Sc 227 Linked Stack and Balanced Symbols

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
C Sc 227 Linked Stack and Balanced Symbols
Given the following interface, code demo a program that reports errors about balanced symbol checking.
public interface OurStack<E> {
public boolean isEmpty();
// Return true if there are 0 elements
public void push(E element); // Put element on "top" of this Stack object.
public E peek(); // Return reference to top or throw new EmptyStackException()
public E pop();
// Remove and return element at top or throw EmptyStackException
}
In teams of two
Assuming LinkedStack<E> implements OurStack<E> write a main method that reads in input file
representing a Java program with unbalanced symbols and uses this algorithm to look for and report and
unbalanced symbol errors. The beginning is given in case you forgot how to read characters in from an
input file (it makes one big string first, so you can look at each character later one by one. Feel free to
write your own unique compiletime errors.
1. Make an empty stack named s
2. For each character in the input file
if it's an opening symbol
push it onto the stack
else if it is a closing symbol && the stack is empty
report error
otherwise
pop the stack
if symbol is not a closer for pop's value, report error
3. At end of file, if the stack is not empty, report error for every element still on the stack
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class BalancedSymbolChecker {
Output
import
import
import
public class BalancedSymbolChecker {
public static void main(String[] args) {
Scanner input = null;
String fileName = "BalancedSymbolChecker.java";
try {
input = new Scanner(new File(fileName));
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
public static void main(String[] args) {
Scanner input = null;
String fileName = "BalancedSymbolChecker.java";
try {
input = new Scanner(new File(fileName));
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
String allText = "";
while (input.hasNextLine()) {
allText += input.nextLine() + "\n";
}
from file to the right:
java.io.File;
java.io.FileNotFoundException;
java.util.Scanner;
String allText = "";
while (input.hasNextLine()) {
allText += input.nextLine() + "\n";
}
System.out.println(allText);
OurStack<Character> my_chars;
char currentChar;
int lineNumber;
int errors;
}
}
System.out.println(allText);
OurStack<Character> my_chars;
char currentChar;
int lineNumber;
int errors;
// TODO:Complete this main method to print valanced symbol errors in the input file
}
}