Question
I need help with making a lexer in java. I've been stuck on it this whole weekend For example, if i have this z54=89 in
I need help with making a lexer in java. I've been stuck on it this whole weekend For example, if i have this z54=89 in my textfile, my Lexer should be able to identify them as,identifier,assignmentOp, and integer.
Here are token types the lexer should be able to identify
Identifier: anything that starts with a letter and is followed by letters and digits Integer: digits AssignmentOperator:= PlusOperator: a single + UnknownOperator: #,"&" EndOfFile
If the text file contains three lines ab=29 cd=2+8 ^
The Lexer's output should be:
Type Value
Identifier ab
AssignOp =
Integer 29
Identifier cd
AssignOp =
Integer 2
PlusOp +
Integer 8 UnknownOperator ^
EndOfFile -
Instructions 1Have a Lexer class and a Token class 2Lexer class methods: public constructor - has a file name as a parameter private getInput - reads the data from the file into a [char] array. public getNextToken - returns a Token object. private functions for getIdentifier and getInteger. TThey handle the extraction of the rest of an identifier/integer once identified. A main function - calls getNextToken within a loop and prints out all the tokens in the given file.
3Token class - must have two private, type and value. both of a type String
It should also have a constructor which takes two parameters, It should also have a toString method which prints a tokens type and value.
Strating code:
import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.FileReader;
public class Lexer { final int MAXSIZE=3000; char buffer[]; int bufSize=0; int index=0;
public Lexer(String fileName) { this.buffer = new char[MAXSIZE]; getInput(fileName); }
public void getInput(String fileName) { File inputFile = new File (fileName); try (FileReader reader = new FileReader(inputFile)) { this.bufSize = reader.read(this.buffer,0,1000); } catch(FileNotFoundException fnf) { System.out.println("File not found."); System.out.println(fnf.getMessage()); System.exit(1); } catch(IOException exc) { System.out.println("IO Exception"); System.out.println(exc.getMessage()); System.exit(1); } }
public static void main(String[] args) { } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started