Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Programming: There are errors in the lexer and shank files. Please fix those errors and make sure all three files run correctly without any

Java Programming:

There are errors in the lexer and shank files. Please fix those errors and make sure all three files run correctly without any error. There must be no error at all. Below are the lexer, shank, and token files. Attached is the rubric as well.

Lexer.java

package mypack;

import java.util.ArrayList; import java.util.HashMap; import java.util.List; 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 keywords = new HashMap() {{ put("while", TokenType.WHILE); put("if", TokenType.IF); put("else", TokenType.ELSE); put("print", TokenType.PRINT); }}; private static HashMap symbols = new HashMap() {{ put('+', TokenType.PLUS); put('-', TokenType.MINUS); put('*', TokenType.MULTIPLY); put('/', TokenType.DIVIDE); put('=', TokenType.EQUALS); put(':', TokenType.COLON); put(';', TokenType.SEMICOLON); put('(', TokenType.LEFT_PAREN); put(')', TokenType.RIGHT_PAREN); put('{', TokenType.LEFT_BRACE); put('}', TokenType.RIGHT_BRACE); put('', TokenType.GREATER_THAN); }};

public Lexer(String input) { Lexer.input = input; index = 0; currentChar = input.charAt(index); }

private void nextChar() { index++; if (index >= input.length()) { currentChar = EOF; } else { currentChar = input.charAt(index); } }

private void skipWhiteSpace() { while (Character.isWhitespace(currentChar)) { nextChar(); } }

private int getIndentLevel() { int level = 0; int i = index; char c = input.charAt(i); while (c == ' ' || c == '\t') { if (c == '\t') { level += 1; } else if (c == ' ') { level += 1; } i++; if (i >= input.length()) { break; } 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 lines = Files.readAllLines(Paths.get(filename));

Lexer lexer = new Lexer();

for (String line : lines) { try { List tokens = lexer.lex(line);

for (Token token : tokens) { System.out.println(token); } } catch (Exception e) { System.out.println("Exception: " + e.getMessage()); } } } catch (IOException e) { System.out.println("Error: Could not read file '" + filename + "'."); } } }

Token.java

package mypack;

public class Token { public enum TokenType { WORD, NUMBER, SYMBOL }

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; } }

image text in transcribed

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

Students also viewed these Databases questions

Question

6. Identify characteristics of whiteness.

Answered: 1 week ago

Question

9. Explain the relationship between identity and communication.

Answered: 1 week ago