Java Programming: Below is parser.java and attached is circled all the methods that must be implemented in parser.java. Make sure to show a full working code & the screenshot of the output. Parser.java package mypack; import java.util.List; import java.util.Queue; public class Parser { private Queue tokens; public Parser(List tokens) { this.tokens = (Queue) tokens; } private Token matchAndRemove(Token.TokenType... types) { if (tokens.isEmpty()) { return null; } Token token = tokens.peek(); for (Token.TokenType type : types) { if (token.getTokenType() == type) { tokens.remove(); return token; } } return null; } private void expectEndsOfLine() throws SyntaxErrorException { while (matchAndRemove(Token.TokenType.ENDOFLINE) != null); if (tokens.isEmpty()) { throw new SyntaxErrorException(); } } private Token peek(int i) { if (tokens.size() < i) { return null; } return (Token) tokens.toArray()[i-1]; } public Node parse() throws SyntaxErrorException { Node result = null; do { result = expression(); if (result != null) { System.out.println(result.toString()); expectEndsOfLine(); } } while (result != null); return null; } private Node expression() throws SyntaxErrorException { Node left = term(); if (left == null) { return null; } while (true) { Token op = matchAndRemove(Token.TokenType.PLUS, Token.TokenType.MINUS); if (op == null) { return left; } Node right = term(); if (right == null) { throw new SyntaxErrorException(); } left = new MathOpNode(op.getTokenType(), left, right); } } private Node term() throws SyntaxErrorException { Node left = factor(); if (left == null) { return null; } while (true) { Token op = matchAndRemove(Token.TokenType.TIMES, Token.TokenType.DIVIDE, Token.TokenType.MODULUS); if (op == null) { return left; } Node right = factor(); if (right == null) { throw new SyntaxErrorException(); } left = new MathOpNode(op.getTokenType(), left, right); } } private Node factor() throws SyntaxErrorException { Token token = matchAndRemove(Token.TokenType.NUMBER, Token.TokenType.MINUS, Token.TokenType.LEFT_PAREN, Token.TokenType.IDENTIFIER); if (token == null) { return null; } switch (token.getTokenType()) { case NUMBER: return new IntegerNode(Integer.parseInt(token.getLexeme())); case MINUS: Node inner = factor(); if (inner == null) { throw new SyntaxErrorException(); } return new MathOpNode(Token.TokenType.MINUS, new IntegerNode(0), inner); case LEFT_PAREN: Node expression = expression(); if (expression == null) { throw new SyntaxErrorException(); } if (matchAndRemove(Token.TokenType.RIGHT_PAREN) == null) { throw new SyntaxErrorException(); } return expression; case IDENTIFIER: VariableReferenceNode varRefNode = new VariableReferenceNode(token.getLexeme()); if (matchAndRemove(Token.TokenType.LEFT_BRACE) != null) { Node indexExpr = expression(); if (indexExpr == null) { throw new SyntaxErrorException(); } varRefNode.setIndex(indexExpr); if (matchAndRemove(Token.TokenType.RIGHT_BRACE) == null) { throw new SyntaxErrorException(); } } return varRefNode; default: return null; }
Java Programming: Below is parser.java and attached is circled all the methods that must be implemented in parser.java. Make sure to show a full working code & the screenshot of the output.
Parser.java
package mypack;
import java.util.List;
import java.util.Queue;
public class Parser {
private Queue<Token> tokens;
public Parser(List<Token> tokens) {
this.tokens = (Queue<Token>) tokens;
}
private Token matchAndRemove(Token.TokenType... types) {
if (tokens.isEmpty()) {
return null;
}
Token token = tokens.peek();
for (Token.TokenType type : types) {
if (token.getTokenType() == type) {
tokens.remove();
return token;
}
}
return null;
}
private void expectEndsOfLine() throws SyntaxErrorException {
while (matchAndRemove(Token.TokenType.ENDOFLINE) != null);
if (tokens.isEmpty()) {
throw new SyntaxErrorException();
}
}
private Token peek(int i) {
if (tokens.size() < i) {
return null;
}
return (Token) tokens.toArray()[i-1];
}
public Node parse() throws SyntaxErrorException {
Node result = null;
do {
result = expression();
if (result != null) {
System.out.println(result.toString());
expectEndsOfLine();
}
} while (result != null);
return null;
}
private Node expression() throws SyntaxErrorException {
Node left = term();
if (left == null) {
return null;
}
while (true) {
Token op = matchAndRemove(Token.TokenType.PLUS, Token.TokenType.MINUS);
if (op == null) {
return left;
}
Node right = term();
if (right == null) {
throw new SyntaxErrorException();
}
left = new MathOpNode(op.getTokenType(), left, right);
}
}
private Node term() throws SyntaxErrorException {
Node left = factor();
if (left == null) {
return null;
}
while (true) {
Token op = matchAndRemove(Token.TokenType.TIMES, Token.TokenType.DIVIDE, Token.TokenType.MODULUS);
if (op == null) {
return left;
}
Node right = factor();
if (right == null) {
throw new SyntaxErrorException();
}
left = new MathOpNode(op.getTokenType(), left, right);
}
}
private Node factor() throws SyntaxErrorException {
Token token = matchAndRemove(Token.TokenType.NUMBER, Token.TokenType.MINUS, Token.TokenType.LEFT_PAREN, Token.TokenType.IDENTIFIER);
if (token == null) {
return null;
}
switch (token.getTokenType()) {
case NUMBER:
return new IntegerNode(Integer.parseInt(token.getLexeme()));
case MINUS:
Node inner = factor();
if (inner == null) {
throw new SyntaxErrorException();
}
return new MathOpNode(Token.TokenType.MINUS, new IntegerNode(0), inner);
case LEFT_PAREN:
Node expression = expression();
if (expression == null) {
throw new SyntaxErrorException();
}
if (matchAndRemove(Token.TokenType.RIGHT_PAREN) == null) {
throw new SyntaxErrorException();
}
return expression;
case IDENTIFIER:
VariableReferenceNode varRefNode = new VariableReferenceNode(token.getLexeme());
if (matchAndRemove(Token.TokenType.LEFT_BRACE) != null) {
Node indexExpr = expression();
if (indexExpr == null) {
throw new SyntaxErrorException();
}
varRefNode.setIndex(indexExpr);
if (matchAndRemove(Token.TokenType.RIGHT_BRACE) == null) {
throw new SyntaxErrorException();
}
}
return varRefNode;
default:
return null;
}
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps