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

Language Interpreter JAVA Problem Description: The goal of this exercise is to r

ID: 3868392 • Letter: L

Question

Language Interpreter

JAVA

Problem Description: The goal of this exercise is to reproduce in a limited way, an interpreter for a computer language. The language will be defined as the following:

a. Declaration statements (integer or string only, single-letter names)

b. Character-string literals (Ex: “Jim Scandale”)

c. Simple numeric assignments (Ex: a = b + c,    =,    , *,   /,   %)

Simple character-string assignments (Ex: n = “CSE116” )

d. Read (input one integer value)

e. Display (output integer value or output character-string value)

f. End-statement (‘end’ signals the end of the program)

The program will be typed by the user, a single statement on each line. The interpreter will read the program in this language and store it onto a text file. Variables will be checked against their declaration statements and each use of an undeclared variable will be flagged as an error. When the ‘end’ statement is reached and stored, the interpreter will check to see if there were any errors flagged. If none, it will begin executing the statements a line at a time. If there were errors, the interpreter will display a summary error message (ex: “too many errors – execution deleted”) and go back to reading user input.

The execution phase will

a. Display each statement exactly as supplied by the user.

b. Create an internal variable for each declaration statement

c. Manipulate values as dictated by the program

d. Perform read and display statements

e. When execution reaches the ‘end’ statement, stop!

Explanation / Answer


package chegg;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class SimpleComplierAndExecutor {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
Map<String, Double> st = new HashMap<String, Double>();
char choice = 'y';
Scanner scanner = new Scanner(System.in);
while (choice == 'y') {
String ll = scanner.next();
list.add(ll);
}
for (String line : list) {
String[] tokens = line.split("\s");
// singe variable - just print out its value
if (tokens.length == 1) {
String zvar = tokens[0];
System.out.println(zvar + " := " + st.get(zvar));
}
// z = x
else if (tokens.length == 3) {
String zvar = tokens[0];
String eq = tokens[1];
String xvar = tokens[2];
if (!eq.equals(":="))
throw new RuntimeException("Illegal assignment");
Double x = st.get(xvar);
if (x == null)
x = Double.parseDouble(xvar);
st.put(zvar, x);
System.out.println(zvar + " := " + st.get(zvar));
}
// z = function x
else if (tokens.length == 4) {
String zvar = tokens[0];
String eq = tokens[1];
String func = tokens[2];
String xvar = tokens[3];
if (!eq.equals(":="))
throw new RuntimeException("Illegal assignment");
Double x = st.get(xvar);
if (x == null)
x = Double.parseDouble(xvar);
if (func.equals("sin"))
st.put(zvar, Math.sin(x));
else if (func.equals("cos"))
st.put(zvar, Math.cos(x));
else if (func.equals("sqrt"))
st.put(zvar, Math.sqrt(x));
else if (func.equals("-"))
st.put(zvar, -x);
else
throw new RuntimeException("Illegal function");
System.out.println(zvar + " := " + st.get(zvar));
}
// z = x + y
else if (tokens.length == 5) {
String zvar = tokens[0];
String eq = tokens[1];
String xvar = tokens[2];
String op = tokens[3];
String yvar = tokens[4];
if (!eq.equals(":="))
throw new RuntimeException("Illegal assignment");
Double x = st.get(xvar);
if (x == null)
x = Double.parseDouble(xvar);
Double y = st.get(xvar);
if (y == null)
y = Double.parseDouble(xvar);
if (op.equals("+"))
st.put(zvar, x + y);
else if (op.equals("-"))
st.put(zvar, x - y);
else if (op.equals("*"))
st.put(zvar, x * y);
else if (op.equals("/"))
st.put(zvar, x / y);
else if (op.equals("^"))
st.put(zvar, Math.pow(x, y));
else
throw new RuntimeException("Illegal operator");
System.out.println(zvar + " = " + st.get(zvar));
}
else
throw new RuntimeException("Illegal expression");
System.out.println();
System.out.print(">> ");
}
}
}
class Interpreter {
}
class StatementType {
public final static String DECLARATION_STATEMENT = "DECLARATION_STATEMENT";
public final static String SIMPLE_NUMERIC_ASSIGMENT = "SIMPLE_NUMERIC_ASSIGMENT";
public final static String SIMPLE_CHARACTER_STRING = "SIMPLE_CHARACTER_STRING";
public final static String READ_INPUT_VALUE = "READ_INPUT_VALUE";
public final static String DISPLAY_OUTPUT_VALUE = "DISPLAY_OUTPUT_VALUE";
}