Java: Below are 3 java files along with a text file. Make sure in the console, shank.txt is read into tokens. Make sure to show the 3 java files with the screenshot of shank.txt being printed out in the console. The output must show shank.txt being printed out in tokens. There must be no error at all.    Main.java 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 + "'.");    }    } }     Lexer.java   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);       }    } }   Token.java 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;    }    }   shank.txt Fibonoacci (Iterative)   define add (num1,num2:integer var sum : integer) variable counter : integer   Finonacci(N)   int N = 10;         while counter < N define start () variables num1,num2,num3 : integer            add num1,num2,var num3            {num1 and num2 are added together to get num3}             num1 = num2;             num2 = num3;             counter = counter + 1;        GCD (Recursive)   define add (int a,int b : gcd)   if b = 0     sum = a   sum gcd(b, a % b)     GCD (Iterative)   define add (inta, intb : gcd) if a = 0 sum = b if b = 0 sum = a while counter a != b              if a > b                 a = a - b;             else                 b = b - a;         sum = a; variables a,b : integer          a = 60    b = 96         subtract a,b

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Java: Below are 3 java files along with a text file. Make sure in the console, shank.txt is read into tokens. Make sure to show the 3 java files with the screenshot of shank.txt being printed out in the console. The output must show shank.txt being printed out in tokens. There must be no error at all. 

 

Main.java

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

  
   Lexer lexer = new Lexer();

   
   for (String line : lines) {
   try {
   List<Token> 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 + "'.");
   }
   }
}

 

 

Lexer.java

 

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<Token> lex(String inputString) throws Exception {
      input = inputString;
      index = 0;
      currentChar = input.charAt(index);
      List<Token> tokens = new ArrayList<Token>();

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

 

Token.java

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

 

shank.txt

Fibonoacci (Iterative)
 
define add (num1,num2:integer var sum : integer)
variable counter : integer
  Finonacci(N)
  int N = 10;
        while counter < N
define start ()
variables num1,num2,num3 : integer
           add num1,num2,var num3
           {num1 and num2 are added together to get num3}
            num1 = num2;
            num2 = num3;
            counter = counter + 1;
    
 
GCD (Recursive)
 
define add (int a,int b : gcd)
  if b = 0
    sum = a
  sum gcd(b, a % b)
 
 
GCD (Iterative)
 
define add (inta, intb : gcd)
if a = 0
sum = b
if b = 0
sum = a
while counter a != b 
            if a > b
                a = a - b;
            else
                b = b - a;
        sum = a;
variables a,b : integer
         a = 60
   b = 96
        subtract a,b
   
 
 
 
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 5 steps with 11 images

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

I ran all 3 java files with the text file and this is the error I got. The error is attached as an image. Please double check and make sure all 3 java files along with the text files work with the screenshot of shank.txt being printed out in the console. The output must show shank.txt being printed out in tokens. There must be no error at all. 

▶ Run ✪ Debug ■ Stop
Token java
Main.java
1- import java.util.ArrayList;
2 import java.util.List;
3
4- public class Lexer
5
6
7
8
9
10
11
12
13
14
15
16
스
17-
18
PANNENHUN
19
20
21
22
23
24
25
26
27
28
Lexer.java
Share H Save {} Beautify
shank.txt ⠀
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 char EOF = (char) -1;
private String input;
private int index;
private char currentChar;
public Lexer() {
currentChar = '; // Initialize currentChar to a space
}
public List<Token> lex(String inputString) throws Exception {
input = inputString;
index = 0;
currentChar = input.charAt(index);
List<Token> tokens = new ArrayList<>();
while (currentChar != EOF) {
if (Character.isWhitespace (currentChar)) {
±
$
Error: Exactly one argument is required.
... Program finished with exit code 0
Press ENTER to exit console.
input
Language Java
Transcribed Image Text:▶ Run ✪ Debug ■ Stop Token java Main.java 1- import java.util.ArrayList; 2 import java.util.List; 3 4- public class Lexer 5 6 7 8 9 10 11 12 13 14 15 16 스 17- 18 PANNENHUN 19 20 21 22 23 24 25 26 27 28 Lexer.java Share H Save {} Beautify shank.txt ⠀ 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 char EOF = (char) -1; private String input; private int index; private char currentChar; public Lexer() { currentChar = '; // Initialize currentChar to a space } public List<Token> lex(String inputString) throws Exception { input = inputString; index = 0; currentChar = input.charAt(index); List<Token> tokens = new ArrayList<>(); while (currentChar != EOF) { if (Character.isWhitespace (currentChar)) { ± $ Error: Exactly one argument is required. ... Program finished with exit code 0 Press ENTER to exit console. input Language Java
Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Constants and Variables
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-engineering and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY