Question: Java Help: Used Eclipse for this code. Ran the entire code and got errors for the lexer file. The main goal is to make sure
Java Help: Used Eclipse for this code. Ran the entire code and got errors for the lexer file. The main goal is to make sure the lexer works completely and prints the output correctly. Below is the three files and fix those errors and show the revised code for all three files with the screenshot of the output. Make sure the lexer works 100%. There must be no errors at all.
Lexer.java
package mypack;
import java.util.ArrayList; 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 ERROR_STATE = 4;
private static final char EOF = (char) -1;
private static String input; private static int index; private static char currentChar;
public List
while (currentChar != EOF) { switch (currentState()) { case INTEGER_STATE: integerState(tokens); break; case DECIMAL_STATE: decimalState(tokens); break; case IDENTIFIER_STATE _STATE: identifierState(tokens); break; case ERROR_STATE: throw new Exception("Invalid character: " + currentChar); default: break; } } return tokens; }
private static int currentState() { if (Character.isDigit(currentChar)) { return INTEGER_STATE; } else if (Character.isLetter(currentChar)) { return IDENTIFIER_STATE; } else if (currentChar == '.') { return DECIMAL_STATE; } else { return ERROR_STATE; } }
private static void integerState(List
private static void decimalState(List
private static void identifierState(List
private static void advance() { index++; if (index >= input.length()) { currentChar = EOF; } else { currentChar = input.charAt(index); } } }
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) { // Ensure there is exactly one command line argument if (args.length != 1) { System.out.println("Error: Exactly one argument is required."); System.exit(0); }
// Get the filename from the command line argument String filename = args[0];
try { // Read all lines from the file List
// Create an instance of the Lexer class Lexer lexer = new Lexer();
// Parse each line using the lex method of the Lexer class for (String line : lines) { try { List
// Print each token after the lexing is complete 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 { private enum TokenType { WORD, NUMBER, SYMBOL }
private 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; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
