Java Code: Below is Parser.java and there are errors. getType() and getStart() is undefined for the type Optional and there is an error in addNode(). Make sure to get rid of all the errors in the code. Attached is images of the errors. 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.getType() == Token.TokenType.NEWLINE || currentToken.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(), 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.getType()); 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(tokenHandler.getLastMatchedToken().getType()); programNode.addNode(actionMap); AcceptSeparators(); return true; } return false; } }
Java Code: Below is Parser.java and there are errors. getType() and getStart() is undefined for the type Optional<Token> and there is an error in addNode(). Make sure to get rid of all the errors in the code. Attached is images of the errors.
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<Token> tokens;
public Parser(LinkedList<Token> tokens) {
this.tokenHandler = new TokenHandler(tokens);
this.tokens = tokens;
}
public boolean AcceptSeparators() {
boolean foundSeparator = false;
while (tokenHandler.MoreTokens()) {
Optional<Token> currentToken = tokenHandler.getCurrentToken();
if (currentToken.getType() == Token.TokenType.NEWLINE || currentToken.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(), tokenHandler.getCurrentToken().getStart());
}
}
return programNode;
}
private boolean ParseFunction(ProgramNode programNode) {
Optional<Token> functionNameToken = tokenHandler.getCurrentToken();
if (tokenHandler.MatchAndRemove(Token.TokenType.IDENTIFIER) != null) {
FunctionDefinitionNode functionNode = new FunctionDefinitionNode(functionNameToken.getType());
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<Token> functionNameToken = tokenHandler.getCurrentToken();
if (tokenHandler.MatchAndRemove(Token.TokenType.IDENTIFIER) != null) {
ActionMap actionMap = new ActionMap(tokenHandler.getLastMatchedToken().getType());
programNode.addNode(actionMap);
AcceptSeparators();
return true;
}
return false;
}
}
![32
33
340
35
36
37
38
39
40
41
42
43
440
45
46
47
48
49
50
51
52
53
54
55
56
57
58
590
60
61
62
€63
64
65
66
67
68
69}
}
public ProgramNode Parse() throws ParseException {
ProgramNode programNode = new ProgramNode(null, 0);
while (tokenHandler. More Tokens()) {
if (!ParseFunction (programNode) && !parseAction(programNode)) {|
throw new ParseException ("Unexpected token: " + tokenHandler.getCurrentToken(), tokenHandler.ge
}
}
private boolean ParseFunction (ProgramNode programNode) {
Optional<Token > functionNameToken = tokenHandler.getCurrentToken();
if (tokenHandler.MatchAnd Remove(Token. TokenType.IDENTIFIER) != null) {
FunctionDefinitionNode functionNode = new FunctionDefinitionNode (functionName Token.getType());
}
}
return programNode;
}
programNode.addNode(functionNode);
if (tokenHandler.MatchAnd Remove(Token. TokenType.LPAREN) != null) {
if (tokenHandler.MatchAndRemove (Token. TokenType.RPAREN) != null) {
AcceptSeparators();
return true;
}
}
}
return false;
private boolean parseAction (ProgramNode programNode) {
Optional<Token› functionNameToken = tokenHandler.getCurrentToken();
if (tokenHandler.MatchAnd Remove (Token. Token Type. IDENTIFIER) != null) {
ActionMap actionMap = new Action Map (tokenHandler.getLast Matched Token ().getType());
programNode.addNode(actionMap);
AcceptSeparators();
return true;
}
return false;
#mypack
✓ Parser
.Parser(LinkedList<Token>)
AcceptSeparators(): boolean
Parse(): ProgramNode
parseAction(ProgramNode): boolean
ParseFunction(ProgramNode): boolean
The method getType() is undefined for the type Optional <Token>
Press 'F2' for focus](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fe7ddc10c-4670-40fd-b02c-6a60c5fcc2f2%2Fcf9ce616-160d-4498-84c9-fe0303a4f1e3%2F6boee7l_processed.png&w=3840&q=75)
![2
3
40
5
6
7
8
9
0
1
2
3
40
5
6
7
8
9
1
2
3
4
5
6
7
8
ge
0
2
4
6
8
}
public ProgramNode Parse() throws ParseException {
ProgramNode programNode = new ProgramNode (null, 0);
while (tokenHandler. More Tokens ()) {
}
}
if (!ParseFunction (programNode) && !parseAction(programNode)) {|
throw new ParseException ("Unexpected token: " + tokenHandler.getCurrentToken(), tokenHandler.ge
}
private boolean ParseFunction (ProgramNode programNode) {
Optional<Token› functionNameToken = tokenHandler.getCurrentToken();
if (tokenHandler.MatchAnd Remove (Token. TokenType.IDENTIFIER) != null) {
FunctionDefinitionNode functionNode = new FunctionDefinitionNode (functionName Token.getType());
programNode.addNode (functionNode);
}
return programNode;
}
if (tokenHandler.MatchAnd Remove (Token. TokenType.LPAREN) != null) {
if (tokenHandler.MatchAnd Remove (Token. TokenType.RPAREN) != null) {
AcceptSeparators();
return true;
}
}
return false;
private boolean parseAction (ProgramNode programNode) {
Optional<Token> functionNameToken = tokenHandler.getCurrentToken();
if (tokenHandler.MatchAndRemove (Token. TokenType.IDENTIFIER) != null) {
ActionMap actionMap = new ActionMap (tokenHandler.getLastMatchedToken().getType());
programNode.addNode (actionMap);
AcceptSepara
return true;
}
return false;
The method addNode(ActionMap) is undefined for the type ProgramNode
2 quick fixes available:
• Create method 'addNode(Action Map) in type 'ProgramNode'
Add cast to 'programNode'
Press 'F2' for focus](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fe7ddc10c-4670-40fd-b02c-6a60c5fcc2f2%2Fcf9ce616-160d-4498-84c9-fe0303a4f1e3%2Fzkskt47_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
It seems there are some issues in your code, including the undefined getType()
and getStart()
methods for Optional<Token>
. The errors you are encountering are because you are trying to access methods on an Optional<Token>
object directly. You should first check if the Optional
contains a value using .isPresent()
and then retrieve the value using .get()
before calling any methods on it
I've updated the code to check for the presence of the Optional<Token>
using isPresent()
and use .get()
and .map()
to access the TokenType
and Start
properties when necessary. Make sure that the Token
class has appropriate getType()
and getStart()
methods defined for this code to work correctly.
Step by step
Solved in 3 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)