Question: Java Programming: There are errors in the parser.java file. Make sure there are no error in the any of the files given. All the files
Java Programming: There are errors in the parser.java file. Make sure there are no error in the any of the files given. All the files must be working correctly without any error. Below are the lexer, parser, token, and shank files. The shank file is the main method file. Attached is rubric.
Lexer.java
Lexer.java
package mypack;
import java.util.HashMap; import java.util.List; import mypack.Token.TokenType;
public class Lexer {
private static final int INTEGER_STATE = 1; private static final int DECIMAL_STATE = 2; private static final int IDENTIFIER_STATE = 3; private static final int SYMBOL_STATE = 4; private static final int ERROR_STATE = 5; private static final int STRING_STATE = 6; private static final int CHAR_STATE = 7; private static final int COMMENT_STATE = 8;
private static final char EOF = (char) -1;
private static String input; private static int index; private static char currentChar; private static int lineNumber = 1; private static int indentLevel = 0; private static int lastIndentLevel = 0;
private static HashMap
private static HashMap
Parser.java
package mypack;
import java.util.List; import java.util.Queue;
public class Parser { private Queue
public Parser(List
private Token matchAndRemove(Token.TokenType type) { if (tokens.isEmpty() || tokens.peek().getType() != type) { return null; } return tokens.remove(); }
private void expectEndsOfLine() throws SyntaxErrorException { while (matchAndRemove(Token.TokenType.ENDOFLINE) != null); if (tokens.isEmpty()) { throw new SyntaxErrorException("Expected at least one ENDOFLINE token."); } }
private Token peek(int i) { if (tokens.size()
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() { 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("Expected a term after " + op.getLexeme()); } left = new MathOpNode(op.getType(), left, right); } }
}
private Node term() { Node left = factor(); if (left == null) { return null; } while (true) { Token op = matchAndRemove(Token.TokenType.TIMES, Token.TokenType.DIVIDE, Token.TokenType.MOD); if (op == null) { return left; } Node right = factor(); if (right == null) { throw new SyntaxErrorException("Expected a factor after " + op.getLexeme()); } left = new MathOpNode(op.getType(), left, right); } }
private Node factor() { Token token = matchAndRemove(Token.TokenType.NUMBER, Token.TokenType.MINUS, Token.TokenType.LPAREN); if (token == null) { return null; } switch (token.getType()) { case NUMBER: return new IntegerNode(Integer.parseInt(token.getLexeme())); case MINUS: Node inner = factor(); if (inner == null) { throw new SyntaxErrorException("Expected a factor after -"); } return new MathOpNode(Token.TokenType.MINUS, new IntegerNode(0), inner); case LPAREN: Node expression = expression(); if (expression == null) { throw new SyntaxErrorException("Expected an expression after ("); } if (matchAndRemove(Token.TokenType.RPAREN) == null) { throw new SyntaxErrorException("Expected ) after expression"); } return expression; default: return null; // unreachable } }
Token.java
package mypack;
public class Token { public enum TokenType { WORD, WHILE, IF, ELSE, NUMBER, SYMBOL, PLUS, MINUS, MULTIPLY, DIVIDE, EQUALS, COLON, SEMICOLON, LEFT_PAREN, RIGHT_PAREN, LEFT_BRACE, RIGHT_BRACE, LESS_THAN, GREATER_THAN, PRINT }
public TokenType tokenType; private String value; public Token(TokenType type, String val) { this.tokenType = type; this.value = val; } public TokenType getTokenType() { return this.tokenType; } public String toString() { return this.tokenType + ": " + this.value; } }
Shank.java
package mypack;
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.List;
public class Shank { public static void main(String[] args) { if (args.length != 1) { System.out.println("Error: Exactly one argument is required."); System.exit(0); }
String filename = args[0];
try { List
for (String line : lines) { try { Lexer lexer = new Lexer(line); List

\begin{tabular}{|l|l|l|l|l|} \hline Rubric & Poor & OK & Good & Great \\ \hline Comments & None/Excessive (0) & "What"not"Why",few(5) & Some"what"commentsormissingsome(7) & Anythingnotobvioushasreasoning(10) \\ \hline Variable/Functionnaming & Singleletterseverywhere(0) & Lotsofabbreviations(5) & Fullwordsmostofthetime(8) & Full words, descriptive (10) \\ \hline Node & None (0) & & & HasToString()andisabstract(5) \\ \hline IntegerNode & None(0) & & ExtendsNode,hasconstructorandprivatemember(5) \\ \hline FloatNode & None(0) & ExtendsNode,hasconstructorandprivatemember(5) \\ \hline \end{tabular} \begin{tabular}{|l|l|l|l|l|} \hline matchAndRemove & None (0) & & & returnsmatchingnextnodeornull(5) \\ \hline expectEndsOfLine & None (0) & & & Matchesmultipleendsoflineandthrowswhenitdoesnt(5) \\ \hline peek & None (0) & & Looksahead,returnsnullwhenitcant(5) \\ \hline parse & None(0) & & Loopsoverexpression()andexpectEndsOfLine()(5) & Loopsoverexpression()andexpectEndsOfLine()andprintsresults(10) \\ \hline expression & none (0) & callsterm,processes1+/(5) & callsterm,loopsover+/(10) \\ \hline term & none (0) & callsfactor,processes1or/or%(5) & callsfactor,loopsoveror/or%(10) \\ \hline factor & none (0) & handles2ofparens,negatives,integersandfloats(10) & handles3ofparens,negatives,integersandfloats(15) & handlesparens,negatives,integersandfloats(20) \\ \hline \end{tabular}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
