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);     } }

Programming Logic & Design Comprehensive
9th Edition
ISBN:9781337669405
Author:FARRELL
Publisher:FARRELL
Chapter3: Understanding Structure
Section: Chapter Questions
Problem 7RQ
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
  • SEE MORE QUESTIONS
Recommended textbooks for you
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
CMPTR
CMPTR
Computer Science
ISBN:
9781337681872
Author:
PINARD
Publisher:
Cengage