Java Programming: Below is the lexer, shank and token files along with the shank.txt file. The shank file is the main method file. The lexer must break up the input text stream into lexemes and return a token object for each one in the shank.txt file. Make sure to fix the errors in the lexer.java file and show the complete code for lexer.java.There must be no error in the code at all. Run the whole code and show the output which the shank.txt must be printed out in the terminal. Attached is the rubric.   Lexer.java package mypack;   import java.util.HashMap; import java.util.List; import mypack.Token.TokenType; 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.LESS_THAN); 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; } } return level; } public List lex(String line) { List tokens = newArrayList(); Lexer lexer = new Lexer(line); while (currentChar != EOF) { if (Character.isWhitespace(currentChar)) {      skipWhiteSpace();      continue; } if (currentChar == '#') { while (currentChar != '\n' && currentChar != EOF) {         nextChar();             }             skipWhiteSpace();             continue;         }         if (Character.isDigit(currentChar)) {             int startPos = index;             int state = INTEGER_STATE;             String value = "";             } } Token.java package mypack; public class Token { public enum TokenType { WORD, WHILE, IF, ELSE, NUMBER, SYMBOL, PLUS, MINUS, MULTIPLY, DIVIDE, EQUALS, COLON, SEMICOLON, LEFT_PAREN, RIGHT_PAREN, LEFT_BRACE, RIGHT_BRACE, LESS_THAN, GREATER_THAN, PRINT } 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.java package mypack; import java.io.IOException; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; public class Shank { public static void main(String[] args) { Path filePath = Paths.get("shank.txt"); Charset charset = StandardCharsets.UTF_8; 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)); for (String line : lines) { try { Lexer lexer = new Lexer(line); 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 + "'."); } } }   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

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter12: Exception Handling
Section: Chapter Questions
Problem 6PE
icon
Related questions
Question

Java Programming: Below is the lexer, shank and token files along with the shank.txt file. The shank file is the main method file. The lexer must break up the input text stream into lexemes and return a token object for each one in the shank.txt file. Make sure to fix the errors in the lexer.java file and show the complete code for lexer.java.There must be no error in the code at all. Run the whole code and show the output which the shank.txt must be printed out in the terminal. Attached is the rubric.

 

Lexer.java

package mypack;

 

import java.util.HashMap;
import java.util.List;
import mypack.Token.TokenType;


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<String, TokenType> keywords = new HashMap<String, TokenType>() {{
put("while", TokenType.WHILE);
put("if", TokenType.IF);
put("else", TokenType.ELSE);
put("print", TokenType.PRINT);
}};

private static HashMap<Character, TokenType> symbols = new HashMap<Character, TokenType>() {{
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.LESS_THAN);
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;
}
}
return level;
}


public List<Token> lex(String line) {
List<Token> tokens = newArrayList<Token>();
Lexer lexer = new Lexer(line);
while (currentChar != EOF) {
if (Character.isWhitespace(currentChar)) {
     skipWhiteSpace();
     continue;
}
if (currentChar == '#') {
while (currentChar != '\n' && currentChar != EOF) {

        nextChar();
            }
            skipWhiteSpace();
            continue;
        }
        if (Character.isDigit(currentChar)) {
            int startPos = index;
            int state = INTEGER_STATE;
            String value = "";
           

}
}

Token.java

package mypack;


public class Token {

public enum TokenType {
WORD, WHILE, IF, ELSE,
NUMBER,
SYMBOL, PLUS, MINUS, MULTIPLY, DIVIDE, EQUALS, COLON, SEMICOLON, LEFT_PAREN, RIGHT_PAREN, LEFT_BRACE, RIGHT_BRACE, LESS_THAN, GREATER_THAN, PRINT
}

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.java

package mypack;


import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;


public class Shank {
public static void main(String[] args) {
Path filePath = Paths.get("shank.txt");
Charset charset = StandardCharsets.UTF_8;

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

for (String line : lines) {
try {
Lexer lexer = new Lexer(line);
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 + "'.");
}
}
}

 

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

 

Rubric
Comments
Variable/Function
naming
Keyword lexing
Punctuation lexing
String and
character literals
Invalid symbols
throw an exception
Comments
Indentation
Poor
None/Excessive (0)
Single letters everywhere
(0)
None(0)
None(0)
None (0)
None (0)
None (0)
None (0)
OK
"What" not "Why", few
(5)
Lots of abbreviations
(5)
A few and/or no hash
map (5)
A few punctuation
marks lex(5)
Exception thrown
and/or some cases
missing (5)
Good
Some "what" comments
or missing some (7)
Full words most of the
time (8)
At least 1/2 of the
keywords in the
hashmap and detected
(10)
Almost all lex but issues
with the multi-character
punctuation (10)
Custom exception
always thrown (10)
Single line handled (5)
Attempted (5)
Great
Anything not obvious has
reasoning (10)
Full words, descriptive (10)
Nearly all keywords in the
hashmap and appropriately
detected (15)
Almost all lex (15)
Lex correctly (10)
Custom exception thrown with
good ToString() (15)
Multi-line handled (10)
Handles all cases (15)
Transcribed Image Text:Rubric Comments Variable/Function naming Keyword lexing Punctuation lexing String and character literals Invalid symbols throw an exception Comments Indentation Poor None/Excessive (0) Single letters everywhere (0) None(0) None(0) None (0) None (0) None (0) None (0) OK "What" not "Why", few (5) Lots of abbreviations (5) A few and/or no hash map (5) A few punctuation marks lex(5) Exception thrown and/or some cases missing (5) Good Some "what" comments or missing some (7) Full words most of the time (8) At least 1/2 of the keywords in the hashmap and detected (10) Almost all lex but issues with the multi-character punctuation (10) Custom exception always thrown (10) Single line handled (5) Attempted (5) Great Anything not obvious has reasoning (10) Full words, descriptive (10) Nearly all keywords in the hashmap and appropriately detected (15) Almost all lex (15) Lex correctly (10) Custom exception thrown with good ToString() (15) Multi-line handled (10) Handles all cases (15)
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Unreferenced Objects
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT