I got errors in parser.java. How to create methods in TokenHandler.java for getCurrentToken(), consumeToken(), getLastMatchedToken()? Please create these methods in TokenHandler.java. Fix the error for programNode that is attached. Below is TokenHandler.java TokenHandler.java import java.util.LinkedList; import java.util.Optional; import mypack.Token.TokenType; public class TokenHandler { private LinkedList tokens;// List of tokens public TokenHandler(LinkedList tokens) { this.tokens = tokens; } public Optional Peek(int j) { if (j < tokens.size()) { return Optional.of(tokens.get(j)); } else { return Optional.empty(); } } public boolean MoreTokens() { return !tokens.isEmpty(); } public Optional MatchAndRemove(TokenType t) { if (MoreTokens() && Peek(0).get().getType() == t) { return Optional.of(tokens.remove(0)); // Remove and return a token of specified type } else { return Optional.empty(); } } }
I got errors in parser.java. How to create methods in TokenHandler.java for getCurrentToken(), consumeToken(), getLastMatchedToken()? Please create these methods in TokenHandler.java. Fix the error for programNode that is attached. Below is TokenHandler.java
TokenHandler.java
To create the methods getCurrentToken(), consumeToken(), and getLastMatchedToken() in TokenHandler.java, you can modify the class as follows:
import java.util.LinkedList;
import java.util.Optional;
import mypack.Token.TokenType;
public class TokenHandler {
private LinkedList<Token> tokens; // List of tokens
private Token currentToken;
private Token lastMatchedToken;
public TokenHandler(LinkedList<Token> tokens) {
this.tokens = tokens;
this.currentToken = null;
this.lastMatchedToken = null;
}
public Optional<Token> Peek(int j) {
if (j < tokens.size()) {
return Optional.of(tokens.get(j));
} else {
return Optional.empty();
}
}
public boolean MoreTokens() {
return !tokens.isEmpty();
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps