Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Theory And Application Bio Science And Bio Technology International Conferences DTA And BSBT 2011 Held As Part Of The Future Generation In Computer And Information Science 258

Authors: Tai-hoon Kim ,Hojjat Adeli ,Alfredo Cuzzocrea ,Tughrul Arslan ,Yanchun Zhang ,Jianhua Ma ,Kyo-il Chung ,Siti Mariyam ,Xiaofeng Song

2011th Edition

3642271561, 978-3642271564

More Books

Students also viewed these Databases questions