Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Help: Ran the entire code and got errors. The main goal is to make sure the lexer works completely and prints the output correctly.

Java Help: Ran the entire code and got errors. 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. There must be no errors at all.

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 { private enum TokenType { WORD, NUMBER, SYMBOL }

private TokenType tokenType; Privateer String-Wert;

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

//Remove public class Main { public static void main(String[] args) { Token token = new Token(Token.TokenType.WORD, "hello"); -Carrot. out.println(token); } }

Lexer.java

package mypack;

import java.util.ArrayList;

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 ArrayList tokens; private static String input; private static int index; private static char currentChar;

public static void lex(String inputString) throws Exception { input = inputString; index = 0; currentChar = input.charAt(index); tokens = new ArrayList();

while (currentChar != EOF) { switch (currentState()) { case INTEGER_STATE: integerState(); break; case DECIMAL_STATE: decimalState(); break; case IDENTIFIER_STATE: identifierState(); break; case ERROR_STATE: throw new Exception("Error: Invalid character " + currentChar); } }

for (Token token : tokens) { System.out.println(token); } }

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() { StringBuilder integer = new StringBuilder();

while (Character.isDigit(currentChar)) { integer.append(currentChar); nextChar(); }

if (currentChar == '.') { nextChar(); decimalState(integer.toString()); } else { tokens.add(new Token("INTEGER", integer.toString())); } } private static void decimalState(String prefix) { StringBuilder decimal = new StringBuilder(prefix).append(".");

while (Character.isDigit(currentChar)) { decimal.append(currentChar); nextChar(); }

tokens.add(new Token("DECIMAL", decimal.toString())); }

private static void decimalState() { decimalState(""); }

private static void identifierState() { StringBuilder identifier = new StringBuilder();

while (Character.isLetterOrDigit(currentChar)) { identifier.append(currentChar); nextChar(); }

tokens.add(new Token("IDENTIFIER", identifier.toString())); }

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

class Token { private String

}

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_2

Step: 3

blur-text-image_3

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

Advanced Oracle Solaris 11 System Administration

Authors: Bill Calkins

1st Edition

0133007170, 9780133007176

More Books

Students also viewed these Databases questions

Question

Does flooding occur over a trunk line?

Answered: 1 week ago

Question

2. Place a value on the outcomes.

Answered: 1 week ago