Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objective Write a class called Parser which will be responsible for parsing comp

ID: 3758446 • Letter: O

Question

Objective

Write a class called Parser which will be responsible for parsing compound s-expressions into atomic s-expressions.

Your class must do the following:

Define the following non-static methods:

getSexp Finds the next atomic s-expression in the file.

params

repl: An instance of a class implementing Repl. You should have already completed this in Lab 4, so you may use the class you have written for that assignment.

scan: A Scanner over some s-expressions. We will be using a Scanner(File) for this assignment.

parenCount: The (integer) number of unmatched parentheses encountered so far.

return An atomic s-expression as a String

A main method that asks the user to enter a file to parse, then calls getSexp on a Scanner over that file.

Hints

A good strategy is to parse the file character by character, and build up the s-expression as a String. Done this way, you have 4 cases to consider:

The character is '('
In this case you have found a nested s-expression, so you should make a recursive call to getSexp. Make sure to update the parameters as appropriate.

The character is ')'
In this case you are closing off the s-expression. Do any necessary updates to the string and pass off the s-expr to your eval method from Lab 4's Lisp class. Then return this result.

The character is whitespace (' ', ' ', or ' ')
You may want to make it easy on your read and convert all whitespace to a single space for ease of parsing later.

Otherwise
Add the current character to the s-expression you are building

In addition, you might find the following methods useful:

«scanner».useDelimiter("")
Force the scanner to have no delimiter, so every call to next returns only the next character.

«string».replaceAll(" +", " ")
Replace multiple instances of a space with a single space in the given string.

«string».replaceAll(" \)", "\)")
Replace the sequence ' )' with a single closing bracket ')' in the given string.

«string».replaceAll("\( ", "\(")
Replace the sequence '( ' with a single closing bracket '(' in the given string.

Example Dialogue

Explanation / Answer

import java.util.stackData;

public class calculateLisp {

stackData<String> stackData;
  
public calculateLisp(){
String[] tokenens = new String[]{"+","1","2","3"};
stackData = new stackData<String>();
for (int i=0;i<tokenens.length;i++){
stackData.push(tokenens[i]);
if(tokenens[i].equals(")")) Interprete();
}
}
  
public void Interprete(){
String token;
stackData<String> handleStack = new stackData<String>();
token = stackData.pop(); //handling ')' character
while(!(token=stackData.pop()).equals("(")){
handleStack.push(token);
}
Call(handleStack);
}
  
public void Call(stackData<String> handleStack){
String func = handleStack.pop(); // popping operator
if(func.equals("+")) {
double result = Plus(handleStack);
stackData.push(String.valueOf(result));
}
else if(func.equals("-")) {
double result = Minus(handleStack);
stackData.push(String.valueOf(result));
}
else if(func.equals("*")) {
double result = multiplication(handleStack);
stackData.push(String.valueOf(result));
}
else if(func.equals("/")) {
double result = division(handleStack);
stackData.push(String.valueOf(result));
}
  
}
  
//handling plus operator function
public double Plus(stackData<String> handleStack){
double a = Double.parseDouble(handleStack.pop());
double b = Double.parseDouble(handleStack.pop());
System.out.println("Result is "+(a+b));
return(a+b);
}

//handling minus operator function
public double Minus(stackData<String> handleStack){
double a = Double.parseDouble(handleStack.pop());
double b = Double.parseDouble(handleStack.pop());
System.out.println("Result is "+(a-b));
return(a-b);
}

//handling multiplication operator function
public double multiplication(stackData<String> handleStack){
double a = Double.parseDouble(handleStack.pop());
double b = Double.parseDouble(handleStack.pop());
System.out.println("Result is "+(a*b));
return(a*b);
}

//handling division operator function
public double division(stackData<String> handleStack){
double a = Double.parseDouble(handleStack.pop());
double b = Double.parseDouble(handleStack.pop());
System.out.println("Result is "+(a/b));
return(a/b);
}
  
public static void main(String[] args) {
new calculateLisp();
}
}