i need help with my java program. Its having trouble running and I need help making sure it works. Please post the results and output for it. Heres my code and below are also what the program is supposed to do. import java.util.*; public class SimpleCalculator {     private final String input;     private int position;     private boolean hasDivByZero = false;     private Map symbolTable;     private List instructions;     public SimpleCalculator(String input) {         this.input = input;         this.position = 0;         this.symbolTable = new HashMap<>();         this.instructions = new ArrayList<>();     }     public static void main(String[] args) {         SimpleCalculator calculator = new SimpleCalculator("3 + 5 * (2 - 1)");         calculator.parseExpression();         if (calculator.hasDivByZero) {             System.out.println("Error: division by zero");         } else {             System.out.println("Result: " + calculator.instructions);         }     }     public void parseExpression() {         parseTerm('+');     }     public String parseTerm(char op) {         String t1 = parseFactor();         while (peek() == op || peek() == '/') {             char operator = consume();             String t2 = parseFactor();             if (operator == '/' && t2.equals("0")) {                 hasDivByZero = true;                 break;             }             String result = newTemp();             emit(result + " = " + t1 + " " + operator + " " + t2);             t1 = result;         }         return t1;     }     public String parseFactor() {         if (consume('(')) {             String result = parseExpression();             consume(')');             return result;         }         StringBuilder sb = new StringBuilder();         while (Character.isDigit(peek())) {             sb.append(consume());         }         String variable = sb.toString();         Optional.ofNullable(symbolTable.get(variable))                 .filter(type -> type.equals("int"))                 .orElseThrow(() -> new IllegalArgumentException("Error: " + variable + " is not defined or not an integer"));         return variable;     }     private String newTemp() {         String temp = "t" + (instructions.size() + 1);         symbolTable.put(temp, "int");         return temp;     }     private void emit(String instruction) {         instructions.add(instruction);     }     private char consume() {         return input.charAt(position++);     }     private boolean consume(char c) {         if (peek() == c) {             position++;             return true;         }         return false;     }     private char peek() {         return position < input.length() ? input.charAt(position) : '\0';     } }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

i need help with my java program. Its having trouble running and I need help making sure it works. Please post the results and output for it.

Heres my code and below are also what the program is supposed to do.
import java.util.*;

public class SimpleCalculator {

    private final String input;
    private int position;
    private boolean hasDivByZero = false;
    private Map<String, String> symbolTable;
    private List<String> instructions;

    public SimpleCalculator(String input) {
        this.input = input;
        this.position = 0;
        this.symbolTable = new HashMap<>();
        this.instructions = new ArrayList<>();
    }

    public static void main(String[] args) {
        SimpleCalculator calculator = new SimpleCalculator("3 + 5 * (2 - 1)");
        calculator.parseExpression();
        if (calculator.hasDivByZero) {
            System.out.println("Error: division by zero");
        } else {
            System.out.println("Result: " + calculator.instructions);
        }
    }

    public void parseExpression() {
        parseTerm('+');
    }

    public String parseTerm(char op) {
        String t1 = parseFactor();

        while (peek() == op || peek() == '/') {
            char operator = consume();
            String t2 = parseFactor();

            if (operator == '/' && t2.equals("0")) {
                hasDivByZero = true;
                break;
            }

            String result = newTemp();
            emit(result + " = " + t1 + " " + operator + " " + t2);
            t1 = result;
        }

        return t1;
    }

    public String parseFactor() {
        if (consume('(')) {
            String result = parseExpression();
            consume(')');
            return result;
        }

        StringBuilder sb = new StringBuilder();
        while (Character.isDigit(peek())) {
            sb.append(consume());
        }

        String variable = sb.toString();

        Optional.ofNullable(symbolTable.get(variable))
                .filter(type -> type.equals("int"))
                .orElseThrow(() -> new IllegalArgumentException("Error: " + variable + " is not defined or not an integer"));

        return variable;
    }

    private String newTemp() {
        String temp = "t" + (instructions.size() + 1);
        symbolTable.put(temp, "int");
        return temp;
    }

    private void emit(String instruction) {
        instructions.add(instruction);
    }

    private char consume() {
        return input.charAt(position++);
    }

    private boolean consume(char c) {
        if (peek() == c) {
            position++;
            return true;
        }
        return false;
    }

    private char peek() {
        return position < input.length() ? input.charAt(position) : '\0';
    }
}

A. Basic requirement: Designing a compiler for a Simple Programming Language involves
creating a software system that can translate code written in the Simple Programming Language
(SPL) into machine code that can be executed on a computer. The project would involve:
1. Defining the syntax of the SPL, including its basic data types, control structures, and
functions.
2. Implementing a lexical analyzer to tokenize the SPL source code into a stream of tokens.
3. Implementing a parser to construct an abstract syntax tree (AST) from the token stream
that represents the structure of the program.
4. Implementing a semantic analyzer to check for type errors, undefined variables, and other
semantic errors in the program.
5.
Generating intermediate code from the AST, such as three-address code or bytecode.
6. Optimizing the intermediate code to improve the efficiency of the generated machine code.
7. Generating the final machine code, such as x86 assembly language, that can be executed
on a computer.
Transcribed Image Text:A. Basic requirement: Designing a compiler for a Simple Programming Language involves creating a software system that can translate code written in the Simple Programming Language (SPL) into machine code that can be executed on a computer. The project would involve: 1. Defining the syntax of the SPL, including its basic data types, control structures, and functions. 2. Implementing a lexical analyzer to tokenize the SPL source code into a stream of tokens. 3. Implementing a parser to construct an abstract syntax tree (AST) from the token stream that represents the structure of the program. 4. Implementing a semantic analyzer to check for type errors, undefined variables, and other semantic errors in the program. 5. Generating intermediate code from the AST, such as three-address code or bytecode. 6. Optimizing the intermediate code to improve the efficiency of the generated machine code. 7. Generating the final machine code, such as x86 assembly language, that can be executed on a computer.
Expert Solution
steps

Step by step

Solved in 4 steps with 5 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY