Below is Parser.java. There are errors in the code, so please fix them and show the fixed
Question:
Below is Parser.java. There are errors in the code, so please fix them and show the fixed code without any errors along with the screenshot of the code working. Make sure to use a test case to make sure the code works properly. Attached is rubric and images of the error. Don't just copy the code given. I need the fixed code.
Parser.java
import java.text.ParseException;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import javax.swing.ActionMap;
public class Parser {
private TokenHandler tokenHandler;
private LinkedList tokens;
public Parser(LinkedList tokens) {
this.tokenHandler = new TokenHandler(tokens);
this.tokens = tokens;
}
public boolean AcceptSeparators() {
boolean foundSeparator = false;
while (tokenHandler.MoreTokens()) {
Optional currentToken = tokenHandler.getCurrentToken();
if (currentToken.isPresent() && (currentToken.get().getType() == Token.TokenType.NEWLINE || currentToken.get().getType() == Token.TokenType.SEMICOLON)) {
tokenHandler.consumeMatchedToken();
foundSeparator = true;
} else {
break;
}
}
return foundSeparator;
}
public ProgramNode Parse() throws ParseException {
ProgramNode programNode = new ProgramNode(null, 0);
while (tokenHandler.MoreTokens()) {
if (!ParseFunction(programNode) && !parseAction(programNode)) {
throw new ParseException("Unexpected token: " + tokenHandler.getCurrentToken().getStart());
}
}
return programNode;
}
private boolean ParseFunction(ProgramNode programNode) {
Optional functionNameToken = tokenHandler.getCurrentToken();
if (tokenHandler.MatchAndRemove(Token.TokenType.IDENTIFIER) != null) {
FunctionDefinitionNode functionNode = new FunctionDefinitionNode(functionNameToken.map(Token::getType).orElse(null).getStart());
programNode.addNode(functionNode);
if (tokenHandler.MatchAndRemove(Token.TokenType.LPAREN) != null) {
if (tokenHandler.MatchAndRemove(Token.TokenType.RPAREN) != null) {
AcceptSeparators();
return true;
}
}
}
return false;
}
private boolean parseAction(ProgramNode programNode) {
Optional functionNameToken = tokenHandler.getCurrentToken();
if (tokenHandler.MatchAndRemove(Token.TokenType.IDENTIFIER) != null) {
ActionMap actionMap = new ActionMap();
programNode.addNode(actionMap);
AcceptSeparators();
return true;
}
return false;
}
public VariableReferenceNode ParseLValueVariable(String variableName) {
return new VariableReferenceNode(variableName, Optional.empty());
}
public VariableReferenceNode ParseLValueArray(String variableName, int index) {
return new VariableReferenceNode(variableName, Optional.of(index));
}
public OperationNode ParseLValueDollar() {
return new OperationNode("$");
}
public ConstantNode ParseBottomLevelConstantsAndPatterns() {
ConstantNodePatternNode node = new ConstantOrNodePatternNode();
node.setConstant("example");
node.setPattern("PATTERN_1");
return node;
}
public OperationNode ParseBottomLevelParenthesis() throws ParseException {
if (tokenHandler.MatchAndRemove(Token.TokenType.LPAREN) != null) {
OperationNode operationNode = ParseExpression();
if (tokenHandler.MatchAndRemove(Token.TokenType.RPAREN) != null) {
return operationNode;
} else {
throw new ParseException("Expected ')'", tokenHandler.getCurrentToken().getStart());
}
return null;
} else {
throw new ParseException("Expected '('", tokenHandler.getCurrentToken().getStart());
}
}
}
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill