private List tokens; private int currentTokenIndex; public Parser(List tokens) { this.tokens = tokens; this.currentTokenIndex = 0; } public Node parse() throws SyntaxErrorExceptio
Java Programming: Below is the parser,java and there are errors in the getValue(). Please fix those errors. Attached is the rubric and image of the output.
Parser.java
package mypack;
import java.util.List;
import mypack.Token.TokenType;
public class Parser {
private List<Token> tokens;
private int currentTokenIndex;
public Parser(List<Token> tokens) {
this.tokens = tokens;
this.currentTokenIndex = 0;
}
public Node parse() throws SyntaxErrorException {
Node result = null;
while (peek() != null) {
Node expr = expression();
expectEndOfLine();
System.out.println(expr.toString());
}
return result;
}
private Node expression() {
Node result = term1();
while (peek() != null && (peek().getTokenType() == TokenType.PLUS || peek().getTokenType() == TokenType.MINUS)) {
Token token = matchAndRemove(peek().getTokenType());
Node right = term1();
if (right == null) {
throw new SyntaxErrorException("Expected right operand after " + token.getValue());
}
result = new MathOpNode(token.getTokenType(), result, right);
}
return result;
}
private Node term1() {
Node result = factor();
while (peek() != null && (peek().getTokenType() == TokenType.MULTIPLY || peek().getTokenType() == TokenType.DIVIDE || peek().getTokenType() == TokenType.MODULUS)) {
Token token = matchAndRemove(peek().getTokenType());
Node right = factor();
if (right == null) {
throw new SyntaxErrorException("Expected right operand after " + token.getValue());
}
result = new MathOpNode(token.getTokenType(), result, right);
}
return result;
}
private Node term() {
Node result = factor();
while (peek() != null && (peek().getTokenType() == TokenType.MULTIPLY || peek().getTokenType() == TokenType.DIVIDE || peek().getTokenType() == TokenType.MODULUS)) {
Token token = matchAndRemove(peek().getTokenType());
Node right = factor();
if (right == null) {
throw new SyntaxErrorException("Expected right operand after " + token.getValue());
}
result = new MathOpNode(token.getTokenType(), result, right);
}
return result;
}
private Node factor() {
Token token = matchAndRemove(TokenType.MINUS);
Node result = null;
if (peek() != null) {
if (peek().getTokenType() == TokenType.NUMBER) {
Token numberToken = matchAndRemove(TokenType.NUMBER);
if (token != null) {
result = new IntegerNode(-Integer.parseInt(numberToken.getValue()));
} else {
result = new IntegerNode(Integer.parseInt(numberToken.getValue()));
}
} else if (peek().getTokenType() == TokenType.LEFT_PAREN) {
matchAndRemove(TokenType.LEFT_PAREN);
result = expression();
expect(TokenType.RIGHT_PAREN);
}
}
return result;
}
private Token matchAndRemove(TokenType type) {
if (peek() != null && peek().getTokenType() == type) {
currentTokenIndex++;
return tokens.get(currentTokenIndex - 1);
}
return null;
}
private void expect(TokenType type) throws SyntaxErrorException {
Token token = matchAndRemove(type);
if (token == null) {
throw new SyntaxErrorException();
}
}
private void expectEndOfLine() throws SyntaxErrorException {
expect(TokenType.ENDOFLINE);
}
private Token peek() {
return currentTokenIndex < tokens.size() ? tokens.get(currentTokenIndex) : null;
}
}



here is a modified version of the Parser.java code that fixes some syntax errors and provides a working implementation:
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images









