Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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. For lexer.java, lines 62, 73, and 82 have the errors with the Token.TokenType. 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 lex(String inputString) throws Exception { input = inputString; index = 0; currentChar = input.charAt(index); List tokens = new ArrayList();

while (currentChar != EOF) { switch (currentState()) { case INTEGER_STATE: integerState(tokens); break; case DECIMAL_STATE: decimalState(tokens); break; case IDENTIFIER_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 tokens) { StringBuilder builder = new StringBuilder(); while (Character.isDigit(currentChar)) { builder.append(currentChar); advance(); } tokens.add(new Token(Token.TokenType.NUMBER, builder.toString())); }

private static void decimalState(List tokens) { StringBuilder builder = new StringBuilder(); builder.append(currentChar); advance(); while (Character.isDigit(currentChar)) { builder.append(currentChar); advance(); } tokens.add(new Token(Token.TokenType.NUMBER, builder.toString())); }

private static void identifierState(List tokens) { StringBuilder builder = new StringBuilder(); while (Character.isLetterOrDigit(currentChar)) { builder.append(currentChar); advance(); } tokens.add(new Token(Token.TokenType.WORD, builder.toString())); }

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 lines = Files.readAllLines(Paths.get(filename));

// 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 tokens = lexer.lex(line);

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

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

Recommended Textbook for

Database Development For Dummies

Authors: Allen G. Taylor

1st Edition

978-0764507526

More Books

Students also viewed these Databases questions