Java Code: For Lexer.java Make a HashMap of in your Lex class. Below is a list of the keywords that you need. Make token types and populate the hash map in your constructor (I would make a helper method that the constructor calls). while, if, do, for, break, continue, else, return, BEGIN, END, print, printf, next, in, delete, getline, exit, nextfile, function Modify “ProcessWord” so that it checks the hash map for known words and makes a token specific to the word with no value if the word is in the hash map, but WORD otherwise. For example, Input: for while hello do Output: FOR WHILE WORD(hello) DO Make a token type for string literals. In Lex, when you encounter a “, call a new method (I called it HandleStringLiteral() ) that reads up through the matching “ and creates a string literal token ( STRINGLITERAL(hello world) ). Be careful of two things: make sure that an empty string literal ( “” ) works and make sure to deal with escaped “ (String quote = “She said, \”hello there\” and then she left.”;) Make a new token type, a new method (HandlePattern) and call it from Lex when you encounter a backtick. The last thing that we need to deal with in our lexer is symbols. Most of these will be familiar from Java, but a few I will detail a bit more. We will be using two different hash maps – one for two-character symbols (like ==, &&, ++) and one for one character symbols (like +, -, $). Why? Well, some two-character symbols start with characters that are also symbols (for example, + and +=). We need to prioritize the += and only match + if it is not a +=. Create a method called “ProcessSymbol” – it should use PeekString to get 2 characters and look them up in the two-character hash map. If it exists, make the appropriate token and return it. Otherwise, use PeekString to get a 1 character string. Look that up in the one-character hash map. If it exists, create the appropriate token and return it. Don’t forget to update the position in the line. If no symbol is found, return null. Call ProcessSymbol in your lex() method. If it returns a value, add the token to the token list. Make sure all the functionality of the unit test are tested. Below are lexer.java and token.java. Make sure to show full code with screenshot of the output. Attached is checklist. Lexer.java import java.util.LinkedList; public class Lexer { private StringHandler stringHandler; private int lineNumber; private int charPosition; public Lexer(String input) { stringHandler = new StringHandler(input); lineNumber = 1; charPosition = 0; } public LinkedList lex() { LinkedList tokens = new LinkedList<>(); while (!stringHandler.isDone()) { char c = stringHandler.peek(0); if (c == ' ' || c == '\t') { stringHandler.swallow(1); charPosition++; } else if (c == '\n') { tokens.add(new Token(TokenType.SEPARATOR, lineNumber, charPosition)); stringHandler.swallow(1); lineNumber++; charPosition = 0; } else if (Character.isLetter(c)) { tokens.add(processWord()); } else if (Character.isDigit(c)) { tokens.add(processNumber()); } else { throw new RuntimeException("Unrecognized character: " + c); } } return tokens; } private Token processWord() { StringBuilder value = new StringBuilder(); while (!stringHandler.isDone() && (Character.isLetterOrDigit(stringHandler.peek(0)) || stringHandler.peek(0) == '_' || stringHandler.peek(0) == ',')) { value.append(stringHandler.getChar()); charPosition++; } return new Token(TokenType.WORD, value.toString(), lineNumber, charPosition - value.length()); } private Token processNumber() { StringBuilder value = new StringBuilder(); while (!stringHandler.isDone() && (Character.isDigit(stringHandler.peek(0)) || stringHandler.peek(0) == '.')) { value.append(stringHandler.getChar()); charPosition++; } return new Token(TokenType.NUMBER, value.toString(), lineNumber, charPosition - value.length()); } } Token.java enum TokenType { WORD, NUMBER, SEPARATOR } public class Token { private TokenType type; private String value; private int lineNumber; private int charPosition; public Token(TokenType type, int lineNumber, int charPosition) { this.type = type; this.lineNumber = lineNumber; this.charPosition = charPosition; } public Token(TokenType type, String value, int lineNumber, int charPosition) { this(type, lineNumber, charPosition); this.value = value; } public String toString() { if (value == null) { return type.toString(); } else { return type.toString() + "(" + value + ")"; } } }
Java Code: For Lexer.java
Make a HashMap of <String, TokenType> in your Lex class. Below is a list of the keywords that you need. Make token types and populate the hash map in your constructor (I would make a helper method that the constructor calls).
while, if, do, for, break, continue, else, return, BEGIN, END, print, printf, next, in, delete, getline, exit, nextfile, function
Modify “ProcessWord” so that it checks the hash map for known words and makes a token specific to the word with no value if the word is in the hash map, but WORD otherwise.
For example,
Input: for while hello do
Output: FOR WHILE WORD(hello) DO
Make a token type for string literals. In Lex, when you encounter a “, call a new method (I called it HandleStringLiteral() ) that reads up through the matching “ and creates a string literal token ( STRINGLITERAL(hello world) ). Be careful of two things: make sure that an empty string literal ( “” ) works and make sure to deal with escaped “ (String quote = “She said, \”hello there\” and then she left.”;)
Make a new token type, a new method (HandlePattern) and call it from Lex when you encounter a backtick.
The last thing that we need to deal with in our lexer is symbols. Most of these will be familiar from Java, but a few I will detail a bit more. We will be using two different hash maps – one for two-character symbols (like ==, &&, ++) and one for one character symbols (like +, -, $). Why? Well, some two-character symbols start with characters that are also symbols (for example, + and +=). We need to prioritize the += and only match + if it is not a +=.
Create a method called “ProcessSymbol” – it should use PeekString to get 2 characters and look them up in the two-character hash map. If it exists, make the appropriate token and return it. Otherwise, use PeekString to get a 1 character string. Look that up in the one-character hash map. If it exists, create the appropriate token and return it. Don’t forget to update the position in the line. If no symbol is found, return null. Call ProcessSymbol in your lex() method. If it returns a value, add the token to the token list.
Make sure all the functionality of the unit test are tested. Below are lexer.java and token.java. Make sure to show full code with screenshot of the output. Attached is checklist.
Lexer.java
![Input: for while hello do
Output: FOR WHILE WORD (hello) DO](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fe7ddc10c-4670-40fd-b02c-6a60c5fcc2f2%2Fa2553b7b-01c8-4208-965f-bfebf4e822ec%2Fsp7faib_processed.png&w=3840&q=75)
![Code Style
Unit Tests
Keywords
Comments
StringLiteral
Patterns
Symbol - One
Character
Symbol - Two
Character
ProcessSymbol
Some good
naming, some
necessary
comments (3)
Don't exist (0) At least one (6)
Few
comments,
bad names (0)
Unmodified
(0)
Not skipped
(0)
Not processed
(0)
Processed ad
hoc (3)
No hashmap
(0)
No hashmap
(0)
None (0)
Token type
exists (3)
Not processed Token type
(0)
exists (3)
Mostly good
naming, most
necessary
comments (6)
Missing tests
(12)
Hashmap
created and
populated (6)
Token type
exists and
Method exists
and is called (6)
Token type
exists and
Method exists
and is called (6)
Hash map exists Most symbols
(3)
exist (6)
Hash map exists Most symbols
(3)
exist (6)
Exists, looks in
one map (3)
Exists, looks in
both maps (6)
Good naming, non-trivial
methods well commented,
static only when necessary,
private members (10)
All functionality tested (20)
Keywords properly
recognized and proper
tokens created (15)
Rest of line ignored (5)
String literals properly
processed into their own
tokens; line count and
column updated (10)
Patterns properly
processed into their own
tokens; line count and
column updated (10)
All symbols exist with
reasonable token types (10)
All symbols exist with
reasonable token types (10)
Exists, looks in twoChar,
then oneChar, updates
StringManager and position
correctly (10)](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fe7ddc10c-4670-40fd-b02c-6a60c5fcc2f2%2Fa2553b7b-01c8-4208-965f-bfebf4e822ec%2Fowq3p8s_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 6 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
Where are the unit test? I need to see various junit test cases being tested.
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)