I dont know what im doing at all with this assignment Help w
I dont know what i\'m doing at all with this assignment! Help with this please! You can upload either the entire code or just the text for each class in comment box. ;) thanks
ng languages, the compiler carries a preprocessing step to determine if certain In most programmi statements will compile. For instance it may check to see if parentheses match. Write a Java program that simulates the actions of a preprocessor, to detect if certain Java constructs are syntactically correct. Table 1 shows the types of Java statement formats under consideration, and also example of each statement. Table 1 Format Statement Method 252 Example int x = 3 + (10-4)*(10 +4) public void display(int n) data-type= expression Table 2 shows the delimiters under consideration. rt name(^parameter>) Table 2 statement int arrl ] new int[n]; Delimiters Symbol System.out.prindn( x[2]); Left parenthesis Right parenthesis Left curly braces Right curly braces Left square brackets Right square brackets Forward slash Star (multiplication symbol) class class Name public class MyParser dt fields; public static void main(String [] arg Kattrib> Name() Note: In your implementation, design a class called Preprocessor that accepts a file that represents the program to be analyzed. The class will contain, among other possible methods, a method that determines whether or not the statement in the file are valid, with respect to the delimiters of Table 2. Do not be concerned with other symbols display (10); method (); You will need a test class. You may want to name it MyPreprocessor You may have to enter all statements on a single line, unless you will be reading the input from a file, in which case the file would be read using presumable the class BufferedReader or LineNumberReader, Your output would echo the input, and say whether or not the input passed the preprocessing tage. You are to use the concept of stack to determine if the constructs are svntactically correct. 1. static void display(int x) rt method() 2. 3. My pre-processor 4.Solution
Preprocessor.java
import java.util.*;
import java.io.*;
public class Preprocessor{
Stack preprocessor;
String file;
public Preprocessor(String file){
preprocessor = new Stack();
this.file = file;
}
public boolean check(){
try {
FileInputStream fis = new FileInputStream(this.file);
char c;
while (fis.available() > 0) {
c = (char) fis.read();
if (c == \'{\' || c == \'(\' || c == \'[\'){
this.preprocessor.push(c);
}
else if(c == \'}\'){
if ((char)this.preprocessor.peek() != \'{\'){
return false;
}
else{
preprocessor.pop();
}
}
else if(c == \')\'){
if ((char)this.preprocessor.peek() != \'(\'){
return false;
}
else{
preprocessor.pop();
}
}
else if(c == \']\'){
if ((char)this.preprocessor.peek() != \'[\'){
return false;
}
else{
preprocessor.pop();
}
}
}
} catch (IOException e) {
System.out.println(\"Could not read the file.\");
return false;
}
if(!preprocessor.empty()){
return false;
}
return true;
}
}
MyPreprocessor.java
public class MyPreprocessor{
public static void main(String[] args) {
String file = \"test.java\";
Preprocessor preprocessor = new Preprocessor(file);
if (preprocessor.check()){
System.out.println(\"The input passed the preprocessing.\");
}
else{
System.out.println(\"The input did not pass the preprocessing.\");
}
}
}

