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); } }
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);
}
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images