Java Program:  There are errors in the lexer and shank file. Please fix those errors and there must be no error in any of the code at all. Below is the lexer, shank, and token files. The shank file is the main method. There is a rubric attached as well.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Java Program: 

There are errors in the lexer and shank file. Please fix those errors and there must be no error in any of the code at all. Below is the lexer, shank, and token files. The shank file is the main method. There is a rubric attached as well.

 

Lexer.java

 

import java.util.ArrayList;
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;
}

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

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 + "'.");
}
}
}

 

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

 

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
Step 1
  1. In the HashMap declaration for the keywords, you can replace the anonymous inner class with a static block.
  2. In the getIndentLevel method, the variable level and i are never used after being incremented, so there is an infinite loop.
  3. In the same method, you should increment level by a specific number of spaces instead of just incrementing by one for each space or tab character.
  4. In the same method, you should break out of the loop when the current character is not a space or tab, not when i >= input.length().
 
package mypack;
 
import java.util.HashMap;
 
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<>() {{
put("while", TokenType.WHILE);
put("if", TokenType.IF);
put("else", TokenType.ELSE);
put("print", TokenType.PRINT);
}};
 
private static HashMap<Character, TokenType> 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;
}
}
 
 
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps

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

There are still errors in the lexer and shank files. Please fix those errors and make sure there are no errors in any of the files at all. Attached is an image of the error on the lexer file. There is still an error in the creation of the Lexer object in the main method. 

 

Lexer.java

package mypack;

import java.util.HashMap;

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

      private static HashMap<Character, TokenType> 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;
          }
}

 

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) {
    
   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 + "'.");
       }
       }
       }

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

private static final int STRING_STATE = 6;
private static final int CHAR_STATE = 7;
private static final int COMMENT_STATE = 8;
31
private static final char EOF= (char) -1;
32
33 /**The methods below will allow the user to throw an exception and set up the input, index, current
34 *character, line number, indent level, and the last indent level of the lexer file (lines 37-42)
27
28
29
30
35 */
367 38 39 40 44 2 B4U 45 46 F 48 49
41
42
430
44
47
500
51
52
53
54
55
56
57
58
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<>() {{
put("while", TokenType.WHILE);
put("if", TokenType.IF);
put ("else", TokenType.ELSE);
put("print", TokenType.PRINT);
}};
private static HashMap<Character, TokenType> symbols = new HashMap<>() {{
put('+', TokenType.PLUS);
put('-', TokenType.MINUS);
put('*', TokenType.MULTIPLY);
put('/', TokenType.DIVIDE);
put('=' TokenType. EQUALS);
put(':', TokenType.COLON);
put(';
TokenType.SEMICOLON);
nut('(' Token Tyne LEFT PAREN).
Problems @Javadoc Declaration Console X
<terminated > Shank (1) [Java Application] C:\Users\subri\.p2\pool\plugins\org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_17.0.1.v2021111
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method lex (String) is undefined for the type Lexer
at mypack.Shank.main(Shank.java:35)
Transcribed Image Text:private static final int STRING_STATE = 6; private static final int CHAR_STATE = 7; private static final int COMMENT_STATE = 8; 31 private static final char EOF= (char) -1; 32 33 /**The methods below will allow the user to throw an exception and set up the input, index, current 34 *character, line number, indent level, and the last indent level of the lexer file (lines 37-42) 27 28 29 30 35 */ 367 38 39 40 44 2 B4U 45 46 F 48 49 41 42 430 44 47 500 51 52 53 54 55 56 57 58 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<>() {{ put("while", TokenType.WHILE); put("if", TokenType.IF); put ("else", TokenType.ELSE); put("print", TokenType.PRINT); }}; private static HashMap<Character, TokenType> symbols = new HashMap<>() {{ put('+', TokenType.PLUS); put('-', TokenType.MINUS); put('*', TokenType.MULTIPLY); put('/', TokenType.DIVIDE); put('=' TokenType. EQUALS); put(':', TokenType.COLON); put('; TokenType.SEMICOLON); nut('(' Token Tyne LEFT PAREN). Problems @Javadoc Declaration Console X <terminated > Shank (1) [Java Application] C:\Users\subri\.p2\pool\plugins\org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_17.0.1.v2021111 Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method lex (String) is undefined for the type Lexer at mypack.Shank.main(Shank.java:35)
Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Files and Directory
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
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education