Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

}

}

}


student submitted image, transcription available belowstudent submitted image, transcription available belowstudent submitted image, transcription available belowstudent submitted image, transcription available belowstudent submitted image, transcription available below   
 
 
 

100 public OperationNode ParseLValue Dollar() { 101 102 Qx103 104 } // You need to implement this logic to create an OperationNode for the $ operator. // Get the value of the $ operator appropriately. return new OperationNode("$"); // Replace with actual implementation. 105 // Method to pa The constructor OperationNode(String) is undefined { nNode(); 106 public Constant 1 quick fix available: 107 108 109 110 111 112 113 } ConstantNod Create constructor 'OperationNode(String)" node.setCon node.setPattern PATTERN_T); // You need to implement this logic to detect strings, numbers, and patterns // and create appropriate nodes. return node; // Replace with actual implementation. 114 public OperationNode ParseBottom LevelParenthesis () throws ParseException { if (tokenHandler.MatchAndRemove (Token. TokenType.LPAREN) != null) { // Parse the expression inside the parentheses recursively OperationNode operationNode = ParseExpression(); // Check for closing parenthesis 115 116 117 118 119 120 121 122 123 124 } 125 126 127 128 129 130 131 132 133 } 134 if (tokenHandler.MatchAndRemove (Token. TokenType.RPAREN) != null) { return operationNode; } else { throw new ParseException("Expected ')'", tokenHandler.getCurrentToken().getStart()); // You need to implement the logic to get the contents of the parenthesis. // Create an OperationNode for the contents. // Handle the closing parenthesis as well. // Example: (expr) return null; // Replace with actual implementation. } else { } throw new ParseException("Expected '('", tokenHandler.getCurrentToken().getStart());

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Income Tax Fundamentals 2013

Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill

31st Edition

1111972516, 978-1285586618, 1285586611, 978-1285613109, 978-1111972516

More Books

Students also viewed these Programming questions