Java Programming: Below is my parser with the different nodes.java. There are errors in the parser. Please fix those errors and attached is the image of the output the parser must produce.  Parser.java import java.util.List; public class Parser {    private final List tokens;    private int current = 0;    public Parser(List tokens) {        this.tokens = tokens;    }    private Token matchAndRemove(TokenType type) {        if (isAtEnd()) return null;        Token token = tokens.get(current);        if (token.getType() == type) {            current++;            return token;        }        return null;    }    private void expectEndsOfLine() {        while (matchAndRemove(TokenType.END_OF_LINE) != null);        if (current >= tokens.size()) {            throw new SyntaxErrorException("Unexpected end of input");        }    }    private Token peek(int n) {        if (current + n >= tokens.size()) {            return null;        }        return tokens.get(current + n);    }    private boolean isAtEnd() {        return peek(0) == null;    }    public Node parse() {        Node expr = expression();        expectEndsOfLine();        while (!isAtEnd()) {            System.out.println(expr.toString());            expr = expression();            expectEndsOfLine();        }        return expr;    }    private Node expression() {        Node left = term();        while (matchAndRemove(TokenType.PLUS, TokenType.MINUS) != null) {            Token operator = tokens.get(current - 1);            Node right = term();            left = new BinaryExprNode(left, operator, right);        }        return left;    }    private Node term() {        Node left = factor();        while (matchAndRemove(TokenType.MULTIPLY, TokenType.DIVIDE) != null) {            Token operator = tokens.get(current - 1);            Node right = factor();            left = new BinaryExprNode(left, operator, right);        }        return left;    }    private Node factor() {        if (matchAndRemove(TokenType.NUMBER) != null) {            return new NumberNode(tokens.get(current - 1));        }        if (matchAndRemove(TokenType.LEFT_PAREN) != null) {            Node expr = expression();            matchAndRemove(TokenType.RIGHT_PAREN);            return expr;        }        throw new SyntaxErrorException("Expected a number or parentheses");    } }   IntegerNode.java package mypack; public class IntegerNode extends Node{ private int value;          public IntegerNode(int value) {         this.value = value;     }          public int getValue() {         return value;     }          public String ToString() {         return Integer.toString(value);     } } MathOpNode.java package mypack; public class MathOpNode extends Node {     private Node left;     private Node right;     private MathOp operation;          public MathOpNode(Node left, Node right, MathOp operation) {         this.left = left;         this.right = right;         this.operation = operation;     }          public Node getLeft() {         return left;     }          public Node getRight() {         return right;     }          public MathOp getOperation() {         return operation;     }          public String ToString() {         return "(" + left.ToString() + " " + operation + " " + right.ToString() + ")";     }          public enum MathOp {         ADD,         SUBTRACT,         MULTIPLY,         DIVIDE,         MODULUS     } } Node.java package mypack; public abstract class Node {     public abstract String ToString(); } RealNode.java package mypack; public class RealNode extends Node { private float value;          public RealNode(float value) {         this.value = value;     }          public float getValue() {         return value;     }          public String ToString() {         return Float.toString(value);     } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Java Programming: Below is my parser with the different nodes.java. There are errors in the parser. Please fix those errors and attached is the image of the output the parser must produce. 

Parser.java

import java.util.List;

public class Parser {
   private final List<Token> tokens;
   private int current = 0;

   public Parser(List<Token> tokens) {
       this.tokens = tokens;
   }

   private Token matchAndRemove(TokenType type) {
       if (isAtEnd()) return null;
       Token token = tokens.get(current);
       if (token.getType() == type) {
           current++;
           return token;
       }
       return null;
   }

   private void expectEndsOfLine() {
       while (matchAndRemove(TokenType.END_OF_LINE) != null);
       if (current >= tokens.size()) {
           throw new SyntaxErrorException("Unexpected end of input");
       }
   }

   private Token peek(int n) {
       if (current + n >= tokens.size()) {
           return null;
       }
       return tokens.get(current + n);
   }

   private boolean isAtEnd() {
       return peek(0) == null;
   }

   public Node parse() {
       Node expr = expression();
       expectEndsOfLine();
       while (!isAtEnd()) {
           System.out.println(expr.toString());
           expr = expression();
           expectEndsOfLine();
       }
       return expr;
   }

   private Node expression() {
       Node left = term();

       while (matchAndRemove(TokenType.PLUS, TokenType.MINUS) != null) {
           Token operator = tokens.get(current - 1);
           Node right = term();
           left = new BinaryExprNode(left, operator, right);
       }

       return left;
   }

   private Node term() {
       Node left = factor();

       while (matchAndRemove(TokenType.MULTIPLY, TokenType.DIVIDE) != null) {
           Token operator = tokens.get(current - 1);
           Node right = factor();
           left = new BinaryExprNode(left, operator, right);
       }

       return left;
   }

   private Node factor() {
       if (matchAndRemove(TokenType.NUMBER) != null) {
           return new NumberNode(tokens.get(current - 1));
       }

       if (matchAndRemove(TokenType.LEFT_PAREN) != null) {
           Node expr = expression();
           matchAndRemove(TokenType.RIGHT_PAREN);
           return expr;
       }

       throw new SyntaxErrorException("Expected a number or parentheses");
   }
}
 

IntegerNode.java

package mypack;

public class IntegerNode extends Node{
private int value;
    
    public IntegerNode(int value) {
        this.value = value;
    }
    
    public int getValue() {
        return value;
    }
    
    public String ToString() {
        return Integer.toString(value);
    }
}

MathOpNode.java

package mypack;

public class MathOpNode extends Node {
    private Node left;
    private Node right;
    private MathOp operation;
    
    public MathOpNode(Node left, Node right, MathOp operation) {
        this.left = left;
        this.right = right;
        this.operation = operation;
    }
    
    public Node getLeft() {
        return left;
    }
    
    public Node getRight() {
        return right;
    }
    
    public MathOp getOperation() {
        return operation;
    }
    
    public String ToString() {
        return "(" + left.ToString() + " " + operation + " " + right.ToString() + ")";
    }
    
    public enum MathOp {
        ADD,
        SUBTRACT,
        MULTIPLY,
        DIVIDE,
        MODULUS
    }
}

Node.java

package mypack;

public abstract class Node {
    public abstract String ToString();

}

RealNode.java

package mypack;

public class RealNode extends Node {
private float value;
    
    public RealNode(float value) {
        this.value = value;
    }
    
    public float getValue() {
        return value;
    }
    
    public String ToString() {
        return Float.toString(value);
    }
}

 

 

The output of the above code would be:
Multiplication Node
Left: AdditionNode
Left: NumberNode (value=2)
Right: NumberNode (value=3)
Right: NumberNode(value=4)
EndOfLineNode
This output corresponds to the following abstract syntax tree:
2 3
Transcribed Image Text:The output of the above code would be: Multiplication Node Left: AdditionNode Left: NumberNode (value=2) Right: NumberNode (value=3) Right: NumberNode(value=4) EndOfLineNode This output corresponds to the following abstract syntax tree: 2 3
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Knowledge Booster
Operations of Linked List
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education